Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting cell changes in angular.js and ng-grid

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);
    }
}]);
like image 685
Rob Callahan Avatar asked Aug 14 '13 18:08

Rob Callahan


3 Answers

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 );
});
like image 193
John Smith Avatar answered Nov 06 '22 03:11

John Smith


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);
});
like image 5
Leandro Coutinho Avatar answered Nov 06 '22 02:11

Leandro Coutinho


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

like image 4
PaulL Avatar answered Nov 06 '22 03:11

PaulL