Updating selected rows into ng-grid

烂漫一生 提交于 2019-12-07 18:26:26

问题


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:


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



来源:https://stackoverflow.com/questions/24116302/updating-selected-rows-into-ng-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!