如何从ng-grid获取单元格值

时间:2022-08-24 11:29:48

I am a beginner of AngularJS. I study the demo of ng-grid and have a question.

我是AngularJS的初学者。我研究了ng-grid的演示并提出了一个问题。

index.html

<!DOCTYPE html>
<html ng-app="myApp">

<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<link rel="stylesheet" type="text/css" href="http://angular-ui.github.com/ng-grid/css/ng-grid.css" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
<script type="text/javascript" src="http://angular-ui.github.com/ng-grid/lib/ng-grid.debug.js"></script>
<script src="app.js"></script>
</head>

<body ng-controller="MyCtrl">
    <div class="gridStyle" ng-grid="gridOptions"></div>
    <div class="selectedItems">{{mySelections}}</div><br><br>

</body>

</html>

app.js

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
$scope.mySelections = [];
$scope.myData = [{name: "Moroni", id: 1},
                 {name: "Tiancum", id: 2},
                 {name: "Jacob", id: 3},
                 {name: "Nephi", id: 4},
                 {name: "Akon", id: 5},
                 {name: "Enos", id: 6}];
$scope.gridOptions = { 
data: 'myData',
selectedItems: $scope.mySelections,
multiSelect: true 
};

//$scope.mySelections_id = $scope.mySelections.length;

});

When I select the first row, the div of selectedItems will show [{"name":"Moroni","id":1}]. the result is OK. If I just want to get the value of cell [id] from selected row, how do I modify my code?

当我选择第一行时,selectedItems的div将显示[{“name”:“Moroni”,“id”:1}]。结果还可以。如果我只想从所选行获取cell [id]的值,我该如何修改我的代码?

here is Plunker

这是Plunker

3 个解决方案

#1


0  

Acutally, I want to get a cell value and modify when user selects the cell at the table.

实际上,我想得到一个单元格值并在用户选择表格中的单元格时进行修改。

Above answers are using fixed columns's filed.. like

上面的答案是使用固定列提交..喜欢

 $scope.selectedIDs.push( item.id ) // id is column filed

Instead of these answers, I found another way exactly what I want to achieve with.

我没有找到这些答案,而是找到了另一种我希望实现的方式。

Example code: http://plnkr.co/edit/wfiMhlJ7by4eUHojjKT4?p=preview

示例代码:http://plnkr.co/edit/wfiMhlJ7by4eUHojjKT4?p = preview

editableCellTemplate which is an option for columnDefs is used for solving this problem.

editableCellTemplate是columnDefs的一个选项,用于解决此问题。

Angular is Aswome!!

#2


5  

Use the afterSelectionChange callback to extract the ids from the selection to an other array.

使用afterSelectionChange回调将选择中的id提取到另一个数组。

$scope.gridOptions = { 
    data: 'myData',
    selectedItems: $scope.mySelections,
    multiSelect: true,
    afterSelectionChange: function () {
      $scope.selectedIDs = [];
      angular.forEach($scope.mySelections, function ( item ) {
        $scope.selectedIDs.push( item.id )
      });
    }
  };

now you can reference {{selectedIDs}} from the template and has all the selected ids in it. Or just the first: {{selectedIDs[0]}}

现在,您可以从模板中引用{{selectedIDs}},并在其中包含所有选定的ID。或者只是第一个:{{selectedIDs [0]}}

See the working example here: http://plnkr.co/edit/xVwVWX

请参阅此处的工作示例:http://plnkr.co/edit/xVwVWX

#3


2  

You can access the rowItem of the selected row with the arguments to the afterSelectionChange event. The entity property will have the id and name properties.

您可以使用afterSelectionChange事件的参数访问所选行的rowItem。 entity属性将具有id和name属性。

 $scope.gridOptions = { 
    data: 'myData',
    selectedItems: $scope.mySelections,
    multiSelect: true,
    afterSelectionChange: function (theRow, evt) {      
       alert(theRow.entity.id);
}

};

#1


0  

Acutally, I want to get a cell value and modify when user selects the cell at the table.

实际上,我想得到一个单元格值并在用户选择表格中的单元格时进行修改。

Above answers are using fixed columns's filed.. like

上面的答案是使用固定列提交..喜欢

 $scope.selectedIDs.push( item.id ) // id is column filed

Instead of these answers, I found another way exactly what I want to achieve with.

我没有找到这些答案,而是找到了另一种我希望实现的方式。

Example code: http://plnkr.co/edit/wfiMhlJ7by4eUHojjKT4?p=preview

示例代码:http://plnkr.co/edit/wfiMhlJ7by4eUHojjKT4?p = preview

editableCellTemplate which is an option for columnDefs is used for solving this problem.

editableCellTemplate是columnDefs的一个选项,用于解决此问题。

Angular is Aswome!!

#2


5  

Use the afterSelectionChange callback to extract the ids from the selection to an other array.

使用afterSelectionChange回调将选择中的id提取到另一个数组。

$scope.gridOptions = { 
    data: 'myData',
    selectedItems: $scope.mySelections,
    multiSelect: true,
    afterSelectionChange: function () {
      $scope.selectedIDs = [];
      angular.forEach($scope.mySelections, function ( item ) {
        $scope.selectedIDs.push( item.id )
      });
    }
  };

now you can reference {{selectedIDs}} from the template and has all the selected ids in it. Or just the first: {{selectedIDs[0]}}

现在,您可以从模板中引用{{selectedIDs}},并在其中包含所有选定的ID。或者只是第一个:{{selectedIDs [0]}}

See the working example here: http://plnkr.co/edit/xVwVWX

请参阅此处的工作示例:http://plnkr.co/edit/xVwVWX

#3


2  

You can access the rowItem of the selected row with the arguments to the afterSelectionChange event. The entity property will have the id and name properties.

您可以使用afterSelectionChange事件的参数访问所选行的rowItem。 entity属性将具有id和name属性。

 $scope.gridOptions = { 
    data: 'myData',
    selectedItems: $scope.mySelections,
    multiSelect: true,
    afterSelectionChange: function (theRow, evt) {      
       alert(theRow.entity.id);
}

};