按属性对 EmberJS 对象数组进行排序 [英] Sort array of EmberJS objects by property

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

问题描述

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

I noticed this on the EmberJS website under Filtering:

另一个在 Enumerable 上执行的常见任务是将可枚举作为输入,并在排序或过滤后返回一个数组基于某些标准.

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.

假设我有一个 Ember 对象数组,我该如何按属性对它们进行排序?

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:

您可以根据某些属性或列表的值对 Enumerable 进行排序使用 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 节: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天全站免登陆