Javascript 我在哪里可以获得 Angular ui-grid 选定的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26188565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where can I get Angular ui-grid selected items
提问by Mlalahoi
Testing out Angular ui-grid (ng-grid v.3.0). Can not for the life of me find the selected row. I just want to grab the rows or even row ID of row when a user clicks it. Found the top comment here but I think this is outdated: Getting select rows from ng-grid?
测试 Angular ui-grid (ng-grid v.3.0)。不能为我的生活找到选定的行。当用户单击它时,我只想获取行的行甚至行 ID。在这里找到了最高评论,但我认为这已经过时了: 从 ng-grid 中获取选择行?
Does anyone know where the gridOptions.selectedItemsis being stored in 3.0?
有谁知道gridOptions.selectedItems3.0 中的存储位置?
采纳答案by Sycomor
Is this what your are looking for ? http://ui-grid.info/docs/#/tutorial/210_selection
这是您要找的吗? http://ui-grid.info/docs/#/tutorial/210_selection
- Activate grid selection capabilities with the ui-grid-selection tag (and ui.grid.selection module registration in your app
- register gridApi and use gridApi.selection to access getSelectedRows()
- 使用 ui-grid-selection 标签激活网格选择功能(以及应用程序中的 ui.grid.selection 模块注册
- 注册 gridApi 并使用 gridApi.selection 访问 getSelectedRows()
回答by Rajush
In addition to the steps above https://stackoverflow.com/a/26188783/2658127, you might have to invoke it through a ng-click event to get the actual value/object. At least that's how I had it working.
除了https://stackoverflow.com/a/26188783/2658127上面的步骤之外,您可能还需要通过 ng-click 事件调用它来获取实际值/对象。至少我是这样工作的。
Eg:
$scope.selectRow = function(){
$scope.gridApi.selection.getSelectedRows();
};
And call selectRow() from the template.
并从模板中调用 selectRow()。
This is for anybody who have been confused like I did, considering the fact that ui-grid does not have the best documentation (specially for this select portion).
考虑到 ui-grid 没有最好的文档(特别是对于这个选择部分),这适用于像我一样感到困惑的任何人。
回答by Arashsoft
The easiest approach is:
最简单的方法是:
Register the gridApi by adding this your controller:
$scope.gridOptions.onRegisterApi = function(gridApi) { $scope.myGridApi = gridApi; };Access the array of selected items:
$scope.myGridApi.selection.getSelectedRows();
通过添加您的控制器来注册 gridApi:
$scope.gridOptions.onRegisterApi = function(gridApi) { $scope.myGridApi = gridApi; };访问所选项目的数组:
$scope.myGridApi.selection.getSelectedRows();
回答by Panciz
With grid ui you have to use the selection.on.rowSelectionChangedto update a scope variable that store the selectedItem.
In this way you can use the value in a binding expression.
使用网格 ui,您必须使用selection.on.rowSelectionChanged更新存储 selectedItem 的范围变量。通过这种方式,您可以在绑定表达式中使用该值。
var SelectController = function($scope) {
...
$scope.selectedItem = null;
$scope.gridOptions = {
data : 'articles',
enableRowSelection : true,
multiSelect : false,
enableRowHeaderSelection : false,
...
};
$scope.gridOptions.onRegisterApi = function(gridApi) {
// set gridApi on scope
this.$scope.gridApi = gridApi;
}.bind(this);
$scope.gridOptions.onRegisterApi = function(gridApi) {
// set gridApi on scope
this.$scope.gridApi = gridApi;
this.$scope.gridApi.selection.on.rowSelectionChanged($scope,
function(row) {
this.$scope.selectedItem = row.entity;
}.bind(this));
}.bind(this);
Use a an array instead of a plain object if you need multiple selection.
如果您需要多项选择,请使用数组而不是普通对象。

