AngularJS and ng-grid - auto save data to the server after a cell was changed

前端 未结 7 1663
悲哀的现实
悲哀的现实 2020-12-02 08:51

My Use Case is pretty simple. A User, after editing a Cell (enableCellEdit: true), should have the data \"automatically\" sent to the server (on cell blur). I tried differen

7条回答
  •  没有蜡笔的小新
    2020-12-02 09:18

    I've spent some time pulling together the bits of this for ng-grid 2.x. I still have a problem with having to click twice to edit a row, but I think that's a bootstrap issue, not an ngGrid issue, it doesn't happen in my sample code (which doesn't have bootstrap yet).

    I've also implemented similar logic in a tutorial for ui-grid 3.0, which is still beta but will soon become the preferred version. This can be found at: http://technpol.wordpress.com/2014/08/23/upgrading-to-ng-grid-3-0-ui-grid/, and provides a much easier and cleaner api for this functionality.

    For the 2.x version, to illustrate all the bits, I've created a running plunker that has an editable grid with both a dropdown and an input field, uses the ngBlur directive, and uses a $timeout to avoid duplicate saves on the update: http://plnkr.co/edit/VABAEu?p=preview

    The basics of the code are:

    var app = angular.module('plunker', ["ngGrid"]);
    
    app.controller('MainCtrl', function($scope, $timeout, StatusesConstant) {
      $scope.statuses = StatusesConstant;
      $scope.cellInputEditableTemplate = '';
      $scope.cellSelectEditableTemplate = '

    actually it needs to be:

  • To trigger a save event when we lose focus, we've created an blur directive, the logic for which I found in stackoverflow: AngularJS and ng-grid - auto save data to the server after a cell was changed

  • This also means changing each editable cell template to call ng-blur, which you can see at the end of the editable cell template

  • We get two blur events when we leave the field (at least in Chrome), so we use a timer so that only one of them is processed. Ugly, but it works.

  • I've also created a blog post that does a more thorough walkthrough of this code: http://technpol.wordpress.com/2013/12/06/editable-nggrid-with-both-dropdowns-and-selects/

提交回复
热议问题