Extjs新手。我试图通过使用afterrender更改组合框的默认值。这是我一直在尝试的:
xtype: 'combo',
fieldLabel: 'Post Method',
name: 'postMethod',
store: Chem.getPostMethod(),
valueField: 'id',
value: 0,
displayField: 'method',
triggerAction: 'all',
mode: 'local',
optional: 0,
hidden: true,
allowBlank: false,
listeners: {
afterrender: function (combo) {
var store = combo.getStore();
if (combo.getValue() === 0) {
combo.setValue(store.getAt(1).get(combo.valueField));
}
}
}
我的ArrayStore如下:
Chem.getPostMethod = function () {
return new Ext.data.ArrayStore({
id: 0,
fields: ['id', 'method'],
data: [
[1, 'Post Url'],
[2, 'Json']
],
})
};
我做错了吗?它无法改变价值。我已经放了一个调试器,并尝试使用
手动更改值combo.setValue('2')
给出了未定义的结果。有什么帮助吗?
答案 0 :(得分:3)
此处的问题是您尝试将值 0 设置为组合,这在商店中不可用。因此,该值未设置为0并且getValue()返回null而不是0,因此解决方案是检查是否存在值而不是将其与0进行比较。
afterrender: function (combo) {
var store = combo.getStore();
if (!combo.getValue()) {
combo.setValue(store.getAt(1).get(combo.valueField));
}
}
对于setValue调用,您传递的是错误的数据类型,因此未选中该值。它应该是combo.setValue(2)而不是combo.setValue(' 2')
答案 1 :(得分:0)
我在应用中使用相同的模式:
afterrender: function(combo) {
if(combo!== undefined) {
var recordSelected = combo.getStore().getAt(0);
if(recordSelected!== undefined) {
combo.setValue(recordSelected.get('AN'));
}
}
}
我可以看到它的差异可能是你的商店没有在afterRender之前加载。如果不是,也许你必须使用arrayStore上的监听器load
来执行setValue而不是afterrender。
答案 2 :(得分:0)
如果您使用ExtJS 6,则Afterrender可能为时尚早。在ExtJS 6中,相应的事件为boxready
。
此外,我建议使用combobox.select()
。
boxready:function() {
if(store.isLoaded()) {
combo.select(store.getAt(1));
} else {
store.on({
load:function() {
combo.select(store.getAt(1))
},
single:true
});
}
}