1

I use datatable

A typical row is

<tr>
      <td data-id="1">Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
</tr>

I created an example

http://jsfiddle.net/hb7v1mgy/

Init of the table

 var table = $('#example').DataTable({
    responsive: true
  });

When I click on a row, I would like to get data attritube id, actually I get column value (Tiger, System...)

$('#example tbody').on('click', 'tr', function() {
    //get only value of td... not data attribute
    var data = table.row(this).data();
});
4
  • Check out the datatables.net-select plugin (which is developed by dt authors) for this purpose. npmjs.com/package/datatables.net-select Commented Jun 13, 2018 at 13:18
  • document is not very speaking datatables.net/reference/event/select#Examples Commented Jun 13, 2018 at 14:34
  • I had my troubles with the documentation as well, but console.logging the selected row's data might help you get started: table.on('select', function (e, dt, type, indexes) { console.log(table.rows(indexes).data()) }); Commented Jun 13, 2018 at 14:45
  • Just remember that select event only gives you the last selected row(s). You will have to calculate the list of selected rows by yourself in case of multiselect. Commented Jun 13, 2018 at 14:47

1 Answer 1

4

In your click method

var tr = $(this).closest('tr');
var id = tr.children("td:eq(0)").attr('data-id')

you don't need select plugin...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.