Updating selected rows into ng-grid

2019-05-22 20:38发布

I am updating the list of selectedItems but the selections (checkboxes) on the UI are not getting updated for the same.

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

Am I missing something here?

1条回答
叼着烟拽天下
2楼-- · 2019-05-22 20:45

Apparently this is how you do it (based on the example at 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

查看更多
登录 后发表回答