GWT Sorting a cell table, probably just something i didn't saw

不想你离开。 提交于 2019-11-30 21:59:57

问题


I've been struggling for the last couple of hour trying to sort a GWT CellTable. It's really a stupid problem because it's been done here http://gwt.google.com/samples/Showcase/Showcase.html#!CwCellTable

But I do not understand what I'm missing in the exemple ...

Here is my code I use to create the column:

    Column<RemoteCommand, String> nbProducts = new Column<RemoteCommand, String>(
                new TextCell()) {
              @Override
              public String getValue(RemoteCommand object) {
                return object.getNumberProduct();
              }
            };
            nbProducts.setSortable(true);
            sortHandler.setComparator(nbProducts, new Comparator<RemoteCommand>() {
              public int compare(RemoteCommand o1, RemoteCommand o2) {
cellTable.redraw();
                  return o1.getCommandSize().compareTo(o2.getCommandSize());
                   // System.out.println(Integer.parseInt(o1.getCommandSize() ) -  Integer.parseInt(o2.getCommandSize()));
                    //  return  Integer.parseInt(o1.getCommandSize() ) -  Integer.parseInt(o2.getCommandSize());
              }
            });

And here is the declaration of the table itself:

// Add a selection model so we can select cells.
final SelectionModel<RemoteCommand> selectionModel = new MultiSelectionModel<RemoteCommand>(
            RemoteCommand.KEY_PROVIDER);
cellTable.setSelectionModel(selectionModel,
DefaultSelectionEventManager.<RemoteCommand> createCheckboxManager());


// Attach a column sort handler to the ListDataProvider to sort the list.
    ListHandler<RemoteCommand> sortHandler = new ListHandler<RemoteCommand>(values);
cellTable.addColumnSortHandler(sortHandler);

// Initialize the columns.
initTableColumns(selectionModel, sortHandler);

cellTable.setRowData(values);

help is requierd :)


回答1:


i guess You've already found the solution, but just to keep it here:

First, create your dataProvider with some known List. Than feed sortHandler with same List; and use the list to update data. Celltable should be set as dataDisplay of the dataProvider:

List myDataList = new ArrayList();
ListDataProvider dataProvider = new ListDataProvider(KEY_PROVIDER);
dataProvider.setList(myDataList);
ListHandler sortHandler = new ListHandler(dataProvider.getList);
//tie provider and table
dataProvider.addDataDisplay(cellTable);

//when you need to update dataprovider
//first do some myDataList cleanup to remove old values
myListData.addAll(values);
//update data displays
dataProvider.refresh();

Consider, you have to always use one and the same List object.



来源:https://stackoverflow.com/questions/6652182/gwt-sorting-a-cell-table-probably-just-something-i-didnt-saw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!