FrontEndist
FrontEndist

Reputation: 153

Incorrect row delete with orderBy filter angularjs

I make table with AngularJS. I used orderBy filter. After it my delete function started to delete another row except I click to delete.

Here is filter:

<tr class = "table-row isActive-{{task.active}} rowNumber-{{$index + 1}}" ng-repeat = "task in tasks | filter:search:strict | orderBy: '-priority':true">
    <td>
        <span class="delete-link">
            <input type="button" data-ng-click="removeRow($index)"/>
        </span>
    </td>
</tr>

and delete function:

$scope.removeRow = function (productIndex) {
    $scope.tasks.splice(productIndex, 1);
    productIndex=0
};

what I missed?

Upvotes: 0

Views: 151

Answers (1)

lex82
lex82

Reputation: 11317

The $index represents the index in the rendered table. However, you delete based on the index in the original array. orderBy: does not sort the original array but passes an ordered copy to ng-repeat.

Solution: You have two options:

  1. Sort the original array and don't use orderBy:

  2. Don't identify the item to delete by its index but rather by its id or the actual entry itself. Example:

    $scope.removeRow = function (task) {
        $scope.tasks.splice($scope.tasks.indexOf(task), 1);
    };
    

Upvotes: 1

Related Questions