我的电源选择不会显示所选的值。
我在这个组件中有三个电源选择,两个工作正常,但第三个(companyType)表现得很奇怪 - 当你第一次进入表单时,该值为空。当您选择一个值时,它会从那时开始为该记录添加(直到您刷新页面)
模板:
// templates/components/companies/edit-new-form.hbs
{{#bs-form formLayout=formLayout model=company onSubmit=(action "save") as |form|}}
<label>State:
{{#power-select
selected=company.state
options=states
onchange=(action "chooseState")
as |name|
}}
{{name}}
{{/power-select}}
</label>
<label>County:
{{#power-select
selected=company.county
options=countyNames
onchange=(action "chooseCounty")
as |name|
}}
{{name}}
{{/power-select}}
</label>
<label>Company Type:
{{#power-select class="select"
selected=company.companyType
options=companyTypes
onchange=(action "chooseCompanyType")
as |name|
}}
{{name.companyType}}
{{/power-select}}
</label>
{{/bs-form}}
JS:
// componnents/companies/edit-new-form.js
export default Ember.Component.extend({
company: null,
router: Ember.inject.service('-routing'),
ajax: Ember.inject.service(),
store: Ember.inject.service(),
countyNames: Ember.computed('company.state', function() {
return this.get('ajax').request(config.host + '/api/counties/' + this.company.get('state')).then( (json) => json.map( (county) => {
return county.countyName;
})
)
}),
states: ['AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY'],
actions: {
// For dropdown handling
chooseState(state) {
this.set('company.state', state);
//Ember.set(this, "counties", this.get('ajax').request(config.host + '/api/counties/' + state).then((json) => json.items));
//this.set('counties', this.get('ajax').request(config.host + '/api/counties/' + state).then((json) => json));
},
chooseCounty(countyName) {
this.set('company.county', countyName);
},
chooseCompanyType(companyType) {
this.set('company.companyType', companyType);
},
}
});
这是路线和模型钩子:
// routes/companies/edit.js
export default Ember.Route.extend({
model(params) {
var store = this.store;
return Ember.RSVP.hash({
companies: store.findRecord('company', params.company_id),
companyTypes: store.findAll('companyType')
});
}
});
获取companyType模型的路线是:
// routes/company-types.js
export default Ember.Route.extend({
model() {
return this.get('store').findAll('company-type');
}
});
我的模特:
// models/company.js
export default DS.Model.extend({
companyName: DS.attr('string'),
street: DS.attr('string'),
county: DS.attr('string'),
city: DS.attr('string'),
state: DS.attr('string'),
zip: DS.attr('string'),
phone: DS.attr('string'),
email: DS.attr('string'),
fax: DS.attr('string'),
companyType: DS.attr('string')
});
// models/company-type.js
export default DS.Model.extend({
companyType: DS.attr('string')
});
查看Ember Inspector,我的模型中有companyType的数据,并且是一个字符串。当我从保管箱中选择一个值时,数据类型将变为<svegbackend@model:company-type::ember1326:Environ Engineer
。当我选择其中一个下拉列表时,该值保持一个字符串,我认为这是有意义的,因为这些字符串是字符串数组。
那么如何在表单加载时显示下拉值?
答案 0 :(得分:0)
问题在于,companyType
的{{1}}是string类型,其中第三个select的选项列表是company
型的类型。这会在最初设置所选选项时产生不匹配。
定义第三个电源选择如下:
companyType
并在{#power-select class="select"
selected=company.companyType
options=companyTypesOptions
onchange=(action "chooseCompanyType")
as |name|
}}
{{name}}
{{/power-select}}
中定义companyTypesOptions
,如下所示:
edit-new-form.js
通过这种方式;您将同时创建选项列表和字符串类型的选定值!不要忘记选项的类型和所选择的选项是否不同;你会遇到麻烦的!尽量避免这种情况!