6

I am populating a DataGrid in GWT on a button click. It is working fine but if I try to re-populate it, the data gets appended to the existing table.

I am using UI Binder/GWT 2.5 to create DataGrid.

I have already tried this:

    // My list which gets updated with the response of RPC. Initially it is empty.
    List<List<String>> tableRowData = new ArrayList<List<String>>();
    grid.setRowCount(0); // Clears all data.
    grid.setRowCount(tableRowData.size(), true); // Set the size of the new data.
    grid.setRowData(0, tableRowData); // Set the new data.

    Populate tableRowData...

    Populate data grid // works perfect 

Also since GWT 2.1.1, there is a new method setRowData(List)

Each element of the list tableRowData is again a list of strings. Is it even possible without using ListDataProvider. For first time it works perfect, though.
Can anyone please point out what I am missing.

Thanks

EMM
  • 1,812
  • 8
  • 36
  • 58
  • 1
    http://stackoverflow.com/questions/9380202/gwt-celltable-columnsorting/9389172#9389172. I did not use dataprovider. Simply grabbed hold of the list for the celltable and manipulated it. – Blessed Geek Oct 31 '12 at 05:03
  • @BlessedGeek - Good post. Any chance you can share your code so that people don't have to "take the table list by its bull-horns" themselves? – logan Oct 31 '12 at 05:30
  • I left my code with my old project and I would have to meditate a lil' bit to get it back. But what I did was embarrassingly simple. I think I did use setRowData. What is missing here is probably setRowCount-exact to zero, every time you need to replace the list, and then setRowCount-exact to the size of the new list. – Blessed Geek Oct 31 '12 at 08:11

1 Answers1

2

The best way to manage Cell widgets is using a DataProvider. GWT's showcase has some elaborate examples, but you should be fine with a simple ListDataProvider as long as your list is guaranteed to be small.

ListDataProvider<List<String>> dataProvider = new ListDataProvider<List<String>>();
dataProvider.addDisplay(dataGrid);

List<List<String>> newData = ...;
dataProvider.getList().clear();
dataProvider.getList().addAll(newData);

For more information, check out the new GWT documentation about data providers. Thanks to this post for tweaks.

Community
  • 1
  • 1
logan
  • 3,416
  • 1
  • 33
  • 45
  • Thanks for the reply logan. But I am not using ListDataProvider. Is it possible to achieve what I need without using ListDataProvider? – EMM Oct 31 '12 at 04:13
  • It is possible, but you would basically just be rebuilding ListDataProvider. Just look through the source of ListDataProvider – logan Oct 31 '12 at 04:22
  • @mohit after looking through the source for a bit, I can't find the error in your ways. I had a similar problem recently (a celllist didn't remove empty values), and the way I fixed it was by using a ListDataProvider – logan Oct 31 '12 at 04:31
  • 1
    See http://stackoverflow.com/a/6443043/768894 , they are doing it without any DataProvider. – EMM Oct 31 '12 at 04:33
  • 1
    @mohit - It is entirely possible to not use a DataProvider. Internally they call just call `grid.setRowData`... it's just code that's tested to work nicely with all the SelectionEvents, ScheduleFinally and pendingState behind the scenes. What's the downside to using a ListDataProvider? – logan Oct 31 '12 at 05:24
  • "Internally they call just call grid.setRowData" then why it is not working when I am calling it explicitly. At the begining of my method I am creating an empty list and calling setRowData() with this blank list later in the method I populate this list and again call setRowData which works fine. – EMM Oct 31 '12 at 09:24
  • I do recall now - I had simply studied ListDataProvider and created a simple list manager/updater. – Blessed Geek Nov 01 '12 at 05:49