I have this plunkr here, which displays an editable table.
Following is the HTML code for table:
<body ng-controller="MainCtrl">
<table style="width:100%">
<tr>
<th>Name</th>
<th>Is enabled?</th>
<th>Points</th>
</tr>
<tr ng-repeat="fooObject in fooObjects | orderBy:'points'">
<td><input ng-model="fooObject.name" ng-disabled="fooState!='EDIT'"/></td>
<td><input ng-model="fooObject.isEnabled" ng-disabled="fooState!='EDIT'"/></td>
<td><input ng-model="fooObject.points" ng-disabled="fooState!='EDIT'"/></td>
<td>
<a href="#" ng-click="handleEdit(fooObject, 'EDIT', $index)">Edit</a>
<a href="#" ng-click="handleEditCancel(fooObject, 'VIEW', $index)">Cancel</a>
</td>
</tr>
</table>
</body>
I would like the Cancel
link in the row display the previous state of the fooObject
as if that row was never touched.
Following is the code in the AngularJS controller, which seems to work as long as I don't have "orderBy:'points'"
in the ng-repeat
expression, but doesn't work otherwise:
app.controller('MainCtrl', function($scope) {
$scope.fooObjects = [
{"name": "mariofoo", "points": 65, "isEnabled": true},
{"name": "supermanfoo", "points": 47, "isEnabled": false},
{"name": "monsterfoo", "points": 85, "isEnabled": true}
];
$scope.fooState = 'VIEW';
$scope.originalFooObject = null;
$scope.handleEdit = function(fooObject, fooState, index){
$scope.originalFooObject = angular.copy(fooObject);
$scope.fooObject = fooObject;
$scope.fooState = fooState;
}
$scope.handleEditCancel=function(fooObject, fooState, index){
$scope.fooObjects[index] = $scope.originalFooObject;
$scope.originalFooObject=null;
$scope.fooState = fooState;
}
});
Could somebody help me understand how get it resolved?