Angular UI 网格 - 选定行上的单击事件
Angular UI Grid - Click event on selected row
目标
我有一个 UI 网格。当我点击一行时,它应该被选中,并且应该调用一个以该行作为参数的函数。
当前方法
我使用以下配置代码生成网格:
$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)
。
将对addToQueue函数的调用移动到gridApi.grid.element.on('click'...)
函数,并将row存储在gridApi.selection.on.rowSelectionChanged
函数中:
$scope.gridOptions.onRegisterApi = function (gridApi) {
//set gridApi on scope
$scope.gridApi = gridApi;
gridApi.selection.on.rowSelectionChanged($scope, function (row) {
$scope.gridApi.grid.appScope.lastSelectedRow = row;
});
gridApi.grid.element.on('click', function (ev) {
if ($scope.gridApi.grid.appScope.lastSelectedRow) {
// affect only rows (not footer or header)
if (ev.target.className.includes('ui-grid-cell-contents')) {
var name = $scope.gridApi.grid.appScope.lastSelectedRow.entity.name;
$scope.addToQueue(name);
}
}
});
};
$scope.addToQueue = function addToQueue (name) {
console.log('addToQueue fired, name = ' + name);
};
对于要 selected 的行,单击它时,我使用以下内容:
对所有列使用 selectionCellTemplate:
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() 方法定义为:
$scope.rowClick = function (row) {
var index = row.grid.renderContainers.body.visibleRowCache.indexOf(row);
$scope.gridApi.selection.selectRow($scope.gridOptions.data[index]);
};
我也定义了 multiselect 为 true
$scope.gridOptions.multiSelect = true;
所以行点击将 select 该行并将其添加到 selected 行。您可以访问这些 selected 行(它为每一行触发 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
});
}
或 selected 行可以随时访问:
$scope.gridApi.selection.getSelectedRows()
批准的答案对我有用,但这里有一个小错误...
var selectionCellTemplate = '<div class="ngCellText ui-grid-cell-contents">' +
' <div ng-click="grid.appScope.rowClick(row)">{{COL_FIELD}}</div>' +
'</div>';
我需要将其更改为...
var selectionCellTemplate = '<div class="ngCellText ui-grid-cell-contents"
ng-click="grid.appScope.rowClick(row)">' +
'<div>{{COL_FIELD}}</div>' +
'</div>';
您会注意到我将 ng-click 移到了父级 div。
这是必需的,因为如果单元格为空,则不会触发 ng-click 事件。
目标
我有一个 UI 网格。当我点击一行时,它应该被选中,并且应该调用一个以该行作为参数的函数。
当前方法
我使用以下配置代码生成网格:
$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)
。
将对addToQueue函数的调用移动到gridApi.grid.element.on('click'...)
函数,并将row存储在gridApi.selection.on.rowSelectionChanged
函数中:
$scope.gridOptions.onRegisterApi = function (gridApi) {
//set gridApi on scope
$scope.gridApi = gridApi;
gridApi.selection.on.rowSelectionChanged($scope, function (row) {
$scope.gridApi.grid.appScope.lastSelectedRow = row;
});
gridApi.grid.element.on('click', function (ev) {
if ($scope.gridApi.grid.appScope.lastSelectedRow) {
// affect only rows (not footer or header)
if (ev.target.className.includes('ui-grid-cell-contents')) {
var name = $scope.gridApi.grid.appScope.lastSelectedRow.entity.name;
$scope.addToQueue(name);
}
}
});
};
$scope.addToQueue = function addToQueue (name) {
console.log('addToQueue fired, name = ' + name);
};
对于要 selected 的行,单击它时,我使用以下内容:
对所有列使用 selectionCellTemplate:
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() 方法定义为:
$scope.rowClick = function (row) {
var index = row.grid.renderContainers.body.visibleRowCache.indexOf(row);
$scope.gridApi.selection.selectRow($scope.gridOptions.data[index]);
};
我也定义了 multiselect 为 true
$scope.gridOptions.multiSelect = true;
所以行点击将 select 该行并将其添加到 selected 行。您可以访问这些 selected 行(它为每一行触发 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
});
}
或 selected 行可以随时访问:
$scope.gridApi.selection.getSelectedRows()
批准的答案对我有用,但这里有一个小错误...
var selectionCellTemplate = '<div class="ngCellText ui-grid-cell-contents">' +
' <div ng-click="grid.appScope.rowClick(row)">{{COL_FIELD}}</div>' +
'</div>';
我需要将其更改为...
var selectionCellTemplate = '<div class="ngCellText ui-grid-cell-contents"
ng-click="grid.appScope.rowClick(row)">' +
'<div>{{COL_FIELD}}</div>' +
'</div>';
您会注意到我将 ng-click 移到了父级 div。 这是必需的,因为如果单元格为空,则不会触发 ng-click 事件。