问题
I'm trying to make angular ui-grid automatically resize the height so that all rows are shown without the need of its scrollbar, but without wasting space if there are only a couple rows in the grid. Someone has asked a similar question (Angular ui-grid dynamically calculate height of the grid), but the question presupposes that the row heights are constant. If the row heights are different (for example, because you have word-wrap enabled), then the accepted solution to the problem (https://stackoverflow.com/a/28706349/877570) won't work, because the solution as does the question assumes constant row height. If I have a cell with a large amount of text in it, and the text wraps to the next line, then that rows height is different.
I found a possible solution by the user anhkind here: (https://github.com/angular-ui/ui-grid/issues/1735)
.ui-grid, .ui-grid-viewport {
  height: auto !important;
}
"And of course minRowsToShow and virtualizationThreshold should be set to the size of the list."
However, when I deploy his solution, it takes a much longer time for the grid to render.
Does anyone know how to address this or have an alternative solution?
回答1:
    $scope._minRows = 10;
    $scope._maxRows = 10;
   // Lots of Grid Options, plus data setting going on here.
    $scope.agendaItemsGridOptions.onRegisterApi = function(gridApi) {
        //set gridApi on scope
        $scope.gridApi = gridApi;
    });
    var setMinRowsLogic = function() {
        $scope.gridApi.grid.options.minRowsToShow = $scope._minRows;
        if (!_.isUndefined($scope.agendaItemsGridOptions.data)) {
            var len = $scope.agendaItemsGridOptions.data.length;
            if (len > $scope._maxRows) {
                $scope.gridApi.grid.options.minRowsToShow = $scope._maxRows;
            } else
            if (len < $scope._minRows) {
                $scope.gridApi.grid.options.minRowsToShow = $scope._minRows;
            } else {
                $scope.gridApi.grid.options.minRowsToShow = len;
            }
        }
    };
来源:https://stackoverflow.com/questions/40311664/angular-ui-grid-auto-height-if-rows-have-non-constant-height