NewBuddy
NewBuddy

Reputation: 55

angularjs UI grid - assign columns and data dynamically

I am trying to add columns dynamically to the ui-grid and also assigning the data. First time, the grid is working good. But, when i try to change the columns and data dynamically, it is not working as expected.

$scope.myfunc = function() {

alert("Rebinding the data");
$scope.gridOptions = {};

$scope.gridOptions.columnDefs.push({
 name: 'firstName'
  });
  $scope.gridOptions.columnDefs.push({
 name: 'lastName'
  });
  $scope.gridOptions.columnDefs.push({
 name: 'company'
  });
  $scope.gridOptions.columnDefs.push({
 name: 'employed'
  });

  alert("added new columns");
  $scope.gridOptions.data = data1;

  $scope.gridApi.grid.refresh();
};

please check the plunkr

Can anybody look into this issue and suggest me how to do this?

Upvotes: 4

Views: 17876

Answers (1)

Python Basketball
Python Basketball

Reputation: 2370

i think you can remove the code: $scope.gridOptions = {};

the latest plunker

the second data1 should be pushed into the first data

I have changed the plunker with the latest code, pls check it!

 $scope.myfunc = function()
  {

    alert("Rebinding the data");
     $scope.gridOptions.columnDefs = new Array();

    $scope.gridOptions.columnDefs.push({
     field: 'firstName'
      });
      $scope.gridOptions.columnDefs.push({
     field: 'lastName'
      });
      $scope.gridOptions.columnDefs.push({
     field: 'company'
      });
      $scope.gridOptions.columnDefs.push({
     field: 'employed'
      });

      alert("added new columns");
      $scope.gridOptions.data = data1;

     /* for(var i=0; i<$scope.gridOptions.data.length; i++){
        $scope.gridOptions.data[i].firstName = data1[i].firstName;
        $scope.gridOptions.data[i].lastName = data1[i].lastName;
        $scope.gridOptions.data[i].company = data1[i].company;
        $scope.gridOptions.data[i].employed = data1[i].employed;
      } */


      $scope.gridApi.grid.refresh();
  };

if you want to remove the old columns, you would use

$scope.gridOptions.columnDefs = new Array();

and then refresh data using:

$scope.gridOptions.data = data1;

$scope.gridApi.grid.refresh();

Upvotes: 6

Related Questions