动态更改 angular ui.grid 中的网格选项

Dynamically Change the Grid Options in 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);
 }

基本上,您需要在将 columnDef 分配给 grid 时使用 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