Detecting cell changes in angular.js and ng-grid
Asked Answered
P

6

5

I've racked my brain but am unable to figure out how to detect the cell data change in ng-grid. The following snippet is using ng-change which does correctly call save(), but it's not the trigger I want since it gets called for any keyed entry. I need to know when the editing of the cell is complete.

Any help would be appreciated.

angular.module('controllers', ['ngGrid']).
controller('ContactsListCtrl', ['$scope', 'Contacts', function ($scope, Contacts) {
    var editableCellTemplate = '<input type="text" ng-class="\'colt\' + col.index" ng-input="COL_FIELD" ng-model="COL_FIELD" ng-change="save()"/>';

    Contacts.get(function(data) {
        $scope.contacts = data;

    });
    $scope.gridOptions = {
        data: 'contacts',
        enableCellSelection: true,
        enableRowSelection: false,
        enableCellEdit: true,
        showSelectionCheckbox: true,
        columnDefs: [
            {field: 'lastName', displayName: 'Last Name', enableCellEdit: true, editableCellTemplate: editableCellTemplate},
            {field: 'firstName', displayName: 'First Name', enableCellEdit: true, editableCellTemplate: editableCellTemplate},
            {field: 'email', displayName: 'EMail Address', enableCellEdit: true, editableCellTemplate: editableCellTemplate},
            {field: 'phone', displayName: 'Phone', enableCellEdit: true, editableCellTemplate: editableCellTemplate}
        ]
    };
    $scope.save = function() {
        $scope.contact = this.row.entity;
        Contacts.save($scope.contact);
    }
}]);
Parrie answered 14/8, 2013 at 18:52 Comment(0)
A
5

This should do the trick, and give you your complete edited row, when one the cell has been edited. Which you can then save / update

$scope.$on('ngGridEventEndCellEdit', function(event) {
    $scope.contact = event.targetScope.row.entity;
    Contacts.save($scope.contact);
    // console.log($scope.contact );
});
Aquarelle answered 13/12, 2013 at 17:27 Comment(2)
if you have two (or more) grids in the same scope, how do you know which of these grids fired the ngGridEventEndCellEdit event?Mountbatten
Using 'event.targetScope.gridId' you can identify which grid has emitted this event.Amitosis
L
5

If you're using UI Grid 3.0 OR 4.x you should wait for: uiGridEventEndCellEdit

$scope.$on('uiGridEventEndCellEdit', function (data) {
    console.log(data.targetScope.row.entity);
});
Levo answered 21/4, 2015 at 0:58 Comment(0)
M
4

You should be able to listen for the ngGridEventEndCellEdit event:

$scope.$on('ngGridEventEndCellEdit', function(data) {
  console.log(data);
});

This is described in not much detail at: https://github.com/angular-ui/ng-grid/wiki/Grid-Events.

Unfortunately I haven't worked out yet how this event tells me which row/cell we've finished editing, so that I can save it. But it's perhaps a start.

Alternatively, this question on stackoverflow seems to have a good answer that involves an ng-blur directive: AngularJS and ng-grid - auto save data to the server after a cell was changed

Metcalfe answered 5/12, 2013 at 7:49 Comment(0)
R
1

i hope this will help someone. I too needed the name of the grid in the ngGridEventEndCellEdit event.

using jquery inside the function:

$scope.$on('ngGridEventEndCellEdit', function (data) {
var gridName = $('.' + data.targetScope.gridId).attr('ng-grid');
});
Renoir answered 5/1, 2015 at 16:40 Comment(0)
D
0

You could create a blur directive and call the save function when the input loses focus.

app.directive('ngBlur', ['$parse', function($parse) {
  return function(scope, element, attr) {
    var fn = $parse(attr['ngBlur']);
    element.bind('blur', function(event) {
      scope.$apply(function() {
        fn(scope, {$event:event});
      });
    });
  }
}]);
Droopy answered 14/8, 2013 at 19:1 Comment(0)
M
0

For ui-grid 3.0.6 I did use the afterCellEdit event.

 $scope.gridOptions.onRegisterApi = function (gridApi) {
      $scope.gridApi = gridApi;

      gridApi.edit.on.afterCellEdit($scope,function(rowEntity, colDef,newValue,oldValue){

        if(newValue != oldValue){
          // Do something.......
          // colDef.field has the name of the column that you are editing
        }

      });

}
Meli answered 7/10, 2015 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.