0

Use case: user clicks on a datatable cell. Depending on the row and column of that cell certain action should be executed.

How can I check whether that doesn't click on a certain column and if he does not click on that column, then I would retrieve information from the row he clicked on and allow him to execute an action based on that information.

I did it like this and it works:

  var validColumn = false;
  $('#fooTable tbody').on( 'click', 'td', function () {
    validColumn = $('#fooTable').DataTable().cell(this).index().column !== 5;
  });
  $('#fooTable tbody').on('click', 'tr', function () {
    if (validColumn) {
       //do stuff
    }
  });

But I feel there is a more elegant approach.

1
  • Show your html to help! Commented Jul 27, 2016 at 15:27

1 Answer 1

1

First set an aria to the td to identify the position, something like Table wiht 6 columns so

<tr aria-number="1">
    <td aria-number="1">
    <td aria-number="2">
    <td aria-number="3">
    <td aria-number="4">
    <td aria-number="5">
    <td aria-number="6">
</tr>

Same for tr, think is easy way to do

Use jQuery function .parent(), sending $(this), so.

var td = $(this).parent();//If the item clicked is something inside of td
var tr = $(this).parent().parent();//To select the tr where was clicked

And call the aria with

if(td.attr('aria-number') == 6){ //do something; }
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.