Search code examples
javagwtgwt-celltable

Not able to sort CellTable Column


Trying to make my CellTable Colum sortable but I'm not getting it to work. I'm having an MVP application which gets data from a rest service. To show the data within the table works fine but to sort is doesn't work.

public class LicenseUsageUserViewImpl<T> extends Composite implements LicenseUsageUserView<T> {

    @UiTemplate("LicenseUsageUserView.ui.xml")
    interface LicenseDataViewUiBinder extends UiBinder<ScrollPanel,LicenseUsageUserViewImpl> {}

    private static LicenseDataViewUiBinder uiBinder = GWT.create(LicenseDataViewUiBinder.class);

    @UiField
    CellTable<GWTLicenseUser> licenseUserCellTable;

    List<GWTLicenseUser> licenseUsers;

    ListDataProvider<GWTLicenseUser> dataProvider;

    public List<GWTLicenseUser> getLicenseUsers() {
        return licenseUsers;
    }

    public void setLicenseUsers(List<GWTLicenseUser> licenseUsers) {
        this.licenseUsers = licenseUsers;
    }

    @UiField Label header;

    ListHandler<GWTLicenseUser> sortHandler;

    public LicenseUsageUserViewImpl() {
        initWidget(uiBinder.createAndBindUi(this));
        initCellTable();
    }

    @Override
    public void setLicenseUsersTable(List<GWTLicenseUser> tmpLicenseUsers) {
        if (tmpLicenseUsers.isEmpty()) {

            licenseUserCellTable.setVisible(false);

        } else {

            setLicenseUsers(tmpLicenseUsers);
            licenseUserCellTable.setWidth("100%");
            licenseUserCellTable.setVisible(true);
            licenseUserCellTable.setPageSize(getLicenseUsers().size());
            licenseUserCellTable.setRowCount(getLicenseUsers().size(), false);
            licenseUserCellTable.setRowData(0, getLicenseUsers());
            licenseUserCellTable.setVisibleRange(new Range(0, licenseUserCellTable.getRowCount()));

            sortHandler.setList(getLicenseUsers());

            dataProvider.getList().clear();
            dataProvider.getList().addAll(getLicenseUsers());
        }

    }

    @Override
    public void initCellTable() {

        sortHandler = new ListHandler<GWTLicenseUser>(getLicenseUsers());
        licenseUserCellTable.addColumnSortHandler(sortHandler);

        licenseUserCellTable.setWidth("100%");
        licenseUserCellTable.setVisible(true);
        licenseUserCellTable.setVisibleRange(new Range(0, licenseUserCellTable.getRowCount()));

        // Create a data provider.
        dataProvider = new ListDataProvider<GWTLicenseUser>();

        // Connect the table to the data provider.
        dataProvider.addDataDisplay(licenseUserCellTable);

        licenseUserCellTable.setWidth("100%");
        licenseUserCellTable.setAutoHeaderRefreshDisabled(true);
        licenseUserCellTable.setAutoFooterRefreshDisabled(true);


        // userID
        TextColumn<GWTLicenseUser> userIdColumn = new TextColumn<GWTLicenseUser>() {

            @Override
            public String getValue(GWTLicenseUser object) {

                if (object != null ){
                    return object.getUserId();
                } else {
                    return "NULL";
                }
            }

        };
        userIdColumn.setSortable(true);
        sortHandler.setComparator(userIdColumn, new Comparator<GWTLicenseUser>() {
            @Override
            public int compare(GWTLicenseUser o1, GWTLicenseUser o2) {
              return o1.getUserId().compareTo(o2.getUserId());
            }
          });
        licenseUserCellTable.addColumn(userIdColumn, "User ID");


        // more column entries


        licenseUserCellTable.getColumnSortList().push(userIdColumn);
        licenseUserCellTable.getColumnSortList().push(countColumn);

        licenseUserCellTable.addColumnSortHandler(sortHandler);

    }

}

setLicenseUsersTable is called from my activity with the response list of my users. When I start my application and make a rest call my data is provide and put into my list also shown within the CellTable but its not sortable, but I have this sort icon before my colum name. I figured I post the whole code because I think its know easier to see what I'm trying to do.

Thanks for any help.


Solution

  • Remove this line:

    sortHandler.setList(getLicenseUsers());
    

    You already passed a List into the SortHandler constructor in

    sortHandler = new ListHandler<GWTLicenseUser>(getLicenseUsers());
    

    Also, instead of

    setLicenseUsers(tmpLicenseUsers);
    

    you may need to use

    licenseUsers.addAll(tmpLicenseUsers);
    

    I hope one of them fixes the problem.