Understanding the button()trigger() index reference

Understanding the button()trigger() index reference

glimpsed_chaosglimpsed_chaos Posts: 140Questions: 30Answers: 4

Link to test case: https://live.datatables.net/pedigiho/1/edit
Error messages shown: "Cannot read properties of undefined (reading 'node')"
Description of problem:

Referring to this: https://datatables.net/reference/api/button().trigger() it shows but gives no context regarding index '2-1' or how it's defined.

var table = $('#myTable').DataTable();
 
table.button( '2-1' ).trigger();

In my linked test case, I have a stateRestore button which itself works fine. With a different button set to invoke this trigger I get "Cannot read properties of undefined (reading 'node')" in the browser.

I'm using index ('0-0') assuming this means the first table and it's first button?? Of course I have tried other combinations trying to understand. And either nothing fires or I get the reading 'node' error.

$("#testClick").on("click", function (e) {
       table.button('0-0').trigger();
});

Any help is appreciated.

This question has accepted answers - jump to:

Answers

  • glimpsed_chaosglimpsed_chaos Posts: 140Questions: 30Answers: 4

    Some more digging around and found more info here but using the select for name, or index (not a button subgroup) does not work as expected.

    I can get a "Test" button to trigger by index (1) but cannot get the stateRestore to trigger which is index (0) or by using it's name value.

  • kthorngrenkthorngren Posts: 21,325Questions: 26Answers: 4,949
    edited August 2023

    I updated your test case to show that your selectors for the Reset button event handers are working. It uses button().text() to change the text.
    https://live.datatables.net/pedigiho/2/edit

    The button().trigger() docs state this:

    Please note that some buttons types might not be able to be triggered programmatically.

    Maybe your StateRestore button falls in this category. @allan will need to confirm.

    This section of the button-selector docs explains the '2-1' format. Its used with a button collection like this example.

    Kevin

  • glimpsed_chaosglimpsed_chaos Posts: 140Questions: 30Answers: 4

    @kthorngren Maybe I am barking up the wrong tree...if stateRestore is not capable of being triggered via button().trigger().

    What I am trying to do here is reset the "order": [] to no order.

    Initially the table is built with no order, I am currently ordering this naturally in my SQL query to handle NumericAlpaha as the primary column (ex: 1, 10, 10b, 10c). This way the order is presented properly. But I do allows users to sort as they would while viewing the data.

    Once they sort, they cannot get the order back to the natural order by clicking the heading of that primary column. This is because the plugin for natural sorting fails after a certain point (confirmed here by@colin)

    But if I can reset the order to no order, I can then force refresh my data.

    FYI - I am not using Ajax to get my data, it comes in via websockets (SignalR) every 30 seconds.

  • glimpsed_chaosglimpsed_chaos Posts: 140Questions: 30Answers: 4
    Answer ✓

    I was trying to create a predefined stateRestore with no order. Then when clicking the Reset button load that "state" and refresh my data. But it does not appear to respond via button.trigger.

    It looks like I needed another plug-in and not use stateRestore at all. The order.neutral.js plug-in will "Change ordering of the table to its data load order".

    This plug-in is under the API plug-ins not the Sorting Plug-ins.

    This works perfectly and I can trigger with a button action.

            buttons: [
                {
                    text: 'Reset Order',
                    action: function (e, dt, node, config) {
                        dt.order.neutral().draw();
                    }
                }
            ],
    

    This can be updated to answered so no one spends time reviewing.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Thanks for the update. Good to hear you've got it working.

    Allan

Sign In or Register to comment.