Get variable name assigned to a datatable

Get variable name assigned to a datatable

jvcunhajvcunha Posts: 81Questions: 10Answers: 1
edited September 2019 in Free community support

It's possible get variable name assigned to a datatable ?
Example:
var tableXPTO1 = $('#myTableOne').DataTable({});
var tableXPTO2 = $('#myTableTwo').DataTable({});

$('body').on('init.dt', function (settings) {
identify variable name assigned to a datatable inside 'settings' (tableXPTO1 or tableXPTO2 ?)
}

Best regards,

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,160Questions: 26Answers: 4,921

    You wouldn't be able to get the variable name. That is a Javascript object which Datatables has no knowledge of.You could do something like this to get the table ID. Or simple just get an API instance to work with the Datatable.
    http://live.datatables.net/marizosi/1/edit

    If this doesn't help then please explain what you want to do in the init event.

    Kevin

  • jvcunhajvcunha Posts: 81Questions: 10Answers: 1
    edited September 2019

    Hi Kevin,
    I would like to set some common events only once for each table. Example:

    $ ('body'). on ('init.dt', function (settings) {
        variablename.on ('select', function (e, dt, type, indexes) {
             code ....
        });
    
        variablename.on ('deselect', function (e, dt, type, indexes) {
             code ....
        });
    }
    

    No need to use:

        $ ('# tableId'). on ('deselect', function (e, dt, type, indexes) {
             code ....
        });
    

    The table variable is already in memory

  • kthorngrenkthorngren Posts: 21,160Questions: 26Answers: 4,921
    Answer ✓

    Like I said the table variables are Javascript objects. Datatables has no knowledge of the variable name used. You can do something like this example:

    http://live.datatables.net/marizosi/2/edit

    It cascades the events and initializes Datatables. Within the events it uses jQuery to get the table being used via $(this).

    Kevin

  • jvcunhajvcunha Posts: 81Questions: 10Answers: 1

    Thanks Kevin, best regards

This discussion has been closed.