Ember - 计算排序 [英] Ember - computed sort
问题描述
sortOptions:['amount:desc','place' ]
Ember.computed.sort('model',sortOptions)
关键金额基本上是一个数字,但在JSON模型中,它作为一个字符串。所以,当我运行这个代码,它不是按数量排序,但是当我修改JSON来将该数量字符串转换为数量,这是有效的。
Ember计算排序的正确行为是否正确?
您可以使用自定义函数与 Ember.computed.sort
可以解决您的问题
我相信您从JSON获取字符串数量,并且您希望将其排序为降序订单。
//使用自定义排序函数
Ember.computed.sort('model',function(a ,b){
if(a.amount> b.amount){
return -1;
} else if(a.amount< b.amount){
返回1;
} else {
return 0;
}
})
I am using the below code for sorting a list.
sortOptions: ['amount:desc','place']
Ember.computed.sort('model',sortOptions)
The key "amount" is basically a number, but in JSON "model", its coming as a string. So, when I ran this code, it wasn't sorting by amount, but when I modified the JSON to convert that amount string to amount number, that worked. Is this correct behavior of Ember computed sort?
you can use custom function with the Ember.computed.sort
which can solve your problem
I believe you get string as amount from JSON and you want to sort it as descending order.
// using a custom sort function
Ember.computed.sort('model', function(a, b){
if (a.amount > b.amount) {
return -1;
} else if (a.amount < b.amount) {
return 1;
} else {
return 0;
}
})
这篇关于Ember - 计算排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!