Ember.js 我可以使用简单对象在把手中使用select-2标记吗?

Ember.js 我可以使用简单对象在把手中使用select-2标记吗?,ember.js,handlebars.js,select2,ember-select,Ember.js,Handlebars.js,Select2,Ember Select,我有一个目标 let StatusDescriptions = { 'A' : 'Available' , 'W' : 'Waitlisted' , 'C' : 'Closed' }; 可在页面中找到。我用把手来显示页面。我可以在select-2标记中使用此对象吗?我尝试将optionValuePath作为“键”“id”等,我知道这很愚蠢。我也是新来的灰烬。请帮忙 {{select-2 id="myID" content=

我有一个目标

let StatusDescriptions = {
            'A' : 'Available' ,
            'W' : 'Waitlisted' ,
            'C' : 'Closed'
};
可在页面中找到。我用把手来显示页面。我可以在select-2标记中使用此对象吗?我尝试将optionValuePath作为“键”“id”等,我知道这很愚蠢。我也是新来的灰烬。请帮忙

{{select-2 id="myID" content=StatusDescriptions optionLabelPath="what should be here" placeholder='Choose Status' searchEnabled=false}}

更新:如果已安装,请选择2个Ember插件。。。(如果没有,说明如下)

您的对象应如下所示:

statusDescriptions: [
  {
    id: 'A',
    text: 'Available'
  },
  {
    id: 'W',
    text: 'Waitlisted'
  },
  {
    id: 'C',
    text: 'Closed'
  }
]
所以你可以把这个放在你的把手上:

{{select-2
    content=statusDescriptions
    id=id
    value=selectedChoice
    searchEnabled=false
}}
在把手或控制器中,您可以观察或在computed属性中使用
selectedChoice
属性。(这可能在您的车把文件中:)

Update2:如果您确实想使用简单对象,可以使用计算属性对其进行转换。例如,这可能在控制器中:

import Ember from 'ember';

export default Ember.Controller.extend({

  statusDescriptions: {
                        'A' : 'Available',
                        'W' : 'Waitlisted',
                        'C' : 'Closed'
                      },

  statusForSelect: Ember.computed('statusDescriptions', function() {
    const statusDescriptions = this.get('statusDescriptions');

    return Object.keys(statusDescriptions).map(key =>
      Object.create({id: key, text: statusDescriptions[key]})
    );
  })

});
因此,在模板中,您可以使用
statusForSelect
作为
content

{{select-2
    content=statusForSelect
    id=id
    value=selectedChoice
    searchEnabled=false
}}

如果尚未安装,请选择2插件:

是的,您可以在Ember项目中使用Select 2,但必须安装特定的Ember插件。您最喜欢的大部分javascript库已经移植到Ember,您可以在这里查看:

您可以在此处找到有关选择加载项的列表:

如您所见,有一个
ember-select-2
addon。在项目文件夹中运行:

ember install ember-select-2
如果要使用此软件包,请按照说明操作:


不过,还有更多最新的select软件包,您可以试用。其中一个最流行的是:

我想问题的作者想问的是,select-2是否可以将简单对象作为参数而不是数组来呈现select框。他已经安装了select-2。这意味着我不能像现在这样显示对象。在这个模型中我总是需要一个对象。没有关键价值的东西是可能的。是吗?根据Select-2文档,希望使用这种格式:您可以有一个键值属性,并且可以创建一个计算属性,该属性将自动转换为Select-2格式。我为此创建了一个jsbin。我在上面的答案中添加了computed属性,它将简单对象转换为与select-2兼容的数组对象。
ember install ember-select-2