1

I want to be able to click on a column cell and also retrieve the cell value of another defined column of the same row in order to store both values in an array which can be passed on.

Here's what I tried:

$('#datatab tbody').on('click', 'tr td.lastname', function () {
    var data = dt.column(0).cell(this).data();
    console.log(data);
    var data1 = dt.columns([0, 1]).data();
    console.log(data1);

....
}

If the table had a lastname and firstname column (not always adjacent), I would need to grab those two cell values based on the column index for example or some css class.

Hope you can help me.

Thank you.

2
  • Try $('#datatab').row( $(this).parents('tr') ).data(); for data selection under your click function. This will give full row data. Commented Jul 4, 2016 at 9:32
  • didn't work for me. "row" is undefined... Commented Jul 4, 2016 at 12:14

1 Answer 1

1

You can get the data of entire row on click of specific column using the below function

//Function for handling click event
$('body').on('click', '#datatab tbody tr td.lastname', function () {

  rowData = table.row( $(this).parents('tr') ).data();

  console.log("First Name : ", rowData[0], "\t\tLast Name : ", rowData[1], "\t\tAge : ", rowData[2]);
 });

I have created the JSFiddle https://jsfiddle.net/Prakash_Thete/0yemnxq9/1/ for the same.Please have a look.

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

1 Comment

very nice and even simpler than I thought :-) Thank you.

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.