GWT - CellTable 可排序列不会响应点击

GWT - CellTable sortable column won't respond to clicks

我有一个需要按字母顺序排序的 GWT Celltable 列。我遵循了 official documentation and other questions posted here,但我无法使其正常工作。

我希望该列同时进行升序和降序排序。目前,克拉符号显示在 header 列旁边,但单击它时没有任何反应。浏览器控制台中也没有抛出任何错误。

我做错了什么?我的混淆代码 -

public class MyClass extends Composite {
    //other fields
    private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
    @UiField CellTable<MyObject> myTable = new CellTable<MyObject>();
    final ListDataProvider<MyObject> myDataProvider = new ListDataProvider<MyObject>();

    @UiConstructor
    public MyClass(...) {
        initWidget(uiBinder.createAndBindUi(this));
        // other initialization
        buildMyTable();
    }

    buildMyTable() {
        myDataProvider.addDataDisplay(myTable);

        Column<MyObject, String> colA = new Column<MyObject, String>(new TextCell()) {

            @Override
            public String getValue(MyObject object) {
                return object.getName();
            }
        };

        Column<MyObject, String> colB = new Column<MyObject, String>(new TextCell()) {

            @Override
            public String getValue(MyObject object) {
                return object.getAddress();
            }
        };

        // created other columns

        colA.setSortable(true);

        myTable.addColumn(colA, "COL A");
        myTable.addColumn(colB, "COL B");
        // added other columns to the table

        ListHandler<MyObject> columnSortHandler = new ListHandler<>(myDataProvider.getList());
        columnSortHandler.setComparator(colA, new Comparator<MyObject>() {

            @Override
            public int compare(MyObject o1, MyObject o2) {
                if (o1 == o2) {
                    return 0;
                }

                if (o1 != null) {
                    return (o2 != null) ? o1.getName.compareTo(o2.getName) : 1;
                }
                return -1;
            }
        });

        myTable.addColumnSortHandler(columnSortHandler);
        myTable.getColumnSortList().push(colA);
        ColumnSortEvent.fire(myTable, myTable.getColumnSortList());
    }
}

全都是关于如何向 CellTable 添加数据。例如,您可以

myDataProvider.setList(rowData);

myTable.setRowData(rowData);

但是以上两种方法都不允许对数据进行排序。那是因为,您已经用空列表定义了 ListHandler。而且它不会注意到列表发生了变化,所以不会进行排序。

参见ListDataProvider documentation

Modifications (inserts, removes, sets, etc.) to the list returned by getList() will be reflected in the model. However, mutations to the items contained within the list will NOT be reflected in the model. You must call List.set(int, Object) to update the item within the list and push the change to the display, or call refresh() to push all rows to the displays. List.set(int, Object) performs better because it allows the data provider to push only those rows which have changed, and usually allows the display to re-render only a subset of the rows.

所以,您应该先 getList() 然后像这样更改它:

myDataProvider.getList().clear();
myDataProvider.getList().addAll(rowData);