1

I have a editTable like this:

<tr ng-repeat='guest in guestList | orderBy:orderByField:reverseSort'>
   <td><span ng-show='!guest.isedit'>{{guest.firstname}}</span><span ng-show='guest.isedit'><input type="text" ng-model='guest.firstname'/></span></td>
    <td><span ng-show='!guest.isedit'><a href="javascript:void(0)" ng-click='editGuest(guest)'><i class="material-icons">mode_edit</i></a></span><span ng-show='guest.isedit'><button ng-click='contactUpdate(guest)'>OK</button></span></td>
</tr>

in my controller:

$scope.editGuest = function(guest){
        delete $scope.orderByField;  
        guest.isedit = true;   
    };
$scope.contactUpdate = function(guest){
       //Save the change then put the order back to re-order the table
      $scope.orderByField = 'firstname';
}

As you can see, this is a editable table, if I click edit, the table becomes editable. I want to disable the sort when editing, until user finish editing and hit the OK button(Which already saved in the server), then I will re-order with new data. The problem is for the first time I fire editGuest(guest),it will still jump. Is there any way I can achieve this?

2 Answers 2

1

You need to have a different ng-model on your input box like this :

<tr ng-repeat='guest in guestList | orderBy:orderByField:reverseSort'>
   <td>
       <span ng-show='!guest.isedit'>{{guest.firstname}}</span>
       <span ng-show='guest.isedit'><input type="text" value="{{ guest.firstname }}" ng-model='firstname'/></span>//I have set the value to the guest's firstname to default the name when the input box becomes active. You can also use ng-init to achieve it.
</td>
    <td>
        <span ng-show='!guest.isedit'><a href="javascript:void(0)" ng-click='editGuest(guest)'><i class="material-icons">mode_edit</i></a></span>
        <span ng-show='guest.isedit'><button ng-click='contactUpdate(firstname)'>OK</button></span></td>
</tr>

In your Controller :

$scope.contactUpdate(guest, name){
    guest.firstname = name;
    name = '';//EDIT
}
Sign up to request clarification or add additional context in comments.

3 Comments

I think firstname is not in guestList so it will be same for all <tr>'s. Don't you think that this should be unique for each row ?
Otherwise you have to pass guest in contactUpdate function and after saving clear firstname value.
@shyammakwana.me yup you're right. I am passing the guest in the update function so that shouldnt be an issue. I should clear the firstname after setting it
0

You need to remove binding first for this.

Create clone of guestList object and do the editing operation on it.

once it is done assign the clone object back to guestList.

e.g.

var dummyGuestList = angular.copy(guestList)

//some operation on dummyGuestList ;

guestList = dummyGuestList //assign bacl again

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.