0

I am creating a angular application, which has one li with ng-repeat and a text box for searching the out put in ng-repeat, the repeated item has a delete "X" button which deletes the record from DB. problem i am facing is how to remove searched record from the array using splice.

http://plnkr.co/edit/oPBofD2wL2ZUrD23f8Rr?p=preview

go to above plnkr click on any of the "x" without searching, then it will get removed from the list . but when you search for something lets say ruby then only ruby will come into the list but by clicking on "x" it will still appear . I need help in removing the searched field from the array using array operation, i dont want to regenerate that array again.

2
  • Why should I visit plunker? Questions at SO should be complete. Always post you code here. Commented May 15, 2014 at 6:53
  • @Mayray Take a look at my answer Commented May 15, 2014 at 6:54

3 Answers 3

5

instead of just using the index as a parameter, calculate your index with a search of the json element in the array.

Like so:

$scope.delete = function(project) {
  idx = $scope.projects.indexOf(project);
  $scope.projects.splice(idx, 1);
};

DEMO

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

2 Comments

I have added plunker link with your question, Reveret back if you don't want that
Thanks a lot it is working fine .. so out of indexOF solution and beow solution by passing ID , which one is efficient $scope.delete = function(idx) { for ( var i = 0; i < $scope.projects.length; i++){ if ($scope.projects[i].id === idx) { $scope.projects.splice(i, 1); } } };
2

The problem is that your idx is set to $index. That is the index of the ng-repeat, not the index of the array so when the list is filtered you're now deleting the wrong element. (You'll notice this if you clear the search-box after deleting)

If you change your HTML to

<a href="" ng-click="delete(project)">X</a>

The delete function will receive the actual element that needs to be removed and can be rewritten to

$scope.delete = function(project) {
  $scope.projects.splice($scope.projects.indexOf(project), 1)
};

2 Comments

Thank you very much .. it worked can u pls tell me which one is efficient finding index of the passed project or the one which Nadhish has posted ?
1

The reason is that you are sending the $index for removing, instead of that pass the id like

 <a href="" ng-click="delete(project.id)">X</a>  

Working Demo

Try this out

$scope.delete = function(idx) {
  for ( var i = 0; i < $scope.projects.length; i++){
    if ($scope.projects[i].id === idx)
     {
         $scope.projects.splice(i, 1);
     }
  }
};

1 Comment

Thank you very much this worked, but not sure which one is efficient ..please see below solutions as well

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.