4

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?

1 Answer 1

6

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();
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the update. but, i want to remove old columns and data once i click on rebind button action. Can you help me on this please?
i have updated the plunker and the above code, you can use $scope.gridOptions.columnDefs = new Array(); and then use $scope.gridOptions.data = data1;
thank you Vino; it solved my problem of re-assigning / re-binding columns dynamically.

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.