以角度ui.grid动态更改网格选项

时间:2015-04-16 14:51:43

标签: angularjs ng-grid angular-ui-grid

我必须显示两种类型的gridOptions,而不使用两个网格试图动态更改gridOptions不工作(一次工作)。

工作示例http://plnkr.co/edit/4QKGIB?p=preview

$scope.grid1=function(){
   $scope.gridOptions = $scope.gridOptions1;
   $scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.ALL);
 }
 $scope.grid2=function(){
   $scope.gridOptions = $scope.gridOptions2;
   $scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.ALL);
 }

1 个答案:

答案 0 :(得分:5)

基本上,您需要在将columnDef分配给网格时使用angular.copy(),网格会克隆数组并将其设置为gridOptions

<强>代码

 $scope.gridOptions = angular.copy($scope.gridOptions1);
 $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/500_complex.json')
 .success(function(data) {
   $scope.gridOptions1.data = data;
   $scope.gridOptions2.data = data;
   $scope.grid1();
 });
 $scope.grid1=function(){
   $scope.gridOptions = angular.copy($scope.gridOptions1);
   $scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.ALL);
 }
 $scope.grid2=function(){
   $scope.gridOptions = angular.copy($scope.gridOptions2);
   $scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.ALL);
 }

Working Plunkr