angular ui-grid event: row selected

前端 未结 3 1506
我在风中等你
我在风中等你 2021-02-19 02:14

I am trying to enable/disable a button based on the selection of a row on a ui-grid. If there are no rows selected, the button is disabled.

I found this plunkr with the

相关标签:
3条回答
  • 2021-02-19 02:51

    In ui-grid, you register a callback function on the event "rowSelectionChanged"

     $scope.gridOptions.onRegisterApi = function (gridApi) {
                    $scope.gridApi = gridApi;
                    gridApi.selection.on.rowSelectionChanged($scope, callbackFunction);
                    gridApi.selection.on.rowSelectionChangedBatch($scope, callbackFunction);
                }
     }
    
     function callbackFunction(row) { 
        var msg = 'row selected ' + row.isSelected; $log.log(msg); 
     })
    

    I think you should take a look at the tutorial page in ui-grid: http://ui-grid.info/docs/#/tutorial/210_selection. The API page sucks, in my opinion :(.

    0 讨论(0)
  • 2021-02-19 02:55

    you can first get all the records selected in the grid currently by doing :

    $scope.rowsSelected = $scope.gridApi.selection.getSelectedRows();
    

    now we can get the length of this array using :

    $scope.countRows = $scope.rowsSelected.length;
    

    based on $scope.countRows>0 or 0 you can enable or disable a button using

    ng-disabled = "countRows"
    
    0 讨论(0)
  • 2021-02-19 02:56
       $scope.countRows=0;
    
        $scope.gridOptions.onRegisterApi = function(gridApi){
    
           $scope.gridApi = gridApi;
    
           gridApi.selection.on.rowSelectionChanged($scope, function(row){ 
            $scope.countRows = $scope.gridApi.selection.getSelectedRows().length;
            });
    
           gridApi.selection.on.rowSelectionChangedBatch($scope, function(row){ 
            $scope.countRows = $scope.gridApi.selection.getSelectedRows().length;
            });
        }; 
    

    In the HTML side you can use some thing like this

        <button class="custom-button" ng-disabled="countRows<=0" ng-click="submit()">Details</button>
    
    0 讨论(0)
提交回复
热议问题