将所选行更新为ng-grid

时间:2014-06-09 08:22:27

标签: angularjs data-binding selection ng-grid

我正在更新selectedItems的列表,但UI上的选择(复选框)没有针对相同的内容进行更新。

的index.html

<body ng-controller="MyCtrl">
    <div class="gridStyle" ng-grid="gridOptions"></div>

    <h3>Rows selected</h3>
    <pre>{{selectedRows}}</pre>
    <button ng-click="selectAdam()">Select Adam</button>
</body>

main.js

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
$scope.myData = [{name: "Sam", age: 50},
                 {name: "Peter", age: 43},
                 {name: "Jacob", age: 27},
                 {name: "Scott", age: 29},
                 {name: "Adam", age: 34}];
$scope.selectedRows = [];
$scope.gridOptions = { data: 'myData',
showSelectionCheckbox: true,
selectedItems: $scope.selectedRows
};
$scope.selectAdam = function() {
$scope.selectedRows.push({name: "Adam", age:34});
}
});

Plunkr for the code

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:3)

显然你就是这样做的(基于ngGrid的例子:

  $scope.selectAdam = function() {
    angular.forEach($scope.myData, function(data, index){
      if(data.name == 'Adam' && data.age == 34){
        $scope.gridOptions.selectItem(index, true);
      }
    });
  }

Working Plunker:http://plnkr.co/edit/K7b2ElQ1Z5uNAijFk7x4?p=info][1