Count Datatable rows with specific String

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Count Datatable rows with specific String



How to count datatable rows with specific text?
I try to use filter() but is not working.


$("#buttonAlert").on("click", function (event)

var count = $("#example1").DataTable.rows
.column(4)
.data()
.filter(function (value, index)
return value = "OK" ? true : false;
).length;

alert('Total OK is: ' + count);

);



The error like below:


ssar:391 Uncaught TypeError: Cannot read property 'column' of undefined
at HTMLButtonElement.<anonymous> (ssar:391)
at HTMLButtonElement.dispatch (jQuery-2.1.4.min.js:3)
at HTMLButtonElement.r.handle (jQuery-2.1.4.min.js:3)




2 Answers
2



Try the below one, I am pushing the rows to matchedRows array based on the column value equality...


matchedRows


var matchedRows = ;
var table = $('#table_id').DataTable();
var data = table.data().toArray();
data.forEach(function(row)
row.forEach(function (column)
if (column == "Row 1 Data 1")
matchedRows.push(row);

)
);
console.log(matchedRows.length);





I've checked in the respective codepen codepen.io/anon/pen/EppebR?editors=1111
– Javascript Lover - SKT
Aug 6 at 6:56





Hi, thank you, but I don't understand, I think this can be more simple
– Bayu Sri Hernogo
Aug 6 at 7:02





@BayuSriHernogo Sorry, I didn't get you. Is that solution working for you?
– Javascript Lover - SKT
Aug 6 at 7:03






Hi, thanks you this work, but this still not standard Datatable function I think, but thank you I can use this method
– Bayu Sri Hernogo
Aug 6 at 7:13





@BayuSriHernogo yeah that's correct :-). But I used Datatables long back ago. I've to check the documentation once, if I find anything minimized code, I'll update my answer
– Javascript Lover - SKT
Aug 6 at 7:19



Datatables



You need to use and columns() with eq(4) method of dataTable like


columns()


eq(4)


dataTable


var count = 0;
$("#example1").DataTable.columns().eq(4).each( function ( index )
var column = $("#example1").DataTable.column( index );
var value = column.data();
if(value === 'OK')
count++;

);

console.log('length'+count);





Hi, Mr. Ankit, I try but still getting error: Uncaught TypeError: Cannot read property 'each' of null
– Bayu Sri Hernogo
Aug 6 at 6:48






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard