State Restore is not clearing out previous ColumnControl filters on loading saved states

State Restore is not clearing out previous ColumnControl filters on loading saved states

bg7bg7 Posts: 119Questions: 15Answers: 0

I'm using State Restore to save search states including columns using ColumnControl filters. If I load a saved state that has items selected in the column's filter list and then I load a second state with different items selected in that same filter list I end up with a union of the two lists. That said, it's only the column filter list drop-down that's incorrect. The actual table is correct and only shows the values for the items saved in the second state.

For example, this is saved state 1:

If I then clear out the search and load saved state 2:

However, if I load state 1 and then don't clear the search and load state two I get a union of the two:

But as I mentioned, the actual rows displayed are correct and match state 2. It's only the column filter check marks that are out of sync.

I asked Coplilot to track this down and it did. I don't know how good the code is but it did fix the issue. Here are the changes it suggested:

Here's the updated code:

CheckList.prototype.values = function (values) {
    var buttons = this._s.buttons;
    if (values === undefined) {
        var out = [];
        for (var j = 0; j < buttons.length; j++) {
            if (buttons[j].active()) {
                out.push(buttons[j].value());
            }
        }
        return out;
    }

    // Fix: replace selection set (do not merge with previous active values)
    this.selectNone();
    values = values || [];
    for (var i = 0; i < values.length; i++) {
        var btn = this.button(values[i]);
        if (btn) {
            btn.active(true);
        }
    }
    var count = this.values().length;
    this._dom.selectNoneCount.innerHTML = count ? '(' + count + ')' : '';

    return this;
};

And the second block:

    dt.on('stateLoaded', function (e, s, state) {
        var values = getState(_this.idxOriginal(), state) || [];
        checkList.values(values); // always apply, including empty => clear
        applySearch(values);
    });

Do these changes make sense? Is this ok or is there a better way to fix this?

Thanks.

Ben

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 65,666Questions: 1Answers: 10,921 Site admin

    Hi Ben,

    What version of ColumnControl are you using here? I haven't had a chance to try and reproduce it with the latest dev builds yet, but I'll try to do so soon.

    Allan

  • bg7bg7 Posts: 119Questions: 15Answers: 0

    Allan,

    Here's the full list:

    JSZip 3.10.1, DataTables 2.3.7, Editor 2.5.2, Buttons 3.2.6, Column visibility 3.2.6, HTML5 export 3.2.6, Print view 3.2.6, ColumnControl 1.2.1, DateTime 1.6.3, FixedColumns 5.0.5, KeyTable 2.12.2, RowGroup 1.6.0, RowReorder 1.5.1, Scroller 2.4.3, Select 3.1.3, StateRestore 1.4.3

    The StateRestore however is actually the nightly version you gave me from this ticket:

    https://datatables.net/forums/discussion/81707/state-restore-looping-through-states-and-filter-data

    Thanks.

    Ben

  • bg7bg7 Posts: 119Questions: 15Answers: 0

    Allan,

    Apologies - I should have thrown together an example. I can see it happening here.

    https://live.datatables.net/hirizobu/1/edit

    I saved the first two records to State 1, then the fourth and fifth records to State 2. Then I loaded State 1, then loaded State two and I get the union of the two but in the background you can see there are just the first two records shown in the table:

    Same as I'm seeing here.

    Thanks.

    Ben

  • allanallan Posts: 65,666Questions: 1Answers: 10,921 Site admin
    Answer ✓

    I've committed this change to address the issue. The nightly has this change and your example works correctly with it now.

    Allan

  • bg7bg7 Posts: 119Questions: 15Answers: 0

    Allan,

    That works great. Thanks!

    Ben

Sign In or Register to comment.