2017-03-07 43 views
2

我有一個應用程序,我繼承了這個代碼:是否有可能導致UI-Grid的afterCellEdit人爲觸發?

onRegisterApi: function(gridApi) { 
    $scope.gridApi = gridApi; 
    $scope.gridApi.edit.on.afterCellEdit($scope, 
     function(rowEntity, colDef, newValue, oldValue) { 
    } 
} 

我已經更新了gridOptions這是結合數據對象的UI電網等我想afterCellEdit回調火這使得更新單元數據。

這可能嗎?

+0

你可以將未命名的函數替換爲一個已命名的函數,然後你可以調用該命名函數來執行任何操作。 – Feathercrown

+1

我的答案在下面有幫助嗎?還有什麼我可以幫忙的嗎? –

回答

1

在回答你的問題,這是你將怎樣使UI-Grid的afterCellEdit人爲火的時候,任何網格數據網格的外部更新中...

var app = angular.module('app', ['ui.grid', 'ui.grid.edit', 'ui.grid.cellNav']); 
 
app.controller('MainCtrl', ['$scope', '$timeout', function($scope, $timeout) { 
 
    $scope.gridData = [{"FirstName": "Matt", "LastName": "W", "Job": "Stack Overflow User"}, 
 
        {"FirstName": "Tim", "LastName": "Harker", "Job": "Stack Overflow User"}]; 
 
    $scope.$watch('gridData', function(newValues, oldValues) { 
 
    for (var i = 0; i < newValues.length; i++) { 
 
     if (!angular.equals(newValues[i], oldValues[i])) { 
 
     for (var property in oldValues[i]) { 
 
      if (oldValues[i].hasOwnProperty(property)) { 
 
      if (oldValues[i][property] != newValues[i][property]) { 
 
       $scope.gridApi.edit.raise.afterCellEdit(newValues[i], null, newValues[i][property], $scope.gridOptions.data[i][property]); 
 
      } 
 
      } 
 
     } 
 
     } 
 
    } 
 
    $timeout(function() { 
 
     $scope.gridOptions.data = angular.copy($scope.gridData); 
 
    }); 
 
    }, true); 
 
    $scope.callbackData = {}; 
 
    $scope.gridOptions = { 
 
    enableCellEditOnFocus: true, 
 
    onRegisterApi: function(gridApi) { 
 
     $scope.gridApi = gridApi; 
 
     $scope.gridApi.edit.on.afterCellEdit($scope, 
 
     function(rowEntity, colDef, newValue, oldValue) { 
 
      $scope.callbackData.oldValue = oldValue; 
 
      $scope.callbackData.newValue = newValue; 
 
     }); 
 
    }, 
 
    columnDefs: [{name: 'FirstName'}, {name: 'LastName'}, {name: 'Job'}] 
 
    }; 
 
}]);
button { 
 
    margin-bottom: 10px; 
 
} 
 

 
div[ui-grid] { 
 
    height: 115px; 
 
    margin-bottom: 10px; 
 
}
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script> 
 
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-grid/4.0.2/ui-grid.min.js"></script> 
 
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/angular-ui-grid/4.0.2/ui-grid.min.css" /> 
 
<div ng-app="app" ng-controller="MainCtrl"> 
 
    <button ng-click="gridData[0].LastName='Williams'">Guess Last Name</button> 
 
    <div ui-grid="gridOptions" ui-grid-edit ui-grid-cellNav></div> 
 
    <div>{{callbackData}}</div> 
 
</div>

設我知道你是否還有其他問題。

相關問題