GWT CellTable columnsorting

2020-03-06 04:09发布

问题:

I am trying to sort on a column in GWT using their ListDataProvider Sorting example as a reference

http://code.google.com/webtoolkit/doc/latest/DevGuideUiCellTable.html#columnSorting

The problem is I can see my table column data is getting sorted (by using the debugger) but the table never gets refreshed. Looking at the example in the above link they do not explicitly refresh the display. Am I missing something here?

Thanks.

回答1:

My experience says sort handler (i.e. ListHandler) is sensitive to the List, which ListDataProvider has, when you are creating it.

So I recommend not to delete or set new List for ListDataProvider (of course I think it should has at least an empty (not null) list when you are creating).

Just use ListDataProvider.getList() and do your jobs on it.

For example for removing all current rows and setting new data, just call ListDataProvider.getList().clear() and then ListDataProvider.getList().addAll(yourNewData); do not call setList() or the sorting becomes not working.



回答2:

Those two examples are useless and confusing.

Using ListHandlers and DataProviders are also not too helpful when illustrating how to use Cell/Grid tables.

The examples obfuscate and hide one simple characteristic of GWT Cell/Grid tables - you merely need to replace and feed the list into the table every time the data change in anyway.

The examples' use of ListHandlers, Async handlers and DataProviders complicates a rather simple process by throwing in a whole bunch of fluffy unhelpful abstract elegance. Essentially these abstract and hideous structures themselves push and repush records into the table list. Don't pay attention to use of the Timer.

Might as well take the table list by its bull-horns and do it yourself - much simpler that way. I cannot remember exactly how I do it as I don't have my code with me.

What you need to do is to keep a buffer copy of the list of records. This is the list of records to be displayed in the table.

It does not matter whether you are doing async or otherwise - any change to the data you wish to make is to be made on that buffer copy. Your GWT-RPC should update that buffer.

When the table requests for ascending sort, your comparator/filtrator would produce an ascending copy of your buffered list to replace data in the table using setRowData. Similarly, when the table requests for descending or for specialised filtering.

Using DataProvider is useful if you wish to let it manage paging for you. It is much simpler to handle the GWT-RPC yourself. The DataProvider is supposed to handle the sorting and filtering for you by requesting the server to send it a new list that is sorted/filtered according to the wishes of the table. Why would you want to consume network traffic by letting the server manage your filtering/sorting, unless you have more than 10 pages of records.

So for a simple 50 record experimentation example, manage the buffer list yourself.