StateRestore 2 removeAllStates/statesRemoveAll

StateRestore 2 removeAllStates/statesRemoveAll

bg7bg7 Posts: 134Questions: 20Answers: 0

We're migrating to DataTables 3 now that StateRestore 2 is available. When I loaded it I got an error that Copilot tracked down to what looks like what might be a naming mismatch. I asked it to summarize what it found:

We’re testing DataTables 3 + StateRestore 2.0.0 and found what looks like a button aliasing bug in StateRestore.

In datatables.net-staterestore/js/dataTables.stateRestore.mjs:
- Line 1420 defines:
DataTable.ext.buttons.removeAllStates = { ... }
- Line 1537 later assigns:
DataTable.ext.buttons.removeAllStates = DataTable.ext.buttons.statesRemoveAll;

However, statesRemoveAll does not appear to be defined in the same module.
Result: removeAllStates becomes undefined, and table config using
extend: 'removeAllStates'
fails at runtime with:
Uncaught Cannot extend unknown button type: removeAllStates

We can reproduce this from npm package datatables.net-staterestore@2.0.0
(also visible in dataTables.stateRestore.js with equivalent lines).

For what it's worth Copilot created a patch to work around what it found:

// StateRestore 2.0.0 resets removeAllStates to an undefined alias (statesRemoveAll).
// Recreate the legacy button name so existing `extend: 'removeAllStates'` configs keep working.
const dtButtons = DataTable?.ext?.buttons;
if (dtButtons && typeof dtButtons.removeAllStates === "undefined") {
  dtButtons.removeAllStates = {
    action(e, dt) {
      const states = dt.settings?.()[0]?._states;
      if (!states || typeof states.storeGet !== "function" || typeof states.remove !== "function") {
        return;
      }

      const myStates = states.storeGet().filter((state) => !state.isSharedIn);
      states.remove(myStates);
    },
    init(dt) {
      const ctx = dt.settings?.()[0];
      if (!ctx) {
        return;
      }

      if (!ctx._states && DataTable?.StateRestore) {
        new DataTable.StateRestore(dt);
      }

      const states = ctx._states;
      if (!states || typeof states.storeGet !== "function") {
        return;
      }

      dt.on("stateRestore", () => {
        this.enable(states.storeGet().length > 0);
      });

      this.enable(states.storeGet().length > 0);
    },
    text(dt) {
      return dt.i18n("stateRestore.button.statesRemoveAll", "Remove all states");
    },
  };
}

Is it possible this is a bug in the StateRestore code or am I doing something wrong?

Thanks.

Ben

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 65,991Questions: 1Answers: 10,987 Site admin

    You are quite right - my apologies. Fix committed here and I'll tag up 2.0.1 shortly (just need to fix something in DataTables core first...!).

    Allan

  • allanallan Posts: 65,991Questions: 1Answers: 10,987 Site admin
    Answer ✓

    2.0.1 now available :)

  • bg7bg7 Posts: 134Questions: 20Answers: 0

    Thanks Allan!

Sign In or Register to comment.