2

I have a table in which i insert rows dynamically with edit/delete link for each row. Edit row is properly working but when it comes to delete it always deletes the first row.

Here is the code:

  <table ng-model="Employee" border="1">
      <thead>
          ..
      </thead>
      <tbody>
        <tr ng-repeat="emp in employees">
           <td>{{emp.id}}</td>
            ..
           <td><a href="#" ng-click="EditRow(emp);">Edit</a>&nbsp &nbsp<a href="#" ng-click="DeleteRow(emp);">Delete</a> </td>

         </tr>
       </tbody>
 </table>

corresponding controller code:

 $scope.DeleteRow=function(emp) {

    $scope.employees.splice(emp,1);

}

It always deletes the first row.Plz help me.

1 Answer 1

4

Pass the index in delete function,

HTML

<td><a href="#" ng-click="EditRow(emp);">Edit</a>&nbsp &nbsp<a href="#" 
       ng-click="DeleteRow($index);">Delete
</a> </td>

JS

DeleteRow($index);

$scope.DeleteRow=function(index) {

    $scope.employees.splice(index,1);

}

in Splice method you need to pass First Index and then How many element you need to delete,

splice(2,1)

Check the Details about the method @ http://www.w3schools.com/jsref/jsref_splice.asp

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.. its working... but even with emp i was passing the same index na... then why didnt it work?
Emp is an object not a Index

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.