EmberJS的数组排序物业对象 [英] Sort array of EmberJS objects by property

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

问题描述

我过滤下注意到这一点的EmberJS网站:

I noticed this on the EmberJS website under Filtering:

在一个可枚举执行另一项常见的任务是走
  枚举作为输入,之后返回数组的排序或对其进行筛选
  基于一些标准。

Another common task to perform on an Enumerable is to take the Enumerable as input, and return an Array after sorting or filtering it based on some criteria.

想象一下,我有灰烬对象的数组,我怎么去有关财产对它们进行排序?

Imagine I have an array of Ember objects, how do I go about sorting them by property?

App.DemoArray = Ember.ArrayController.create({
    content:[
        Ember.Object.create({name:'Joe', Age:29}),
        Ember.Object.create({name:'Jim', Age:53}),
        Ember.Object.create({name:'Jack', Age:12})
    ]
})

如果我想按年龄排序上面?感谢您的帮助!

What if I wanted to sort the above by age? Thanks for your help!

编辑:
我发现这个SproutCore的文档中,但它似乎并没有与Ember工作:

I found this in the sproutcore documentation but it doesn't seem to work with Ember:

您可以根据某些属性或列表的排序值的可枚举
  物业使用sortProperty。如果您在多个属性传球,
  SproutCore的将整理用相同价值的物品的第一属性
  由第二个参数的值,并依此类推。

You can sort an Enumerable based on the value of some property or list of properties using sortProperty. If you pass in multiple properties, SproutCore will sort items with the same value for the first property by the value of the second parameter, and so on.

第3.8节在此页面上:<一href=\"http://guides.sproutcore20.com/enumerables.html\">http://guides.sproutcore20.com/enumerables.html

Section 3.8 on this page: http://guides.sproutcore20.com/enumerables.html

推荐答案

这里描述你可以现在你的排序ArrayController。

As described here you can now sort your ArrayController.

你做的方式是在你的ArrayController提供额外的属性(从上面的链接粘贴):

The way you do it is provide extra properties on your ArrayController (pasted from link above):

songs = [
  {trackNumber: 4, title: 'Ob-La-Di, Ob-La-Da'},
  {trackNumber: 2, title: 'Back in the U.S.S.R.'},
  {trackNumber: 3, title: 'Glass Onion'},
];

songsController = Ember.ArrayController.create({
  content: songs,
  sortProperties: ['trackNumber'],
  sortAscending: true
});

songsController.get('firstObject');  // {trackNumber: 2, title: 'Back in the U.S.S.R.'}
songsController.addObject({trackNumber: 1, title: 'Dear Prudence'});
songsController.get('firstObject');  // {trackNumber: 1, title: 'Dear Prudence'}

这篇关于EmberJS的数组排序物业对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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