Ember - 计算排序 [英] Ember - computed sort

查看:211
本文介绍了Ember - 计算排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆