[英]Orderby weighted average in angular
I am trying to sort a table by a weighted average of two columns. 我正在尝试按两列的加权平均值对表进行排序。 The weights for the columns are stored in a controller's scope. 列的权重存储在控制器的作用域中。 When I try to refer to these weights in my orderBy expression sorting is not done correctly. 当我尝试在orderB中引用这些权重时,表达式排序不正确。
<tr ng-repeat = "x in fruitdata | orderBy:'cost.apples*apples + cost.oranges * oranges'">
Fiddle of what I want: https://jsfiddle.net/t3op50p9/2/ 我想要的小提琴: https : //jsfiddle.net/t3op50p9/2/
If I hard code weights instead everything works as it should 如果我对权重进行硬编码,那么一切都会正常运行
<tr ng-repeat = "x in fruitdata | orderBy:'1.89*apples + 1.49 * oranges'">
Fiddle with hard coded weights (not what I want): https://jsfiddle.net/t3op50p9/3/ 用硬编码的砝码提琴(不是我想要的): https : //jsfiddle.net/t3op50p9/3/
You can define a function in your scope to be used to calculate the weight for ordering 您可以在范围内定义一个函数,用于计算订购权重
$scope.orderFn = function (fruit) {
return fruit.apples * $scope.cost.apples + fruit.oranges * $scope.cost.oranges;
}
ng-repeat="x in fruitdata | orderBy: orderFn:true"
(note: the true
on the end reverses the order generated by the ordering function making the largest weight on top.) ng-repeat="x in fruitdata | orderBy: orderFn:true"
(注意:最后的true
颠倒排序功能所产生的顺序,使排序权重最大。)
https://jsfiddle.net/TheSharpieOne/t3op50p9/6/ https://jsfiddle.net/TheSharpieOne/t3op50p9/6/
You should use cost.apples
& cost.oranges
shouldn't be inside the '
single quotes as they are not the part of ng-repeat
repeat array, so they will binded as string concatenation. 您应该使用cost.apples
和cost.oranges
不应位于'
单引号内,因为它们不是ng-repeat
重复数组的一部分,因此它们将作为字符串连接绑定。
Markup 标记
<tr ng-repeat="x in fruitdata | orderBy:cost.apples+'*apples + '+ cost.oranges+' * oranges'">
<td> {{x.oranges}} </td>
<td> {{x.apples}} </td>
<td> {{x.apples * cost.apples + x.oranges * cost.oranges}} </td>
</tr>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.