0

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 :)

MLavoie
  • 9,671
  • 41
  • 36
  • 56
florent
  • 29
  • 1
  • 3
  • 12

1 Answers1

2

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.

sab
  • 651
  • 8
  • 17
  • Nice work, this is from my point of view exactly how it's done - just one thing: the refresh is not necessary because it's done in the update when you modify the list. In my experience I had to use the dataProvider.getList()-Method instead of using myDataList - otherwise there were no updates fired. – Chris Dec 05 '11 at 14:27
  • Chirs, you'r completely right! Using dataProvider.getList() is better! Actually, modifying myDataList works for me, but then i need refresh(), and, most important(!) - updating myDataList causes incorrect pagination behavior (if there was no data for table when you create it - paging remains invalid until some sorting done, for example). Thank you. – sab Dec 07 '11 at 08:48