maintaining sort state in a table

242 views
Skip to first unread message

David Manz

unread,
Aug 28, 2015, 10:02:08 AM8/28/15
to Google Visualization API
I have a table with multiple sortable columns and dynamic data.  When the data in the table is updated, I need to redraw the table, and I would like to maintain the sort state across the redraw.  However, it appears to me that there is no way to do this without actually modifying the order of the underlying data--mixing the model and the view.  Table and DataView both allow me to get the entire sort state of the table, but when I want to restore it, I am only allowed to specify a single column to sort by, which is insufficient.  DataTable allows a full specification of the sort state.  Is that how I am supposed to maintain sort state?

David

Daniel LaLiberte

unread,
Aug 28, 2015, 10:23:52 AM8/28/15
to Google Visualization API
At this point, yes, you'll have to manage the sorting yourself, which would probably be easiest using getSortedRows() and constructing a DataView with those rows using setRows().  

The Table chart only knows about one column for sorting so it can indicate that column in the header; it also supports sorting by any columns interactively, of course.  

If you want to keep the same ordering after the user does several sort operations, then you'll want to get the 'sort' events as they occur, do the sort yourself as you save the columns in an array that you can later use to sort another DataTable.  One problem is deciding how many sort steps to remember, if not all steps.  Repeatedly sorting the same column, which reverses the previous sort, could be abbreviated to the last one.  But sorting subsequent columns depends on the order determined by all the previous sorts, potentially.

We could allow the sort column option (or sortColumns) to be an array, in which case, it would sort of each of those columns in order.    And then we could also maintain the order of the columns sorted by the user, and return that array to you so you could apply the same ordering to another DataTable.   This would be doing all the work that you will have to do yourself currently, so if you end up doing that, post your code, and maybe we will be able to use it.


On Fri, Aug 28, 2015 at 10:02 AM, David Manz <supe...@gmail.com> wrote:
I have a table with multiple sortable columns and dynamic data.  When the data in the table is updated, I need to redraw the table, and I would like to maintain the sort state across the redraw.  However, it appears to me that there is no way to do this without actually modifying the order of the underlying data--mixing the model and the view.  Table and DataView both allow me to get the entire sort state of the table, but when I want to restore it, I am only allowed to specify a single column to sort by, which is insufficient.  DataTable allows a full specification of the sort state.  Is that how I am supposed to maintain sort state?

David

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-visualization-api/fc5f399b-7bb3-4203-82d9-9ef837c4cd20%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
dlaliberte@Google.com   5CC, Cambridge MA
daniel.laliberte@GMail.com 9 Juniper Ridge Road, Acton MA

David Manz

unread,
Aug 28, 2015, 10:21:03 PM8/28/15
to google-visua...@googlegroups.com
Daniel,

Thank you for your guidance--it was very helpful.

I considered your idea of keeping the history of sorts and then replaying them upon a redraw, but that's a bit complicated and it is conceivable that the order of rows could change in response to events other than sorting--the Netflix DVD queue would be an example of that,--and then you'd still have a problem.

What I ended up doing was to persist the sortIndexes returned by getSortInfo.  The one tricky part is that redrawing the table resets getSortInfo's frame of reference, so I have to keep around an alias table to translate between the current frame of reference and the original one.  I also use getSortInfo's sortColumn and sortAscending in the redraw so as to preserve the visual indicator of the most recent column sorted.  As long as the sort algorithm is a stable one, this doesn't cause any problems.

Here's my code with a small test case.


To see the original problem, just comment out the tableDataView.setRows line, and then on the table sort by row number descending, sort by parity, and then click 'redraw'.

If you do make a change to the google visualizations api, I think it would make sense for the draw method to take the entire state returned by getSortInfo as an option instead of just the sortColumn and sortAscending attributes.

David



--
You received this message because you are subscribed to a topic in the Google Groups "Google Visualization API" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-visualization-api/S_6dZOlNph4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-visualizati...@googlegroups.com.

To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.

David Manz

unread,
Sep 24, 2015, 12:32:41 AM9/24/15
to google-visua...@googlegroups.com
Here's an improved version:

http://jsfiddle.net/p96x6pxz/2/

It maintains entire sort state (not just the last sort) and selection state across redraws.

David

Daniel LaLiberte

unread,
Sep 24, 2015, 8:53:06 AM9/24/15
to Google Visualization API
Thanks for posting your code, and your update.  Preserving and restoring the entire row order is certainly simpler and faster than what I was suggesting.  

But one potential limitation comes up if the data can be changed, including the adding or removing of rows.  I thought that was what you wanted to support.  In that situation, it may not be correct to apply the previous row order to the current table, whereas reapplying the sorting of a sequence of columns would always work, though it would certainly be more time consuming than merely setting the row order.

An option, or at least a method, to specify the entire sort info would be possible.  I'll see about adding that.


For more options, visit https://groups.google.com/d/optout.

David Manz

unread,
Sep 24, 2015, 9:22:56 AM9/24/15
to google-visua...@googlegroups.com
I had meant that there could be modifications to the order that are not the result of sorting, but now that I think about that, they wouldn't be reflected in the sortIndexes unless there were a subsequent sort, so you're right, sortIndexes isn't the definitive source for row ordering.  In any case, this solution works for me right now.

Thanks again for the guidance on this problem.

It would be great if the API supported taking the entire sortInfo as returned by getSortInfo in the configuration options, and perhaps the selection state returned by getSelection too.

David

Reply all
Reply to author
Forward
0 new messages