我正在使用AngularJS来显示应用程序密钥表(应用程序标识符),我想使用编辑按钮在该表行中显示一个小表单。然后用户可以编辑字段并单击“保存”或“取消”
我的内联表格效果很好。我单击编辑并显示一个表单。取消也很有效。
我的问题是
editMode
?这是我在我的控制器中使用的实际代码(在JSFiddle我无法进行http调用)。第一个$ http填写表单,editAppKey函数是保存按钮调用的函数。
function AppKeysCtrl($scope, $http, $location) {
$http({
method: 'POST',
url: 'http://' + $location.host() + ':1111/sys/appkey/save',
data: {
// How do I get the data?
}
}).
success(function(data, status, headers, config) {
$scope.appkeys = data;
}).
error(function(data, status, headers, config) {
$scope.appkeys = [{ "appkey" : "ERROR", "name" : "ERROR", "created" : "ERROR" }];
});
$scope.editAppKey = function() {
$http({
method: 'POST',
url: 'http://' + $location.host() + ':1111/sys/appkeys'
}).
success(function(data, status, headers, config) {
alert("Success!");
$scope.editMode = false;
}).
error(function(data, status, headers, config) {
alert("There was an error.");
});
}
}
答案 0 :(得分:23)
当我们按下“编辑”按钮并更改其中一个字段时,我们也会更改主模型appkeys
。它意味着在“取消”我们需要恢复旧模型。
它的意思是我们至少需要:
所以这是HTML的片段:
<td>
<button type="submit" data-ng-hide="editMode" data-ng-click="editMode = true; editAppKey(entry)" class="btn btn-default">Edit</button>
<button type="submit" data-ng-show="editMode" data-ng-click="editMode = false; saveField()" class="btn btn-default">Save</button>
<button type="submit" data-ng-show="editMode" data-ng-click="editMode = false; cancel()" class="btn btn-default">Cancel</button>
</td>
我们的控制器:
$scope.newField = {};
$scope.editing = false;
$scope.appkeys = [
{ "appkey" : "0123456789", "name" : "My new app key", "created" : tmpDate },
{ "appkey" : "abcdefghij", "name" : "Someone elses app key", "created" : tmpDate }
];
$scope.editAppKey = function(field) {
$scope.editing = $scope.appkeys.indexOf(field);
$scope.newField = angular.copy(field);
}
$scope.saveField = function() {
if ($scope.editing !== false) {
$scope.appkeys[$scope.editing] = $scope.newField;
$scope.editing = false;
}
};
$scope.cancel = function() {
if ($scope.editing !== false) {
$scope.appkeys[$scope.editing] = $scope.newField;
$scope.editing = false;
}
};
演示 Fiddle
<强> [编辑] 强>
我想一次编辑多行,使用newFields
数组代替$scope.newField
答案 1 :(得分:4)
你可以通过例如current index作为editAppKey()函数的参数:
... data-ng-click="editAppKey($index)"
并在JS文件中:
$scope.editAppKey = function(index) {
window.console.log(appkeys[index]); // do what ever you want
}
请求返回后禁用。如果我不准备,你想只允许一次编辑,并且在某行上调用editAppKey()一次之后,禁用它,对吧?如果是这样,可能会像
那样<button type="submit" data-ng-hide="editMode" data-ng-click="editMode = true" class="btn btn-default"
data-ng-disabled="entry.isDisabled">Edit</button>
并在editAppKey()函数中,类似
$scope.editAppKey = function(index){
$http.post(url, data).onsuccess(function(){
$scope.appkeys[index].isDisabled = true;
});
答案 2 :(得分:2)
如果有人需要一次进行多次编辑:
请执行以下操作:
在html取消按钮上,传递索引
data-ng-click="editMode = false; cancel($index)"
:
1)$scope.newField = {};
至$scope.newField = [];
2)在editAppKey
函数内,$scope.newField = angular.copy(field);
到$scope.newField[$scope.editing] = angular.copy(field);
3)将saveField
功能更改为:
$scope.saveField = function(index) {
$scope.appkeys[$scope.editing] = $scope.newField;
};
4)将cancel
功能更改为:
$scope.cancel = function(index) {
$scope.appkeys[index] = $scope.newField[index];
$scope.editing = false;
};