【问题标题】:Incorrect row delete with orderBy filter angularjs使用 orderBy 过滤器 angularjs 删除不正确的行
【发布时间】:2016-01-21 11:30:37
【问题描述】:

我用 AngularJS 做表。我使用了orderBy 过滤器。之后我的删除功能开始删除另一行,除了我点击删除。

这里是过滤器:

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

我错过了什么?

【问题讨论】:

  • 你必须在removeRow函数中使用相同的顺序。 ng-repeat 中应用的过滤器排序不会对数组本身进行排序。

标签: html css angularjs filter


【解决方案1】:

$index 表示呈现表中的索引。但是,您根据原始数组中的索引进行删除。 orderBy: 不对原始数组进行排序,而是将有序副本传递给 ng-repeat

解决方案:您有两种选择:

  1. 对原数组排序,不要使用orderBy:

  2. 不要通过索引来识别要删除的项目,而是通过它的 id 或实际条目本身来识别。示例:

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

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多