Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 891
Alex Avatar asked Dec 27 '17 15:12

Alex


1 Answers

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);
        }
    });
like image 172
Alex Avatar answered Oct 01 '22 22:10

Alex