1

I have a simple DataTable for which I'm adding a class called selected when the user clicks the row:

$("#datatable-buttons tbody").delegate("tr", "click", function (event) {
     var $row = $(event.target);
     if ($row[0].tagName !== "TR") $row = $row.parent();
     $row.toggleClass("selected");
     if (event.ctrlKey === false) {
        $row.toggleClass("selected");
        $row.siblings().removeClass("selected");
     }
});

Inside the same function I'm trying to count the rows that have the second column different from ---

var clickedD = 0;
table.rows('.selected').every(function () {
      if (this.cell('.selected', 1).data() != "---")
          clickedD++;
 });

But when there are more than one row selected it counts just the first row with this class. Is there a way to get the number of rows that are selected (have the class selected) with the second cell of each row different from ---?

1 Answer 1

2

You can just use the row().data() array. And use the API more heavily. Here is a more simplified version :

table.on('click', 'tbody tr', function() {
  table.row(this).nodes().to$().toggleClass('selected');
  var count = 0;
  table.rows('.selected').every(function() {
    if (table.row(this).data()[1] !== '---') count ++;
  })
  $('#count').text(count+' selected rows with #2 col different from ---')
})

demo -> http://jsfiddle.net/q6d8wqLk/

If you have a JSON based dataSource,, you can use

if (table.row(this).data().secondColData !== '---') count ++;

FYI: delegate() is now deprecated.

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

1 Comment

Thanks for your answer! Works perfectly! :)

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.