Datatable with checkbox, how to get IDs ?

Datatable with checkbox, how to get IDs ?

DalemanDaleman Posts: 15Questions: 4Answers: 0

I have this datatable, http://live.datatables.net/baquqore/1/edit. Each row has unique ID.
How do I get ID if I click the checkbox ? Whether single, multiple selection or all rows.

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 2,749Questions: 0Answers: 480

    Hi @Daleman ,

    Your ID is just the value in the first column, but because you're over-writing it with a checkbox, it's no longer available. The best bet is to use another hidden column for that ID, as in this example here.

    Cheers,

    Colin

  • DalemanDaleman Posts: 15Questions: 4Answers: 0

    so, how do I get sequential ID when I click checkbox ?
    Even, when I separate ID and checkbox column, It doesn't work.

  • rf1234rf1234 Posts: 700Questions: 38Answers: 109
    edited November 8 Answer ✓

    In your context the checkbox has the only function to select rows. Hence you can use this:

    yourTable.rows({selected: true}).ids();
    

    https://datatables.net/reference/api/rows().ids()

    Here is more:
    https://datatables.net/forums/discussion/30848

  • rf1234rf1234 Posts: 700Questions: 38Answers: 109
    edited November 8 Answer ✓

    If you want the ids in an array use this:

    var idArray = yourTable.rows( { selected: true } ).ids().toArray();
    
    

    You could also use an event to get the id's whenever something is being selected:

    yourTable.on('select', function () {
        var idArray = yourTable.rows( { selected: true } ).ids().toArray();
        .... do something with the array ...
    })
    
  • colincolin Posts: 2,749Questions: 0Answers: 480
    Answer ✓

    Did you look at the example in my link - it was displaying the data in the console. Here it is again, this time I'm explicitly displaying that ID column in the array:

    http://live.datatables.net/baquqore/6/edit

  • DalemanDaleman Posts: 15Questions: 4Answers: 0

    thank's to both of you....

Sign In or Register to comment.