Is there a way to test if jqGrid has data or not?
Asked Answered
F

3

12

I'm trying to enable and disable custom buttons on a jqgrid, but would enable that button only if the grid is empty and then disable when its not.

Is there a way to test of the grid has data or not?

Thanks.

Froehlich answered 13/3, 2012 at 14:17 Comment(0)
T
13

You can test to see how many records are in the grid. If there are no rows then the grid is empty:

jQuery('#grid').jqGrid('getGridParam', 'reccount');

See the documentation for reccount:

Readonly property. Determines the exactly number of rows in the grid.

Also, since the default value is 0 you need to make sure you call this function after data has loaded, such as in the loadComplete event.

Thorlay answered 13/3, 2012 at 14:25 Comment(4)
Hey i've added some code that i did... I'm not sure if this is the way it is done. see from "var count".Froehlich
@Froehlich - I would have to see a larger code example to understand how everything fits together, especially how you are populating the grid. But what might work better for you is to create the pager and buttons at initialization time and then dynamically hide or disable buttons if the grid is empty after you try to populate it. Also, the question you asked here is a good general question, you might want to ask a new question (and link to it from this one) about your specific code.Thorlay
@Justin.. see new question here https://mcmap.net/q/1009449/-how-to-hide-and-show-custom-buttons-in-jqgrid-by-using-quot-reccount-quot/213982 This has the full code implementationFroehlich
@Froehlich - Thanks! I just posted an answer to that question, hopefully it will be useful to you...Thorlay
D
2

From the docs:

reccount integer Readonly property.

Determines the exactly number of rows in the grid. Do not mix this with records parameter. Instead that in most cases they are equal there is a case where this is not true. By example you define rowNum parameter 15, but you return from server records parameter = 20, then the records parameter will be 20, the reccount parameter will be 15, and in the grid you will have 15 records.

Dale answered 13/3, 2012 at 14:23 Comment(0)
R
1

In the loadcomplete event you have access to the data object that was bound to the grid and you can check the number of records. There you will also be able to setup your buttons

loadComplete: function(data){ 
    //data.Rows.length or call reccount
   },
Reciprocation answered 13/3, 2012 at 14:23 Comment(3)
I saw this answer a while ago. https://mcmap.net/q/582419/-how-to-display-information-in-jqgrid-that-there-are-not-any-data. Is it something like this?Froehlich
you wouldnt do the jQuery("#grid_id").getGridParam("records") call it is the old api and "data" in loadcomplete would contain those records and you could get the count from it or call the reccount method as other answers have indicated. But i do think you will want to do this in the loadcomplete event so that you can then make your button changesReciprocation
Agreed - See #9688961Thorlay

© 2022 - 2024 — McMap. All rights reserved.