Retrieving Row Values Using Checkboxes

Retrieving Row Values Using Checkboxes

miyatakamiyataka Posts: 10Questions: 3Answers: 0

It's so simple, but I couldn't find it. sorry.
I'm using checkboxes.
For example, I want to get the age "15" when "TOM"s checkbox is pressed.
I want to get by the column name "AGE".
I think it will probably be written something like this

table.checkboxes.selected().colomun('AGE').value

please tell me the correct way to write

$(document).ready( function () {

let option = {
data:[
{ NAME: "TOM" , AGE: "15" },
{ NAME: "JIM" , AGE: "45" },
{ NAME: "MIKE" , AGE :"32" },
{ NAME: "BOB" , AGE: "21" }
],
columns: [
{
data: null,
defaultContent: '<input type="checkbox" name="chkbx">',
},
{ data: 'NAME' },
{ data : 'AGE' }
]
};

let table = $('#example').DataTable( option );

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954

    I updated a checkbox demo from another thread to show getting a specific column.
    http://live.datatables.net/mutavegi/511/edit

    I uses rows().nodes() with a row-selector as a function to get the checked checkboxes. It uses pluck() to get the name column. It optionally uses toArray() to convert the API data set to a Javascript object.

    Kevin

  • miyatakamiyataka Posts: 10Questions: 3Answers: 0

    Thank you for reply again Kevin-san!
    Is it possible to get only the pressed row when the checkbox is pressed?
    I was able to get the checkbox pressed event by the following method.

      $(document).on('change', 'tbody :checkbox', function() {
    

    And also I was able to get the index of the row where the checkbox was pressed by the following method

      let checkRow = $(this).parent().parent().index();
    

    I believe that I can achieve this by combining these elements and devising further.

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954
    Answer ✓

    Take a look at this example. You will need to get the td or tr of the clicked row to pass into row().data() as the row-selector. Something like this:

    var data = table.row( $(this).closest("td") ).data();
    

    Kevin

  • miyatakamiyataka Posts: 10Questions: 3Answers: 0

    Thank you again Kevin-san !
    I was able to solve the problem again with your help !

Sign In or Register to comment.