Get DataTable object from cell click event

Get DataTable object from cell click event

mctriviamctrivia Posts: 1Questions: 0Answers: 0

I have multiple tables on the same page. I would like to have an onclick event for multiple cells based on class. This part is easy. What I can't seem to figure out is how to get the DataTable object associated with the clicked cell.

Any ideas.

Replies

  • kthorngrenkthorngren Posts: 13,584Questions: 25Answers: 3,197

    This example uses cell().data() to get the data of the clicked cell:
    http://live.datatables.net/retoniyi/1/edit

    If you still need help please provide a simple test case showing what you are trying to do.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • haraldharald Posts: 8Questions: 2Answers: 0

    I am using the above cell().data(). It works perfect!

      $('#example').on('click', 'td', function () {
          var table = $('#example').DataTable();
          var data = table.cell( this ).data();
          console.log(data);
        })
    

    Is there a possibility to click on a cell and getting instead the data from a cell in the same row, several colums left or right?
    Thanks for any help!

  • colincolin Posts: 12,453Questions: 0Answers: 2,110

    Yep, you can just get the entire row data with

    ```
    table.row(this).data()
    ````

    as that also accepts a td,

    Colin

  • haraldharald Posts: 8Questions: 2Answers: 0

    Hi Colin,
    thanks for your quick reply! I allready tried table.row(this).data(); But it returns undefined. table.cell( this ).data() returns the correct value. In the last column I have an icon. I am looking for a way, to click on the last cell in the row (icon) and get the data from the first cell of the row for further calculations.

    This is what I tried:

    $('#example').on('click', 'td', function () {
          var table = $('#example').DataTable();
          var cell = table.cell( this ).data();          // returns correct cell data
          console.log(cell);
          var row = table.row(this).data();           // returns undefined
          console.log(row);
        })
    

    Is there no way to get a specific cell of the row by clicking on another cell in the row? Instead of this cell, the cell two colums left?

    Thanks Harald

  • kthorngrenkthorngren Posts: 13,584Questions: 25Answers: 3,197

    The row-selector docs state that using the td for row selection was put in Datatables version 1.10.11. Are you using an earlier version?

    Your code works in this test case with the latest version:
    http://live.datatables.net/zecacima/1/edit

    You can use jQuery closest() to get the closest tr, like this:

    $('#example').on('click', 'td', function () {
          var table = $('#example').DataTable();
          var cell = table.cell( this ).data();          // returns correct cell data
          console.log(cell);
          var tr = $(this).closest('tr');
          var row = table.row( tr ).data();           // returns undefined
          console.log(row);
        })
    

    Kevin

  • haraldharald Posts: 8Questions: 2Answers: 0

    Hi Kevin,
    thanks for your reply. Starting to update to latest version and give it another try!

Sign In or Register to comment.