Clovis and StateRestore

Clovis and StateRestore

edknitteledknittel Posts: 5Questions: 3Answers: 0

I'm running into what I would consider a common problem: we have tables set up with the columns that we want in the order that we want those columns to be in but of course, over time, we need to add/remove columns. This inevitably leads to issues where a saved state doesn't display the correct columns because the column indexs have changed since the last state save.

For example, I have a table with the following columns:
1. Name
2. Age
3. Date of Birth
4. Email Address

Using colvis users can show or hide columns. Using saveState users can save this view for future reference. If for instance, the user hides Email address the saved state will store the column visibility with the column indexes as a comma separated list as a query string parameter such as:
* :vf4, or;
* :vt1,2,3

Depending on if the array of visible columns is greater than or less than the array of hidden columns.

If I add a new weight column to this table between Date of Birth and Email Address such that the column order looks like:
1. Name
2. Age
3. Date of Birth
4. Weight
5. Email Address

when the user with the saved state tries to load up this table where the 4th column index was Email Address the new 4th column index is now Weight and as such the Weight column will be hidden rather than than the Email Address column.

What I'd like to do
I'd like to maintain a JavaScript array of the columns as they are added to the table, not necessarily the position of the columns as they visibly appear in the table. So going back to my example, the visible column order would be:
1. Name
2. Age
3. Date of Birth
4. Weight
5. Email Address

While the column order is
1. Name
2. Age
3. Date of Birth
4. Email Address
5. Weight

So weight would always be an index value of 5 which wouldn't break the existing saved states for my users. You can imagine over time a table could visibly look like this:
1. Name
2. Address
3. City
4. State
5. Postal Code
6. Age
7. Date of Birth
8. Weight
9. Email Address

But the columns may be added gradually over time and the order that the columns were added could look something like this:
1. Name
2. Age
3. Date of Birth
4. Email Address
5. Weight
6. Postal Code
7. Address
8. City
9. State

And now imagine that some time later age is visibly removed from the table. The new visible columns would be
1. Name
2. Address
3. City
4. State
5. Postal Code
6. Date of Birth
7. Weight
8. Email Address

But I would maintain the column list which wouldn't break any existing saved states:
1. Name
2. Age
3. Date of Birth
4. Email Address
5. Weight
6. Postal Code
7. Address
8. City
9. State

TL;DR: is this possible? If not, can I extend the colvis functionality to support this?

Answers

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    Interesting one - thanks for flagging this up. It does sound like it could be a fairly common issue, but I don't recall hearing about it before.

    At the moment if the column count changes, then DataTables will throw the old state away since it can no longer be valid. The code for that check is here.

    What would need to be done to support what you are looking for is to store the data in a way that is independent of the column indexes. For that you could use stateSaveCallback to convert the state object DataTables creates into some other format (probably key the columns index on columns.name) and then store it in localStorage (or anywhere else).

    Then use stateLoadCallback to read it back and convert from the name based columns array into the index based array DataTables is expecting.

    What might be nice is if DataTables were to check for columns.name and use that as the key rather than the column index. I'll have a think about that as a future enhancement.

    Allan

Sign In or Register to comment.