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.

pundit
  • 772
  • 3
  • 18
  • 31

3 Answers3

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.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • 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". – pundit Mar 13 '12 at 15:10
  • @pundit - 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. – Justin Ethier Mar 13 '12 at 15:53
  • @Justin.. see new question here http://stackoverflow.com/q/9688461/213982 This has the full code implementation – pundit Mar 13 '12 at 16:55
  • @pundit - Thanks! I just posted an answer to that question, hopefully it will be useful to you... – Justin Ethier Mar 13 '12 at 17:23
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.

epascarello
  • 204,599
  • 20
  • 195
  • 236
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
   },
DisplayName
  • 3,093
  • 5
  • 35
  • 42
  • I saw this answer a while ago. http://stackoverflow.com/a/1019528/213982. Is it something like this? – pundit Mar 13 '12 at 14:28
  • 2
    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 changes – DisplayName Mar 13 '12 at 14:31
  • 1
    Agreed - See http://stackoverflow.com/questions/9688461/how-to-hide-and-show-custom-buttons-in-jqgrid-by-using-reccount/9688910#9688910 – Justin Ethier Mar 13 '12 at 17:24