AngularJS ui-grid row select limit

孤街醉人 提交于 2019-12-24 00:19:42

问题


I want to limit the selection in my ui-grid to 10. In my gridOptions I do

onRegisterApi: function (gridApi) {
    $scope.gridApi = gridApi;
    gridApi.selection.on.rowSelectionChanged($scope, function (row) {
        $scope.rowsSelected = $scope.gridApi.selection.getSelectedRows();
        $scope.countRows = $scope.rowsSelected.length;
        if ($scope.countRows === 10)
        {
            // disable option to select rows now
        }
    });
}

but now I don't know how to disable this option... thanks for any help!


回答1:


A possible solution, albeit a bit late:

onRegisterApi: function (gridApi) {
    $scope.gridApi = gridApi;
    gridApi.selection.on.rowSelectionChanged($scope, function (row) {
        $scope.rowsSelected = $scope.gridApi.selection.getSelectedRows();
        $scope.countRows = $scope.rowsSelected.length;
        if ($scope.countRows > 10)
        {
            row.setSelected(false); // Remove selection for the current row
        }
    });
}

When the rowSelectionChanged event is triggered, the row has already been selected, and the selectedCount has been updated. If you want a maximum of 10 selected rows, we need to deselect the current row when there are more than 10 rows selected.

Also, make sure $scope.gridOptions.enableSelectionBatchEvent = false.



来源:https://stackoverflow.com/questions/39271440/angularjs-ui-grid-row-select-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!