ArticleZip > How Can I Hide The Jqgrid Completely When No Data Returned

How Can I Hide The Jqgrid Completely When No Data Returned

When working with jqGrid, there may be times when you want to hide the entire jqGrid table if no data is returned from your server. This can create a cleaner user interface and prevent empty grids from cluttering your page. In this article, I'll walk you through a simple and effective way to hide the jqGrid completely when no data is returned.

One common approach to hiding the jqGrid when it's empty is to check if there are any rows returned from the server. If there are no rows, you can hide the jqGrid element. Here's how you can achieve this in your code:

First, you need to include the logic to check if there is any data returned from the server. You can do this by checking the length of the data array or the number of rows returned.

Javascript

loadComplete: function(data) {
  if (data && data.rows && data.rows.length === 0) {
    $('#jqGrid').hide();
  } else {
    $('#jqGrid').show();
  }
}

In the code snippet above, we are using the `loadComplete` event of jqGrid to check if the `data` object contains any rows. If there are no rows (i.e., length is 0), we hide the `jqGrid` element using jQuery's `hide()` method. Otherwise, we show the `jqGrid` element.

Remember to replace `#jqGrid` with the actual ID of your jqGrid element in your code.

Another way to handle hiding the jqGrid when no data is returned is to set the `height` property of the jqGrid dynamically based on the number of rows returned. If no rows are returned, you can set the height to 0 to effectively hide the jqGrid. Here's an example of how you can achieve this:

Javascript

loadComplete: function(data) {
  var gridHeight = data.rows ? data.rows.length * 30 : 0; // Assuming row height is 30px
  $('#jqGrid').jqGrid('setGridHeight', gridHeight);
}

In this code snippet, we calculate the `gridHeight` based on the number of rows returned multiplied by the height of each row (assuming 30 pixels). If no rows are returned, the `gridHeight` is set to 0, effectively hiding the jqGrid by setting its height to 0.

By using these techniques, you can easily hide the jqGrid when no data is returned, providing a better user experience and keeping your interface clean and organized. Experiment with these methods in your jqGrid implementation and see which approach works best for your specific case.

×