Angular UI Grid - 所选行上的Click事件 [英] Angular UI Grid - Click event on selected row
问题描述
目标
我有一个UI网格。当我点击一行时,它应该被选中,并且应该调用以行作为参数的函数。
I've got a UI Grid. When I click on a row it should get selected and a function with the row as a parameter should be called.
当前方法
我使用以下配置代码生成网格:
I use the following config code to generate the Grid:
$scope.gridOptions = {
enableFiltering: true,
enableRowHeaderSelection: false,
enableRowSelection: true,
multiSelect: false,
noUnselect: true,
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
$scope.gridApi.selection.on.rowSelectionChanged($scope, function (row) {
var name = row.entity.name;
$scope.addToQueue(name);
});
}
};
问题
当我实际更改选择时(作为函数的名称建议),上面的代码很有效。但应该可以多次向队列添加一行。因此,即使已经选择了行,我也想调用 $ scope.addToQueue(name)
。
The above code works well when I actually change the selection (as the name of the function suggest). But it should be possible to add a row multiple times to the queue. So I want to call $scope.addToQueue(name)
even when the row is already selected.
推荐答案
对于要选择的行,单击它时,我使用以下内容:
For row to be selected, when it is clicked, I use following:
对所有列使用selectionCellTemplate:
Use selectionCellTemplate for all columns:
var selectionCellTemplate = '<div class="ngCellText ui-grid-cell-contents">' +
' <div ng-click="grid.appScope.rowClick(row)">{{COL_FIELD}}</div>' +
'</div>';
$scope.gridOptions.columnDefs = [
{ name: 'name', displayName: 'Name', width: '15%', cellTemplate: selectionCellTemplate },
];
然后将rowClick()方法定义为:
And then define rowClick() method as:
$scope.rowClick = function (row) {
var index = row.grid.renderContainers.body.visibleRowCache.indexOf(row);
$scope.gridApi.selection.selectRow($scope.gridOptions.data[index]);
};
我还将multiselect定义为真
I have also defined multiselect to be true
$scope.gridOptions.multiSelect = true;
因此,行单击将选择该行并将其添加到所选行。您可以访问这些选定的行(它为每行选择/取消选择触发):
So row click will select the row and add it to the selected rows. you can access these selected rows as (It is triggered for each row select/unselect) :
$scope.gridOptions.onRegisterApi = function (gridApi) {
//set gridApi on scope
$scope.gridApi = gridApi;
gridApi.selection.on.rowSelectionChanged($scope, doSelection);
};
function doSelection(row) {
_.each($scope.gridApi.selection.getSelectedRows(), function (row) {
//Do something //It is triggered for each row select/unselect
});
}
或者可以随时访问所选行:
Or selected rows can be accessed anytime:
$scope.gridApi.selection.getSelectedRows()
这篇关于Angular UI Grid - 所选行上的Click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!