Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement the cancel button for edit functionality in angularjs

i have used edit button. after editing i have save and cancel button .save button is working as i expected but not the cancel button. if i click the cancel button after trying to edit, it should show the previous text. can anyone please help me
js fiddle here http://jsfiddle.net/F7K63/143/

 <tr ng-repeat="item in items">
        <td>
            <span ng-hide="item.editing">{{item.name}} <button  ng-click="editItem(item)">Edit</button></span>
            <input ng-show="item.editing" ng-model="item.name"  autofocus />
            <button ng-show="item.editing" ng-click="doneEditing(item)">Save</button>
             <button ng-show="item.editing" ng-click="Cancel(item)">Cancel</button>
        </td>
    </tr>
like image 267
Thilak Raj Avatar asked Jan 13 '16 09:01

Thilak Raj


2 Answers

Make a copy of your object you want to edit. If you press cancel replace the origin. See the fiddle

$scope.editItem = function (item) {
    item.editing = true;
    item.backupName = angular.copy(item.name);
}

$scope.doneEditing = function (item) {
    item.editing = false;
    delete item.backupName;
    //do some background ajax calling for persistence...
};
$scope.Cancel = function (item) {
    item.editing = false;
    item.name = angular.copy(item.backupName);
    delete item.backupName;
};
like image 148
Christoph Avatar answered Nov 10 '22 10:11

Christoph


The fastest option is to edit your $scope.eidtItem and $scope.Cancel functions.

$scope.editItem = function (item) {
    item.editing = true;
    item.oldName = item.name;
}

...

$scope.Cancel = function (item) {
    item.editing = false;
    item.name = item.oldName;
};

http://jsfiddle.net/F7K63/147/

like image 31
Michał Kolenda Avatar answered Nov 10 '22 08:11

Michał Kolenda