How to Count the Entire Number of Rows in a Datatable

How to count the entire number of rows in a datatable

Update for New Versions

table.data().count()

Reference:https://datatables.net/reference/api/count()

For older versions:

$(document).ready(function() {
//Initialize your table
var table = $('#jobSearchResultTable').dataTable();
//Get the total rows
alert(table.fnGetData().length);
});

Source: https://stackoverflow.com/questions/3238047/jquery-datatables-row-count-across-pages

Another method:

table.fnSettings().fnRecordsTotal();

see which one works for you

Source: http://datatables.net/forums/discussion/2278/how-to-get-number-of-rows/

Count how many rows and columns are in a mysql datatable

For the number of rows, a simple count query will do:

SELECT COUNT(*) AS num_rows FROM yourTable;

For the number of columns, we need to do a bit more work:

SELECT COUNT(*) AS num_columns
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'yourdatabasename'
AND TABLE_NAME = 'yourTable';

You could combine these into a single query if you wanted, or even write a stored procedure. By the way, I can't even think of the last time I needed to know the number of columns in a MySQL table. Most of the time, you would know beforehand which columns you wanted to access, and so you would not need a numerical count. The number of rows is another story, and often you might want to know this.

Datatables count rows only in a specific column

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>

How to count number of rows in data.table using same condition on multiple columns in R

We can use melt to reshape into 'long' format, and then do a group by 'variable' (i.e. the column name column), get the number of 'No' by summing the logical vector (value == 'No')

library(data.table)
melt(dt, id.var = 'q')[, .(Type = 'No', Count = sum(value == 'No')), .(variable)]
# variable Type Count
#1: x No 2
#2: y No 1
#3: z No 3

For the new example

melt(dt[, .(x, y, z)], measure = c('x', 'y', 'z'))[, 
.(Type = 'No', Count = sum(value == 'No')), .(variable)]
# variable Type Count
#1: x No 2
#2: y No 1
#3: z No 3

Or specify the logical condition in i and use .N

melt(dt[, .(x, y, z)], measure = c('x', 'y', 'z'))[value == 'No',
.(Type = first(value), Count = .N), variable]
# variable Type Count
#1: x No 2
#2: y No 1
#3: z No 3

Or without melting we can loop over the Subset of Data.table (.SD), create a logical vector and reduce the list to a single vector with Reduce

dt[, .(variable = names(.SD), Type = 'No', 
Count = Reduce(`+`, lapply(.SD, `==`, 'No'))), .SDcols = x:z]
# variable Type Count
#1: x No 3
#2: y No 2
#3: z No 1

Or using rowSums

dt[, .(Variable = names(.SD), Type = 'No',
Count = rowSums(.SD == 'No')), .SDcols = x:z]

In the OP's code, the grouping columns were taken as 'x', 'y', 'z'. Then, if we do .N, it is looking for the number of rows for each unique set of values across those columns and this will be 1 for each case because there is only 1 row for No No No or No Yes No or Yes Yes No



Related Topics



Leave a reply



Submit