In this post, I’ll show how display a row count for the grid and update it after executing a search.
Dojo Data Grid Series
Dojo Grids in XPages — All Blog Posts
All About the Timing
The grid has a rowCount property which will generally return the number of rows in the grid.
It will work fine if you check it by clicking a button after the page loads. However, if you run this immediately as the page loads (even if it’s in the onClientLoad event), it will return 0, because the grid hasn’t been drawn out yet).
At this point, my solution is to introduce a delay to give it time — then it can return the correct amount.
Displaying the Count
One simple way to display the count is to put a simple span tag on the page, add a class to it so it can be programmatically updated.
I added this to my page:
Row Count: <span class="displayRowCount"></span>
Updating the Count
Another consideration is that you don’t just want to check the count when the page loads, you’ll want it to be updated any time the grid is refreshed.
In the post where I showed how to add search to a grid I mentioned that executing a search must refresh the rest service and the grid, so you should put them both in a panel and target that panel with a partial refresh.
To finish this solution, add code that will check the grid’s row count and update the display (span) tag on the onClientLoad event of the panel, so it re-runs every time the panel is refreshed.
var t=setTimeout(function(){ var myGrid = dijit.byId('#{id:djxDataGrid1}'); dojo.query('.displayRowCount')[0].innerHTML = myGrid.rowCount; } ,500);
This code waits half of a second (500 milliseconds) and then checks the grid’s row count (line 2) and updates the span tag to display it (line 3).
These screen shots show the row count below the grid after initial load and then after a search:
REST Service Note
I tested this with a JSON rest service as the source for the grid. Interestingly, the service’s count property is set to 20, so it shouldn’t be retrieving the full set of rows, but it returns the full count of over 1,300 anyway.
