4

I am newbie in ng-grid. How do we dynamically update the columns and results inside the grid.

I have created a http://plnkr.co/edit/CwUVIzSKVNCMTgpOW87f?p=preview

Script

var app = angular.module('myApp', ['ngGrid']); app.controller('MyCtrl', function($scope) {
    $scope.myData = [{name: "Moroni", age: 50},
                     {name: "Tiancum", age: 43},
                     {name: "Jacob", age: 27},
                     {name: "Nephi", age: 29},
                     {name: "Enos", age: 34}];  
    $scope.gridOptions = { 
        data: 'myData',
        columnDefs: [{field: 'name', displayName: 'Name'}, {field:'age', displayName:'Age'}]
    };

    $scope.update_columns = function($event){ console.log("asd")
      $scope.myData = [{new_name: "Moroni", new_age: 50, pin: 123},
       {new_name: "Tiancum", new_age: 43, pin: 345},
       {new_name: "Jacob", new_age: 27, pin: 567},
       {new_name: "Nephi", new_age: 29, pin: 789},
       {new_name: "Enos", new_age: 34, pin: 012}
      ];

      $scope.gridOptions.columnDefs = [
        {field: 'new_name', displayName: 'New Name'}, 
        {field:'new_age', displayName:'New Age'},
        {field:'pin', displayName:'Pin'}
      ];

    } });

On page load, the grid will display a set of result and on clicking the change columns, we need to update the results and columns. The way I have implemented right now is not working as expected. Am i doing something wrong?

Thanks in advance.

2 Answers 2

17

You must set up your column definition variations on the scope. You can also define the configuration property columnDefs with a string that represents a scope property. Like the configuration of data, this allows you to watch the scope field and switch it out.

Here is an updated Plunker.

Changes to the js:

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
    $scope.columns1 = [{field: 'name', displayName: 'Name'}, {field:'age', displayName:'Age'}];
    $scope.columns2 = [{field: 'new_name', displayName: 'New Name'}, {field:'new_age', displayName:'New Age'},{field:'pin', displayName:'Pin'}];
    $scope.columnsSelected = $scope.columns1;
    $scope.myData = [{name: "Moroni", age: 50},
                     {name: "Tiancum", age: 43},
                     {name: "Jacob", age: 27},
                     {name: "Nephi", age: 29},
                     {name: "Enos", age: 34}];  
    $scope.gridOptions = { 
        data: 'myData',
        columnDefs: 'columnsSelected'
    };

    $scope.update_columns = function($event) { 

      $scope.columnsSelected = $scope.columns2;

      $scope.myData = [{new_name: "Moroni", new_age: 50, pin: 123},
       {new_name: "Tiancum", new_age: 43, pin: 345},
       {new_name: "Jacob", new_age: 27, pin: 567},
       {new_name: "Nephi", new_age: 29, pin: 789},
       {new_name: "Enos", new_age: 34, pin: 012}
      ];
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Stumbled across the same issue. this works like a charm! Thanks !
1

You can actually use the same data set since Angular ignores columns that aren't defined in the columnDefs / columnsSelected:

app.controller('MyCtrl', function($scope) {
$scope.columns = [];
$scope.columns[true] = [{field: 'name', displayName: 'Name'}, {field:'age', displayName:'Age'}];
$scope.columns[false] = [{field: 'name', displayName: 'New Name'}, {field:'age', displayName:'New Age'},{field:'pin', displayName:'Pin'}];
$scope.displayFlag = true;
$scope.columnsSelected = $scope.columns[$scope.displayFlag];
  $scope.myData = [
   {name: "Moroni", age: 50, pin: 123},
   {name: "Tiancum", age: 43, pin: 345},
   {name: "Jacob", age: 27, pin: 567},
   {name: "Nephi", age: 29, pin: 789},
   {name: "Enos", age: 34, pin: 012}
  ]; 
$scope.gridOptions = { 
    data: 'myData',
    columnDefs: 'columnsSelected'
};

$scope.update_columns = function($event) { 
  $scope.displayFlag = !$scope.displayFlag;
  $scope.columnsSelected = $scope.columns[$scope.displayFlag];
}

});

http://plnkr.co/edit/y5PXl78h653VCWtIMers?p=preview

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.