Preserving sort

Hi, two part (related) question:

(1) I’m trying to figure out how to preserve sort.

That is, given a datable with a number of rows, that have been sorted, if I reload that datatable with new data (table.parse(data)), then I seem to lose the sort order.

I was expecting that there would be some way to preserve sort order, even if the data changes. If not, Is there some way to capture the current sort order and then reapply it after the data changes?

(2) Updating a table, preserving current order of first column.

Given a datable consisting of 3 columns: stock, price, quantity, in which the stock names always stay the same, but the price and quantity columns change, all at the same time.

I’d like to be able to sort the table by any of those columns, and then update the information for the 2nd and 3rd columns, while preserving their row order, based on the order of the first column.

In other words, the cells in columns 2 and 3 change their contents, and the sort marker is removed from the header (because it might not make any sense with the new data), but the order of the rows stays the same, based on the current order of the rows in the first column.

table.parse(data) seems to blow away the previous table, and also the current sort.

I could potentially go through each row, copy the data across cell, by cell, but my table is huge (many rows and columns), and updates very frequently.

thanks

David

Hey,

I recently had the same question as your first one. I don’t know if this is the preferred way, but here is how I solved it.

To get the current state of sorting, you can use DataTable.getState(), which returns an object with a sort property. The sort property itself contains id and dir which are the column and the direction of the current sorting state.

// After you loaded the data
state = table.getState();
table.sort(state.sort.id, state.sort.dir);

(1)
the order of already existing data will be preserved, but newly added data will not be sorted. You can apply sort after data loading, as it was described in the above post.

(2)
If you are using .pase API without clearAll call, then old data must be preserved in grid and updated with values from new dataset.

Check the next snippet.

http://webix.com/snippet/b137e194

You can sort data by any column and click on Parse button after that. Data in grid will be updated without affecting the sort order.

(1) @Marius, Maksim, thanks, I solved it like this:

step one, add this to the table:

on: { “onAfterSort” : function (by, dir, as, state) {
this.$lastSort = state;
}},

step two:

table.parse(data);
var state = table.$lastSort;
if (state)
table.data.sort(state);

(2) @Maksim, thanks for the snippet, which is exactly what I’m trying to do, but it’s not working for me.

Without clearall, the table.parse(data) function is just appending the new data to the end of the table.

At first I thought it could be because my rows did not have an id field (and were thus being assigned one randomly by webix I think), however even after fixing that, and assigning fixed id’s to each row, the new data was just appended to the table. Additionally, the table was somewhat unstable at that point, I’m guessing it didn’t like having multiple rows with the same id’s.

This is a fairly large table, 70 rows x 341 columns, so that might also be part of the problem. Perhaps I should try it out with a smaller table and see what happens.

David

function is just appending the new data to the end of the table.

Check that each data object has “id” property. Parse command updates record with equal id or add new ones if there is no record with matched id.

If problem still occurs, please share the problematic dataset ( original set and value used in the parse command )

Problem still persists.
Just to be completely clear, I’m setting the id’s explicitly before parse, so there is no doubt that the rows have the same set of ids.

Please share a snippet or a demo link.