KB. KB. - 3 months ago 146
AngularJS Question

AngularJS ui-grid, make column editable conditionally

I am using AngularJS 1.5.5 and ui-grid 3.2.6.

I have a grid with 5 columns.Col "4" is the only column editable with a dropdownEditor, all the other columns are non editable.

What i need is, if the value in col 4 changes, then i need to make column 5 editable.

I am familier with cellEditableCondition. But, i am not sure how i can use that property to meet the requirement.

Answer

You can use a temporary property on the row entity to track column 4 changes.

var app = angular.module('app', ['ngTouch', 'ui.grid','ui.grid.edit']);
var grid;
app.controller('MainCtrl', ['$scope', function ($scope) {

  var myData = [
    {
      id1:"1",id2:"2",id3:"3",id4:"4",id5:"5",
    },]

  var cellEditable = function($scope){
    if($scope.row.entity.oldId4===undefined)
      return false;
    return $scope.row.entity.oldId4!=$scope.row.entity.id4;
  }

  $scope.gridOptions = {
        enableFiltering: true,
         onRegisterApi: function(gridApi){
      grid = gridApi;
    },
    data: myData,
    columnDefs:[
        {
          field: 'id1',
          displayName: "id1",
          width: 100,

         enableCellEdit:true,
          cellEditableCondition : cellEditable
        },
        {
          field: 'id2',
          displayName: "id2",
          width: 100,

         enableCellEdit:true,
          cellEditableCondition : cellEditable
        },{
          field: 'id3',
          displayName: "id3",
          width: 100,
          enableCellEdit:true,
          cellEditableCondition : cellEditable
        },{
          field: 'id4',
          displayName: "id4",
          width: 100,
         enableCellEdit:true,
        },{
          field: 'id5',
          displayName: "id5",
          width: 100,
         enableCellEdit:true,
          cellEditableCondition : cellEditable
        },

    ],
}
 $scope.gridOptions.onRegisterApi = function(gridApi){
          //set gridApi on scope
          $scope.gridApi = gridApi;
          gridApi.edit.on.afterCellEdit($scope,function(rowEntity, colDef, newValue, oldValue){
            rowEntity.oldId4 = oldValue;
            $scope.$apply();
          });
        };

 $scope.test = function()
 {
   window.alert("Cell clicked")
 }
}]);

Here is a working plnkr. http://plnkr.co/edit/wJfPkcBmpseaJjw20ct9?p=preview