Reputation: 1106
I'm creating editable table, and while editing all rows became input fields, how to choose only one row that I'm clicking on?
This is a piece of my table
<table>
<tr ng-repeat="item in data track by $index">
<td>
<span ng-if="!editing">{{item.username}}</span>
<span ng-if="editing">
<!-- If I use ng-if="editing && index == $index" than all other fields disappear -->
<input type="text" name="name" class="form-control" ng-model="item.username">
</span>
</td>
<td>
<div ng-if="!editing">
<button ng-click="edit()" >Edit</button>
</div>
</td>
</tr>
<table>
Controller:
.controller('formCtrl', function($scope) {
$scope.editing = false;
$scope.index = "";
$scope.edit = function(index) {
$scope.editing = true;
$scope.index = index;
};
Upvotes: 0
Views: 182
Reputation: 25352
pass index on click
try like this
<button ng-click="edit($index)" >Edit</button>
Or the object itself
<button ng-click="edit(item)" >Edit</button>
Upvotes: 1
Reputation: 7605
You have to check that the index of the current item is not the item in edition while in editing mode:
<span ng-if="!editing || index!=$index">{{item.username}}</span>
<span ng-if="editing && index==$index">
<input type="text" name="name" class="form-control" ng-model="item.username">
</span>
See working JsFiddle: http://jsfiddle.net/805rt1wd/1/
Upvotes: 1