使用 Ember.Select 在 Ember.js 中创建选择相对容易.
问题是,如何使用 optgroup 将其变成分组选择。我不认为这是内置的,但我猜对模板进行一些修改是可能的。
最佳答案
Ember 现在原生支持它,但有一些问题。在 1.4.0 中,以下解决方案有效...
这是示例数据:
foos: Ember.A([{value: 'foo', label: 'Foo', group: 'Foos'}, {value: 'bar', label: 'Bar', group: 'Bars'}, {value: 'bar2', label: 'Bar2', group: 'Bars'}, {value: 'foo2', label: 'Foo2', group: 'Foos'}])
这是 View 助手:
{{view Ember.Select content=controller.foos optionLabelPath='content.label' optionValuePath='content.value' optionGroupPath='group'}}
如果您只是执行上述操作,您将获得如下所示的选择列表:
我能解决这个问题的唯一方法是按分组字段首先对数组排序:
foos: Ember.A([{value: 'foo', label: 'Foo', group: 'Foos'}, {value: 'bar', label: 'Bar', group: 'Bars'}, {value: 'bar2', label: 'Bar2', group: 'Bars'}, {value: 'foo2', label: 'Foo2', group: 'Foos'}]).sortBy("group")
关于javascript - 在 Ember.js 中创建分组选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11836596/