I have a table that displays items and allows inline editing with the ability to save or discard changes.
Each table row contains columns that show the content of the item when the row is not being edited, and an input for editing when the row is on edit mode.
Here's the HTML:
<table class="table table-bordered">
<thead>
<tr>
<th>Field A</th>
<th>Field B</th>
<th>Field C</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items">
<td>
<span ng-show="editingItem !== item">
{{item.fieldA}}
</span>
<input type="text" ng-model="item.fieldA" class="form-control" ng-show="editingItem === item" />
</td>
<td>
<span ng-show="editingItem !== item">
{{item.fieldB}}
</span>
<input type="text" ng-model="item.fieldB" class="form-control" ng-show="editingItem === item" />
</td>
<td>
<span ng-show="editingItem !== item">
{{item.fieldC}}
</span>
<input type="text" ng-model="item.fieldC" class="form-control" ng-show="editingItem === item" />
</td>
<td>
<i ng-click="startEditItem(item)" ng-show="editingItem !== item" class="fa fa-pencil link text-primary"></i>
<i ng-click="confirmEditItem(item)" ng-show="editingItem === item" class="fa fa-check link text-success "></i>
<i ng-click="cancelEditItem(item)" ng-show="editingItem === item" class="fa fa-times link text-danger"></i>
</td>
</tr>
</tbody>
</table>
Controller code:
angular.module('myApp', [])
.controller('myController', function($scope) {
$scope.items = [{
id: '1',
fieldA: 'foo1',
fieldB: 'bar1',
fieldC: 'baz1'
}, {
id: '2',
fieldA: 'foo2',
fieldB: 'bar2',
fieldC: 'baz2'
}, {
id: '3',
fieldA: 'foo3',
fieldB: 'bar3',
fieldC: 'baz3'
}, ];
$scope.startEditItem = function(item) {
//clone, won't point to same reference
$scope.editingItemCopy = JSON.parse(JSON.stringify(item));
$scope.editingItem = item;
}
$scope.confirmEditItem = function() {
$scope.editingItem = null;
// no need to do anything, the model is changed already.
}
$scope.cancelEditItem = function(item) {
$scope.editingItem = null;
//below line doesn't work
//item = JSON.parse(JSON.stringify($scope.editingItemCopy));
for(var i = 0; i < $scope.items.length; i++) {
if($scope.items[i].id === item.id) {
$scope.items[i] = JSON.parse(JSON.stringify($scope.editingItemCopy));
}
}
}
});
I want to know why the commented line in $scope.cancelEditItem is not working and I have to modify the $scope.items array if the passed argument to the function is supposed to be pointing to the same element within the array.
Here's the plunkr http://plnkr.co/edit/XF1ytvJ6HYfceaivZhfr?p=preview , feel free to uncomment that line and comment the array iteration to see what I'm talking about.