Angular ui-grid 3.0 get selected rows
This answer states that this code:
$scope.gridOptions.onRegisterApi = function(gridApi){
$scope.gridApi = gridApi;
$scope.mySelectedRows=$scope.gridApi.selection.getSelectedRows();
}
Should work to get the selected rows, but for me it always returns [] to keep track of the selected rows, I have to call gridApi.selection.getSelectedRows()
every time the select event fires, is that correct?
What I want to achieve is to make my own footer that keeps track of the number of selected grid rows, is this the correct way to achieve this?
source to share
There is already an example showing the number of selected items in the footer.
This plnkr shows the footer of the selected items. http://plnkr.co/edit/jc1YPCXBmfOKWyu8sLkx?p=preview
If you want to continue parsing the selected row, you can register a listener for the selected row and act on it.
$scope.gridOptions.onRegisterApi = function(gridApi){
//set gridApi on scope
$scope.gridApi = gridApi;
gridApi.selection.on.rowSelectionChanged($scope,function(row){
var msg = 'row selected ' + row.isSelected;
$log.log(msg);
});
gridApi.selection.on.rowSelectionChangedBatch($scope,function(rows){
var msg = 'rows changed ' + rows.length;
$log.log(msg);
});
};
source to share
I have a job without using an event trigger. I added a function, bound it to a button, and can only fetch selected items when I need them.
$scope.gridOptions = {
data: 'data',
enableRowSelection: true,
onRegisterApi: function(gridApi) { //register grid data first within the gridOptions
$scope.gridApi = gridApi;
}
};
//this is the on click function
$scope.getCurrentSelection = function() {
var currentSelection = $scope.gridApi.selection.getSelectedRows();
console.log(currentSelection);
};
source to share
$scope.gridOptions = {
data: 'data',
enableRowSelection: true,
onRegisterApi: function(gridApi) { //register grid data first within the gridOptions
$scope.gridApi = gridApi;
}
};
Once you have access to the {{vm.gridApi.selection.getSelectedRows () variable. Length}} in the html template.
source to share