【发布时间】:2016-06-05 18:08:23
【问题描述】:
在输入搜索参数之前,“selected”属性中必须设置一个值。谁能帮我解决这个问题?#ember-power-select
通过在 js 中为“destination”设置一个值并将“destination”分配给“selected”,它与正常的动作处理一起工作。在此处查看此类示例http://www.ember-power-select.com/docs/action-handling。
但无法为 custom-serach-action http://www.ember-power-select.com/docs/custom-search-action 赋值。
我的代码:
型号:
-
hpqualifcation.js
import DS from 'ember-data'; export default DS.Model.extend({ type_name: DS.attr('string'), hoprofile: DS.belongsTo('hoprofile') }); -
hoprofile.js
import DS from 'ember-data'; export default DS.Model.extend({ name: DS.attr('string'), hpqualification: DS.hasMany('hpqualification') });
路线:
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params){
return Ember.RSVP.hash({
hpqualifications: this.store.query('hpqualification', {username: params.username}),
…
});
}
})
API 端的数据:
{
"hpqualification": [
{
"id": 1,
"type_name": "UG",
"hoprofile": 1
},
{
"id": 1,
"type_name": "PG",
"hoprofile": 2
}
],
"hoprofile": [
{
"id": 1,
"name": "silicon guy",
"hpqualifications": [1]
},
{
"id": 2,
"name": "power star",
"hpqualifications": [2]
}
]
}
模板:
使用了 ember-power-select custom-search-action,请求将被发送到 API 端输入每个字母,返回的数据将显示在选择框选项http://www.ember-power-select.com/docs/custom-search-action
{{#each model.hpqualifications as |hpqualification|}}
{{#power-select
selected=hpqualification.hoprofile.name
search=(action "hoProfile")
onchange=(action (mut hpqualification.hoprofile.name))
as |repo|
}}
{{repo.name}}
{{/power-select}}
{{/each}}
{{/power-select}}
{{/each}}
组件:
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
hoProfile(term){
if (Ember.isBlank(term)) { return []; }
const url = `//localhost:3000/betaweb/filters/hoprofiles? name=${term}`;
return Ember.$.ajax({ url }).then(json => json.hoprofiles);
}
}
});
为电源选择操作返回的数据:
{
"hoprofiles": [{
"id": 7,
"name": "silicon guy"
}, {
"id": 14,
"name": "power star"
}]
}
一切正常。但是在 ember-power-select 中,未选择预选值。在输入搜索参数之前选择框是空白的。 通常使用下面的代码值是可见的
{{#each model.hpqualifications as |hpqualification|}}
<label>HO Profile Name<label>
<li> {{input value=hpqualification.hoprofile.name}} </li>
{{/each}}
它显示从API端返回的所有数据。
HO Profile Name
- silicon guy
- power star
但是当我使用 ember-power-select 时,数据没有在选择框中预选。我尝试了很多方法,但它并没有解决我的问题。谁能给我一个解决方案或使用 power-select 的替代方法?
【问题讨论】:
-
预填是什么意思?输入搜索参数时是否填充了列表?
-
是的,我在输入搜索参数时填充了列表。预填充是否应该在输入搜索参数@Igor之前已经在“selected”中放置了一些值
标签: ember.js ember-data ember-cli ember-cli-addons