简体   繁体   English

Ember.js:对ArrayController进行分组/分区

[英]Ember.js: Grouping/partitioning an ArrayController

I have an ArrayController, and I would like to group the contents of that ArrayController based on a the value of a specific key. 我有一个ArrayController,我想根据特定键的值对该ArrayController的内容进行分组。

For example, if the objects in my ArrayController are: 例如,如果我的ArrayController中的对象是:

id   status   name
-----------------------------
1    1        some name
2    1        some other name
3    2        blah
4    3        blah again

Then I would like to group the contents by status . 然后,我想按status对内容进行分组。

To do this, I tried using computed properties in my ArrayController: 为此,我尝试在ArrayController中使用计算属性:

App.SomeArrayController = Ember.ArrayController.extend({
  itemController: 'some-item',      

  status1: Ember.computed.filterBy('content', 'status', 1),
  status2: Ember.computed.filterBy('content', 'status', 2),
  status3: Ember.computed.filterBy('content', 'status', 3)
});

In the template, these are being displayed, but they are not being wrapped by the itemController I specified in the ArrayController: 在模板中,这些都被显示, 但他们都没有我的ArrayController指定的itemController包裹

// item controller used in the ArrayController
App.SomeItemController = Ember.ObjectController.extend({
  anotherKey: function () {
    return 'hey ' + this.get('name');
  }.property('name')
});


<!-- template to display status=1 items -->
{{#each status1}}

  // displays the name (as a property from the model)
  {{this.name}}

  // nothing displays here
  // (leading me to believe it is not being wrapped by the item controller)
  {{this.anotherKey}}
{{/each}}

What am I doing wrong? 我究竟做错了什么?

itemController only wraps items when you iterate over the controller collection. 仅当您遍历控制器集合时, itemController才会包装项目。

{{#each item in controller}}
  {{item.coolItemControllerProperty}}
{{/each}}

It doesn't apply to any collection within the controller. 它不适用于控制器内的任何集合。 If you tried to iterate the underlying model/content it wouldn't be wrapped. 如果您尝试迭代基础模型/内容,则不会对其进行包装。

{{#each item in content}}
  {{item.coolItemControllerProperty}} // undefined
{{/each}}

{{#each item in model}}
  {{item.coolItemControllerProperty}} // undefined
{{/each}}

fortunately you can specify an itemController in your template for these situations. 幸运的是,您可以在这些情况下的模板中指定一个itemController

{{#each item in status1 itemController='some-item'}}
  {{item.coolItemControllerProperty}}
{{/each}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM