1

I want to count all rows inside a column with the output idea.

I tried this:

alert('Rows: ' + table.column(1).rows(':contains("idea")').count());

the .column(1) doesn't seem to work because it also counts other columns.

Example of the DataTable

Column0  | Column1
------------------
blahidea | error
blah     | idea 
blah     | idea
blah     | error

the alert will count 3 and not 2! What do I need to change that it counts only 2?

1 Answer 1

2

Using search() and rows()

var table = $('#example').DataTable()

var length = table
              .column(1)
              .search('idea')
              .rows({ search: 'applied'})
              .count()

console.log(length)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />


<table id="example" class="display" style="width:100%">
  <thead>
    <tr>
      <th>Column0</th>
      <th>Column1</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>blahidea </td>
      <td>error</td>
    </tr>
    <tr>
      <td>blah </td>
      <td>idea </td>
    </tr>
    <tr>
      <td>blah </td>
      <td>idea </td>
    </tr>
    <tr>
      <td>blah </td>
      <td>error </td>
    </tr>
  </tbody>
</table>

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

3 Comments

That's good. The only thing I dont like about it is that it manipulates the table. So the rows with error don't show up anymore
To my question! After the alert you can do a table.column(1).search(''); to prevent that the searchfunction manipulates the table! and Thank You! :)
@Timebreaker900 search('') is absolutely fine. Or else you can use filter instead of search with a callback

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.