flower
flower

Reputation: 2242

Retrieving the row count from an html table

How can I calculate a grid's row count? My grid has data, but this code returns 0:

alert($('#Grid').data("kendoGrid").tbody.find('>tr.k-master-row').length);

Upvotes: 1

Views: 1780

Answers (3)

OnaBai
OnaBai

Reputation: 40887

First, one side note: finding the length of a grid based on tbody will show you only the number of rows in the view (those visible) if there is paging you will not know those not visible.

There are several options:

  1. Based on what you did: $("#grid").data('kendoGrid').tbody.find('>tr').length
  2. Not having to calculate it (solution 1 might be expensive since it has to count it based on navigating the DOM): $("#grid").data('kendoGrid').tbody[0].rows.length
  3. Based on dataSource.view: grid.dataSource.view().length

Upvotes: 3

Samuel Caillerie
Samuel Caillerie

Reputation: 8275

As OnaBai says, it depends if you want the whole count of lines in the table or only the one visibles.

If you want the total number of lines, you can use :

$("#grid").data("kendoGrid").dataSource.total();

and if you only want the visible ones, you can use :

$("#grid").find("tbody > tr").length;

Upvotes: 2

Muthu
Muthu

Reputation: 780

Try this @flower

$("#grid").data('kendoGrid').tbody[0].rows.length

Demo: http://jsfiddle.net/SWnSs/1/

Upvotes: 0

Related Questions