How To Customize UI-Grid Height Dynamically

In this article, you will learn how to customize UI-Grid height dynamically based on record count list.

Intro

When we use UI-Grid with paging and load data from the database, then we provide constant height for that grid but sometimes the grid has fewer records and then we see some blank space in the bottom of the grid. So this article is about how to get rid of that blank space and customize grid height dynamically based on record count.

This is the second part for my existing article Custom Paging in Angular UI-Grid. Now we have records list based on custom paging.

Let’s take some screenshots with less data, so you can understand better.


In a given screenshot, as you can see, the grid has five records but still I can see some unused space in the bottom. So let’s get rid of this blank space dynamically.

Add the following function after adding previous article code.

  1. // for resize grid's height dynamically based on pageSize  
  2. $scope.tableHeight = 'height: 600px';  
  3.   
  4. function getTableHeight(totalPage, currentPage, pageSize, dataLen) {  
  5.     var rowHeight = 30; // row height  
  6.     var headerHeight = 50; // header height  
  7.     var footerHeight = 60; // bottom scroll bar height  
  8.     var totalH = 0;  
  9.     if (totalPage > 1) {  
  10.         if (currentPage < totalPage) {  
  11.             totalH = pageSize * rowHeight + headerHeight + footerHeight;  
  12.         } else {  
  13.             var lastPageSize = dataLen % pageSize;  
  14.             if (lastPageSize === 0) {  
  15.                 totalH = pageSize * rowHeight + headerHeight + footerHeight;  
  16.             } else {  
  17.                 totalH = lastPageSize * rowHeight + headerHeight + footerHeight;  
  18.             }  
  19.         }  
  20.         console.log(totalH);  
  21.     } else {  
  22.         totalH = dataLen * rowHeight + headerHeight + footerHeight;  
  23.     }  
  24.     return 'height: ' + (totalH) + 'px';  
  25. }  
  26. $interval(function() {  
  27.     $scope.tableHeight = getTableHeight($scope.totalPage, $scope.currentPage, $scope.pageSize, $scope.gridOptions.data.length);  
  28.     console.log($scope.tableHeight);  
  29.     $scope.gridApi.grid.handleWindowResize();  
  30.     $scope.gridApi.core.refresh();  
  31. }, 200);  

 

Now go back to html and add this style in UI-Grid.
  1. <div ng-controller="employeeController">  
  2.     <div ui-grid="gridOptions" ui-grid-pagination ui-grid-selection ui-grid-exporter ui-grid-resize-columns ui-grid-auto-resize class="grid" style="{{tableHeight}}"> </div>  
  3. </div>  
As you can see grid has a style tableHeight.

As everything is done, run the application.


As you can see in given screenshot, record count is five then grid has height accordingly. Let’s change the record count to 10 or 20.


As you can see in screenshot 3, I put record count as 10 and the grid height automatically increased.

Conclusion

In this article, we have seen how to implement custom height with Angular UI-Grid with Web API and Entity Framework in MVC. If you have any questions or comments, drop me a line in the comments section.