admin管理员组文章数量:1413820
I'm trying to use the row saving feature in bination with the expandable grid. The goal is to be able to save sub-grid rows, independently of the parent row.
$scope.gridOptions = {
expandableRowTemplate: 'ponents/grid/orderLineTemplate.html',
expandableRowHeight: 150,
expandableRowScope: {
subGridVariable: 'subGridScopeVariable'
},
columnDefs: [
{field: '_id'},
{field: 'number'}
]
};
$http.get(ORDER_API)
.success(function (data) {
for (var i = 0; i < data.length; i++) {
var rowScope = data[i];
rowScope.subGridOptions = {
appScopeProvider: $scope,
columnDefs: [
{field: 'amount'},
{field: 'packageAmount'},
{field: 'carrierAmount'}
],
data: rowScope.orderLines,
saveRow : $scope.saveRow
}
}
$scope.gridOptions.data = data;
});
$scope.gridOptions.onRegisterApi = function (gridApi) {
$scope.gridApi = gridApi;
gridApi.rowEdit.on.saveRow($scope, $scope.saveRow);
};
$scope.saveRow = function (order) {
var promise = $q.defer();
$scope.gridApi.rowEdit.setSavePromise(order, promise.promise);
if(order.number) {
$http.put(ORDER_API + '/' + order._id, order).success(function () {
promise.resolve();
}).error(function () {
promise.reject();
});
}
}
});
The saveRow function is called correctly when I edit a field in the parent row. When I edit a field in the sub-row, the following message appears in the console;
'A promise was not returned when saveRow event was raised, either nobody is listening to event, or event handler did not return a promise'
SaveRow is never called for the expanded sub-row.
I'm trying to use the row saving feature in bination with the expandable grid. The goal is to be able to save sub-grid rows, independently of the parent row.
$scope.gridOptions = {
expandableRowTemplate: 'ponents/grid/orderLineTemplate.html',
expandableRowHeight: 150,
expandableRowScope: {
subGridVariable: 'subGridScopeVariable'
},
columnDefs: [
{field: '_id'},
{field: 'number'}
]
};
$http.get(ORDER_API)
.success(function (data) {
for (var i = 0; i < data.length; i++) {
var rowScope = data[i];
rowScope.subGridOptions = {
appScopeProvider: $scope,
columnDefs: [
{field: 'amount'},
{field: 'packageAmount'},
{field: 'carrierAmount'}
],
data: rowScope.orderLines,
saveRow : $scope.saveRow
}
}
$scope.gridOptions.data = data;
});
$scope.gridOptions.onRegisterApi = function (gridApi) {
$scope.gridApi = gridApi;
gridApi.rowEdit.on.saveRow($scope, $scope.saveRow);
};
$scope.saveRow = function (order) {
var promise = $q.defer();
$scope.gridApi.rowEdit.setSavePromise(order, promise.promise);
if(order.number) {
$http.put(ORDER_API + '/' + order._id, order).success(function () {
promise.resolve();
}).error(function () {
promise.reject();
});
}
}
});
The saveRow function is called correctly when I edit a field in the parent row. When I edit a field in the sub-row, the following message appears in the console;
'A promise was not returned when saveRow event was raised, either nobody is listening to event, or event handler did not return a promise'
SaveRow is never called for the expanded sub-row.
3 Answers
Reset to default 3 +50You need to register the subgrid APIs. Each grid has its own separate API instance that you use to municate with it:
rowScope.subGridOptions = {
appScopeProvider: $scope,
columnDefs: [
{field: 'amount'},
{field: 'packageAmount'},
{field: 'carrierAmount'}
],
data: rowScope.orderLines,
saveRow : $scope.saveRow,
onRegisterApi: function (gridApi) {
gridApi.rowEdit.on.saveRow($scope, $scope.saveRow)
}
}
That's close, but you're injecting our controller scope into the subgrid scope with appScopeProvider, which you don't really need to do. Instead, we can make saveRow generic and bind it to the gridApi we want. The first argument of bind()
sets the this
for the function. We'll just pass in the grid object, but we won't need it. The second argument to bind will be the gridApi we want to pass. Then in the saveRow definition we know we'll receive the right API as the first argument, and then the rowEntity as the second arg.
// Main grid:
$scope.gridOptions.onRegisterApi = function(gridApi) {
gridApi.rowEdit.on.saveRow($scope, saveRow.bind(gridApi.grid, gridApi));
};
// Subgrids:
onRegisterApi: function(gridApi) {
gridApi.rowEdit.on.saveRow($scope, saveRow.bind(gridApi.grid, gridApi));
}
// Altered saveRow:
function saveRow(gridApi, rowEntity) {
var promise = $q.defer();
gridApi.rowEdit.setSavePromise( rowEntity, promise.promise );
// fake a delay of 3 seconds whilst the save occurs, return error if gender is "male"
$interval( function() {
if (rowEntity.gender === 'male' ){
promise.reject();
} else {
promise.resolve();
}
}, 3000, 1);
};
Since you'll probably have a different save function for your subgrids the main thing to remember is to register the "saveRow" event on them all with onRegisterApi
Here's a working plunker demonstrating the code above: http://plnkr.co/edit/52mp9C?p=preview
If you use Angular $http or $resource, you needn't create any other 'deferred' objects, just return result:
$scope.saveRow = function (order) {
// with use $http
var promise = $http.put(ORDER_API + '/' + order._id, order);
// or with use $resource
var promise = $resource(ORDER_API + '/:id').save({ id: order._id }, order).$promise;
$scope.gridApi.rowEdit.setSavePromise(order, promise);
return promise;
}
You already created a deferred promise in you code. As error clearly says you need to return a promise, You should add return deferred.promise;
to your code. I think you should also return a promise from else statement in order to get promise resolve/reject anyhow.
Code
$scope.saveRow = function(order) {
var deferred = $q.defer();
$scope.gridApi.rowEdit.setSavePromise(order, promise.promise);
if (order.number) {
$http.put(ORDER_API + '/' + order._id, order).success(function() {
deferred.resolve();
}).error(function() {
deferred.reject();
});
} else {
deferred.reject();
}
return deferred.promise; //this will return promise to caller function.
};
Hope this could help you, let me know if anything else is required. Thanks. :)
本文标签: javascriptNot able to save row in Expandable row in Angular UI GridStack Overflow
版权声明:本文标题:javascript - Not able to save row in Expandable row in Angular UI Grid - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744577322a2613705.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论