How to retrieve data attribute of a table inside a button
How to retrieve data attribute of a table inside a button
itajackass
Posts: 165Questions: 49Answers: 3
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
I guess you would need something like this
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.
Hi, the problem is still present: inside "action: function ( e, dt, node, config ) {" how can determinate the correct counter (ID) i need to use?
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.
This should be the right option from the api:
https://datatables.net/reference/api/table().node()
You could also try this.
Just tested it; works like a charme! And no "id" needed for this!
tried now! It is perfect! thanks!