Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular UI-Grid (new one) - Dynamic/Autosize Height Based on Rows

How do I get the angular ui-grid (the new one) to resize based on the number of rows? Bonus question, up to a max height or maximum number of rows to show?

like image 451
richard Avatar asked Mar 17 '26 04:03

richard


1 Answers

Here is complete dynamic UI-Grid Implementation.

Working with pagination options and grouping.

$scope.gridOptions = {
enableFiltering: true,
paginationPageSizes: [10, 25, 50, 100, 500],
paginationPageSize: 10,
onRegisterApi: function(gridApi) {
  $scope.gridApi = gridApi;
  $scope.gridApi.grid.registerRowsProcessor($scope.singleFilter, 200);
  $scope.gridApi.treeBase.on.rowExpanded(null, function(row) {
    updatePagination(row.treeNode.children.length);
  });
  $scope.gridApi.treeBase.on.rowCollapsed(null, function(row) {
    updatePagination(-row.treeNode.children.length);
  });
}

};

And the functions:

function updatePagination(rowsDifference) {
  //updating pagination will trigger singleFilter where the height is being reset
  $scope.gridOptions.paginationPageSizes = [$scope.gridOptions.paginationPageSize + rowsDifference, 25, 50, 100, 500];
  $scope.gridOptions.paginationPageSize = $scope.gridOptions.paginationPageSize + rowsDifference;
}
//you can name the function anything, I was doing global external filtering inside this, so just named this.
$scope.singleFilter = function(renderableRows) {
    $timeout(function() {
      var newHeight = ($scope.gridApi.grid.getVisibleRowCount() * 30) + 145;
      angular.element(document.getElementsByClassName('grid')[0]).css('height', newHeight + 'px');
    }, 10);
    return renderableRows;
  }

Don't forget to inject 'ui.grid.grouping', 'ui.grid.autoResize', 'ui.grid.pagination' dependencies

HTML

<div ui-grid="gridOptions" class="grid" ui-grid-auto-resize ui-grid-pagination ui-grid-grouping></div>

Working PLUNKER

like image 96
Fahad Khan Avatar answered Mar 18 '26 19:03

Fahad Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!