2016-01-21 34 views
0

我使用AngularJS表。我用orderBy過濾器。之後,我的刪除功能開始刪除另一行,除了我點擊刪除。錯誤的行刪除與orderBy過濾器angularjs

這裏是過濾器:

<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> 

和刪除功能:

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

我錯過了什麼?

+1

你必須在'removeRow'功能使用相同的順序。在ng-repeat中應用的過濾順序不會對數組本身進行排序。 –

回答

1

$index代表呈現表中的索引。但是,您將根據原始數組中的索引進行刪除。 orderBy:不排序原始數組,但將有序副本傳遞到ng-repeat

解決方案:你有兩個選擇:

  1. 排序原始數組,不要使用orderBy:

  2. 不認同它的指數,而是通過刪除的項目其ID或實際條目本身。例如:

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