Saving row data with AngularJS ui-grid $scope.saveRow

流过昼夜 提交于 2019-12-03 03:31:53
punkologist

You would normally do the promise/api call in a separate repository, but basically the code you are looking for is something like this:

 $scope.saveRow = function( rowEntity ) {

    var promise = $scope.someRepositoryFunction(rowEntity);
    $scope.gridApi.rowEdit.setSavePromise($scope.gridApi.grid, rowEntity, promise);

    // fake a delay of 3 seconds whilst the save occurs, return error if gender is "male"
    //$interval( function() {
    //    if (rowEntity.firstName === 'male' ){
    //        promise.reject();
    //    } else {
    //        promise.resolve();
    //    }
    //}, 3000, 1);
};

$scope.someRepositoryFunction = function(row) {
    return $http.put('/Home/UpdateRow',row);
}

I'm not allowed to comment so I have to submit an answer. The answer above is incorrect in that the $scope.gridApi.grid parameter to teh setSavePromise function is NOT required. Although the function does require a grid parameter, it is added by the wrapper during the .apply call.

I had actually used the code submitted by punkologist and it was indeed working but there seemed to be errors in the console on resolving promise after including $interval.

I guess the following is the appropriate code to make it run error free. Hope it works.

$scope.saveRow = function( rowEntity ) {

var promise = $q.defer();
$http.put('/Home/UpdateRow',row).success(function(){
$interval(function(){
     promise.resolved();
     },3000, 1)
}).error(promise.reject);

$scope.gridApi.rowEdit.setSavePromise($scope.gridApi.grid, rowEntity, promise);
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!