我正在研究ui网格编辑单元格功能。我需要使用rest api将编辑的单元格值更新到数据库。另外,我如何获得在控制器中选择的行列表。
我的工作代码
var app = angular.module('app', ['ngTouch', 'ui.grid', 'ui.grid.edit']);
app.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.gridOptions = { };
$scope.gridOptions.columnDefs = [
{ name: 'id', enableCellEdit: false},
{ name: 'name' },
{ name: 'age', displayName: 'Age' , type: 'number', width: '10%' }
];
$http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/500_complex.json')
.success(function(data) {
$scope.gridOptions.data = data;
});
}])
答案 0 :(得分:18)
将以下内容添加到您的控制器:
$scope.gridOptions.onRegisterApi = function(gridApi) {
//set gridApi on scope
$scope.gridApi = gridApi;
gridApi.edit.on.afterCellEdit($scope, function(rowEntity, colDef, newValue, oldValue) {
//Do your REST call here via $http.get or $http.post
//Alert to show what info about the edit is available
alert('Column: ' + colDef.name + ' ID: ' + rowEntity.id + ' Name: ' + rowEntity.name + ' Age: ' + rowEntity.age);
});
};
您拥有有关编辑哪个列的所有信息(在colDef.name
中)以及单元格的实际值(在rowEntity.xxx
中)。
您现在要做的就是调用您的REST API(为了避免不必要的流量,您还可以将newValue
与oldValue
进行比较,看看内容是否真的被更改了。)
您不需要重新加载数据,因为更改已应用于范围。
在此处找到forked Plunker。
问题的第二部分:
您的所有行都不可选。这可能会有点复杂。请就此问题开始一个新问题(使用新的Plunker)。