How can I hide the jqgrid completely when no data returned?

<!-- table for Pagination START -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td>
            <div id="pager" class="scroll" style="text-align: right;" />
        </td>
    </tr>
</table> 
<!-- table for Pagination END -->
<table id="tblCompany" class="scroll" width="100%" cellpadding="0" cellspacing="0">
</table>
<div id="NoRecord" class="NoRecord">
    <%: CommonText.NoRecords %>
</div>

JqGrid No Records Check

gridComplete: function () {
    var recs = $("#").getGridParam("records");
    if (recs == 0 || recs == null) {
        $(tableContacts).setGridHeight(100);
        $("#NoRecordContact").show();
    }
}

I'm finding that this:

parseInt($("#grid").getGridParam("records"),10);

is returning a "NaN." The "records" property is set to null if there are no records in the grid. So you can't cast it to a number and check to see if it equals zero.


jqGrid wraps your table with it's special sauce and divs so you should be able to do what you want by wrapping that table with your own div that you can hide:

 <div id="gridWrapper">
    <table id="list" class="scroll"></table> 
 </div>

Then in your gridComplete:

   gridComplete: function() {
        var recs = parseInt($("#list").getGridParam("records"),10);
        if (isNaN(recs) || recs == 0) {
            $("#gridWrapper").hide();
        }
        else {
            $('#gridWrapper').show();
            alert('records > 0');
        }
    }

Hope this helps.


just a little twist on seth's solution:

  1. you can use in place of var recs = $('#list').jqGrid('getGridParam','records');

    var recs = $('#list').jqGrid('getGridParam','reccount');

    jqGrid grid option 'records' default value = 'None'

    jqGrid grid option 'reccount' defaults to 0 and will always return a number even when there are no records (returns 0)

    see wiki:options @ jqGrid Wiki

  2. If you don't want to use a wrapping div you can hide the whole jqGrid using $('.ui-jqgrid').hide() or $('.ui-jqgrid').show().

    The ui-jqgrid class is only used for the jqGrid parent.