Search code examples
javagwtgwt2gwt-celltable

Multiple ColumnSorting with AsyncDataProvider


I have sortable columns A and B . When user click on column A to sort , my result datas should sort via column A type and column B will show relative datas.When user was clicked on column B to sort , my result datas should sort via column B type and column A will show relative datas. I followed column sorting as GWT Column Sort example by using GWT DataGrid with AsyncDataProvider. This example show single column sorting and I would like to use one or more column sorting but I don't know how to determine
which column was fired column sort event ?

At my RPC method ,

    AsyncHandler handler = new AsyncHandler(myGridTable);
    myGridTable.addColumnSortHandler(handler);
    myGridTable.getColumnSortList().push(A_column);
    myGridTable.getColumnSortList().push(B_column);

    AsyncDataProvider<AdminModel> provider = new AsyncDataProvider<AdminModel>() {
        protected void onRangeChanged(final HasData<AdminModel> display) {
            final int start = display.getVisibleRange().getStart();
            int length = display.getVisibleRange().getLength();

            AsyncCallback<String> callback = new AsyncCallback<String> {
                public void onFailure(final Throwable caught) {
                    Window.alert(caught.getMessage());
                }
                public void onSuccess(final String result) {
                    ........
                    updateRowData(start, adminList);
                }
            };
            final ColumnSortList sortList = myGridTable.getColumnSortList();
            // Here to determine which column sort

            // send request to server
        }
    };

    provider.addDataDisplay(view.getDataGridResults());

Solution

  • See here for details.

    Basically view.getDataGridResults().getColumnSortList().get(0).getColumn() will return the column the user clicked on.

    If you want to get also the other columns (for example for multiple sorting) you have to check also the indices > 0 (i.e. get(1).getColumn())

    Once you have the column you can check the direction by calling view.getDataGridResults().getColumnSortList().get(0).getColumn().isAscending()