Search code examples
angularjsangular-ui-gridui-grid

How to cancel cell editing in Angular ui-grid?


I have some condition in beginCellEdit event

       gridApi.edit.on.beginCellEdit($scope, function(rowEntity, colDef, event) {
            var scope = angular.element(event.currentTarget).scope();
            var selectedRowIndex = scope.rowRenderIndex;
            var selectedColumnName = scope.col.name;

            if ($scope.gridOptions.data[selectedRowIndex-1][selectedColumnName].trim()=='') {      
                alert("You cannot enter the value in this cell");
                // At this place I want to cancel edit and return focus to grid
            } 
        })

Is there any preventDefault or similar method so can easily cancel the cell editing and return focus to grid.

Thank you!


Solution

  • You can have a look at this plunker example to have conditional editable field.

    Controller:

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

    Gridoptions -> columnDefs:

    cellEditableCondition : cellEditable