Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove item from array with filter in AngularJS?

When I click on tr without any filter, my function array.splice() works. Indexes in the array are in the correct order, so the array.splice() works.

When the filter is enable, Indexes in the array are not updated and still in the same order. So array.splice() removes the wrong item.

    <span ng-click="orderP0 = 'statut_name'; reversePO=!reversePO">order</span>

    <tr ng-repeat="project in projects | orderBy : orderPO : reverse track by $index" ng-click="remove($event,$index,projects)">
        <span class="label" ng-bind="project.statut_name"></span>
    </tr>

    $scope.remove = function($event,index,array){
        array.splice(index,1);
    };

How to update index in the array ? Or How to removes the right item ?

like image 302
Steffi Avatar asked May 22 '14 14:05

Steffi


1 Answers

It's easier to splice you projects in the actual position of the element in the array using indexOf.

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

This way you need to pass to the remove function only the current project.

<tr ng-repeat="project in projects | orderBy : orderPO : reverse track by $index" ng-click="remove(project)">
    <span class="label" ng-bind="project.statut_name"></span>
</tr>
like image 124
Victor Ivens Avatar answered Sep 22 '22 13:09

Victor Ivens