检测angular.js和ng-grid中的单元格变化

时间:2022-08-24 11:25:15

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.

我绞尽脑汁但却无法弄清楚如何检测ng-grid中的细胞数据变化。下面的代码片段正在使用ng-change,它正确调用save(),但它不是我想要的触发器,因为它被调用任何键控条目。我需要知道单元格的编辑何时完成。

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);
    }
}]);

6 个解决方案

#1


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 );
});

#2


4  

You should be able to listen for the ngGridEventEndCellEdit event:

您应该能够监听ngGridEventEndCellEdit事件:

$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.

这在以下网址中没有详细描述: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 * 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

或者,*上的这个问题似乎有一个很好的答案,涉及ng-blur指令:AngularJS和ng-grid - 在单元格更改后自动将数据保存到服务器

#3


4  

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

如果你正在使用UI Grid 3.0或4.x,你应该等待:uiGridEventEndCellEdit

$scope.$on('uiGridEventEndCellEdit', function (data) {
    console.log(data.targetScope.row.entity);
});

#4


1  

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

我希望这会对某人有所帮助。我也需要ngGridEventEndCellEdit事件中的网格名称。

using jquery inside the function:

在函数内部使用jquery:

$scope.$on('ngGridEventEndCellEdit', function (data) {
var gridName = $('.' + data.targetScope.gridId).attr('ng-grid');
});

#5


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});
      });
    });
  }
}]);

#6


0  

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

对于ui-grid 3.0.6,我确实使用了afterCellEdit事件。

 $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
        }

      });

}

#1


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 );
});

#2


4  

You should be able to listen for the ngGridEventEndCellEdit event:

您应该能够监听ngGridEventEndCellEdit事件:

$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.

这在以下网址中没有详细描述: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 * 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

或者,*上的这个问题似乎有一个很好的答案,涉及ng-blur指令:AngularJS和ng-grid - 在单元格更改后自动将数据保存到服务器

#3


4  

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

如果你正在使用UI Grid 3.0或4.x,你应该等待:uiGridEventEndCellEdit

$scope.$on('uiGridEventEndCellEdit', function (data) {
    console.log(data.targetScope.row.entity);
});

#4


1  

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

我希望这会对某人有所帮助。我也需要ngGridEventEndCellEdit事件中的网格名称。

using jquery inside the function:

在函数内部使用jquery:

$scope.$on('ngGridEventEndCellEdit', function (data) {
var gridName = $('.' + data.targetScope.gridId).attr('ng-grid');
});

#5


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});
      });
    });
  }
}]);

#6


0  

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

对于ui-grid 3.0.6,我确实使用了afterCellEdit事件。

 $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
        }

      });

}