How to retrieve data attribute of a table inside a button

How to retrieve data attribute of a table inside a button

itajackassitajackass Posts: 162Questions: 48Answers: 3
edited November 2023 in Free community support

Hi, i've several html tables inside a page:they are generated in a loop based on some parameters so i don't know how many tables will be generated on dom load.

Then they are initializated by class: .tabellaRisultati

For each table i've a button that i use for my custom function, where i need to pass the attribure "data-meta" regarding each table. But i don't know how to retrieve it. Any suggest?

html:

<table class="display table table-bordered table-striped tabellaRisultati" style="width:100%" data-meta="some_custom_value_different_for_each_table">
   ...
 </table>

javascript:

 $('.tabellaRisultati').DataTable( {
    dom: 'Bfrtip',
            ....
           buttons: [
        {
            extend: 'collection',
                            text: 'Menu',
            buttons: [
                {
                    text: 'Do some stuff',
                    action: function ( e, dt, node, config ) {
                                        var meta = <----- here i need to retrieve  data-meta attribute of relative table
                        my_custom_function(meta);
                    }
                },
                                    ....
           }]
      });

This question has accepted answers - jump to:

Answers

  • rf1234rf1234 Posts: 2,980Questions: 87Answers: 421
    edited November 2023

    Hi, i've several html tables inside a page:they are generated in a loop based on some parameters so i don't know how many tables will be generated on dom load.
    Then they are initializated by class: .tabellaRisultati

    I guess you would need something like this

    var meta = $( "#individualID" ).data( "meta" );
    
    

    You would need to create an individual ID for each table in the loop that generates the tables. The id could be a simple counter that you increase by one for each table generated.

  • itajackassitajackass Posts: 162Questions: 48Answers: 3

    Hi, the problem is still present: inside "action: function ( e, dt, node, config ) {" how can determinate the correct counter (ID) i need to use?

  • rf1234rf1234 Posts: 2,980Questions: 87Answers: 421
    edited November 2023

    There is a lot on this on SO. In terms of HTML you generate N tables. Those N tables should have individual IDs that you can identify.

    Then all you need to do is to retrieve the individual ID of the table you are currently dealing with.

    tableID = $(this).closest('table').attr('id');

    I would also check the content of the parameters passed into the "action" function of the button. The ID might "hide" there somewhere as well.

  • rf1234rf1234 Posts: 2,980Questions: 87Answers: 421
    Answer ✓

    This should be the right option from the api:
    https://datatables.net/reference/api/table().node()

    You could also try this.

    var meta = $( dt.table().node() ).data( "meta" );
    
  • rf1234rf1234 Posts: 2,980Questions: 87Answers: 421
    Answer ✓

    Just tested it; works like a charme! And no "id" needed for this!

    var meta = $( dt.table().node() ).data( "meta" );
    
  • itajackassitajackass Posts: 162Questions: 48Answers: 3

    tried now! It is perfect! thanks!

Sign In or Register to comment.