Angularjs排序依据错后$指数 [英] Angularjs wrong $index after orderBy
问题描述
我是新来Angular.js,并有一些问题整理我的数组,上排序的数据的工作。
I am new to Angular.js and have some problems sorting my array and working on that sorted data.
我有项目的列表和由Store.storeName,这是工作迄今希望如此排序的。但是,对数据进行排序后,我删除功能不好用了。我想,那是因为$指数排序后错,所以错误的数据将被删除。
I have a list with items and want so sort it by "Store.storeName", which is working so far. But after sorting the data, my delete-function is not working anymore. I think thats because the $index is wrong after sorting, and so the wrong data is deleted.
我该如何解决呢?订货中的范围,而不是在视图中的数据?那怎么办?
How can I solve that? Ordering the data in the scope and not in the view? How to do that?
下面是一些相关的code
Here is some relevant code:
在View:
<tr ng-repeat="item in items | orderBy:'Store.storeName'">
<td><input class="toggle" type="checkbox" ng-model="item.Completed"></td>
<td>{{item.Name}}</td>
<td>{{item.Quantity}} Stk.</td>
<td>{{item.Price || 0 | number:2}} €</td>
<td>{{item.Quantity*item.Price|| 0 | number:2}} €</td>
<td>{{item.Store.storeName}}</td>
<td><a><img src="img/delete.png" ng-click="removeItem($index)">{{$index}}</a></td>
</tr>
在我的控制器我有这样的删除功能,这应该删除的具体数据:
And in my controller I have this delete function, which should delete the specific data:
$scope.removeItem = function(index){
$scope.items.splice(index,1);
}
这很好地工作在查看订货前。
如果一些重要的东西丢失了,请让我现在。
This works nicely before ordering in the View. If something important is missing, please let me now.
谢谢!
推荐答案
相反或中继的 $指数
- 这 - 因为你已经注意到了 - 将指向索引在排序/过滤阵列,可以将项目本身传递给你的的removeItem
功能:
Instead or relaying on the $index
- which - as you have noticed - will point to the index in a sorted / filtered array, you can pass the item itself to your removeItem
function:
<a><img src="img/delete.png" ng-click="removeItem(item)">{{$index}}</a>
和修改的removeItem
函数查找使用数组的的indexOf
方法的指标如下:
and modify the removeItem
function to find an index using the indexOf
method of an array as follows:
$scope.removeItem = function(item){
$scope.items.splice($scope.items.indexOf(item),1);
}
这篇关于Angularjs排序依据错后$指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!