To count the number of rows, the “#Table_Id tr” selector is used. It selects all the <tr> elements in the table. This includes the row that contains the heading of the table. The length property is used on the selected elements to get the number of rows.
To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.
Simple solution is to use: var rowCount = document. getElementById("tableId"). rows.
To get the row count of an HTML table with JavaScript, we can use the rows. length property to get the total number of rows in the table. And to get the number of rows in tbody , we can use the tBodies[0]. rows.
Use a selector that will select all the rows and take the length.
var rowCount = $('#myTable tr').length;
Note: this approach also counts all trs of every nested table!
If you use <tbody>
or <tfoot>
in your table, you'll have to use the following syntax or you'll get a incorrect value:
var rowCount = $('#myTable >tbody >tr').length;
Alternatively...
var rowCount = $('table#myTable tr:last').index() + 1;
jsFiddle DEMO
This will ensure that any nested table-rows are not also counted.
Well, I get the attr rows from the table and get the length for that collection:
$("#myTable").attr('rows').length;
I think that jQuery works less.
Here's my take on it:
//Helper function that gets a count of all the rows <TR> in a table body <TBODY>
$.fn.rowCount = function() {
return $('tr', $(this).find('tbody')).length;
};
USAGE:
var rowCount = $('#productTypesTable').rowCount();
I got the following:
jQuery('#tableId').find('tr').index();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With