Alex
Alex

Reputation: 785

how to add data-attributes to the rows of a JQuery DataTable

I would like to had (multiple) data-attributes to the generated rows of my JQuery DataTable so that I can store data in it.

What I want to achieve is to make DataTables generate something like this:

<tr data-foo="bar">
  <td></td>
</tr>

Is it possible? How to do it? Thanks.

Upvotes: 10

Views: 21431

Answers (1)

Alex
Alex

Reputation: 785

So I found out, what I needed was to set my attributes dynamically, it's possible using the data parameter of the function, it contains the model corresponding to the row.

    var table = $('#incidentTable').DataTable({
        createdRow: function (row, data, dataIndex) {
            $(row).attr('data-id', data.Id);
            $(row).attr('data-ownerid', data.OwnerId);
        }
    });

Upvotes: 35

Related Questions