Extjs 新手。我正在尝试使用渲染后更改组合框的默认值。这是我一直在尝试的:
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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的ArrayStore如下:
Chem.getPostMethod = function () {
return new Ext.data.ArrayStore({
id: 0,
fields: ['id', 'method'],
data: [
[1, 'Post Url'],
[2, 'Json']
],
})
};
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?它无法改变该值。我已经放置了一个调试器并尝试使用手动更改值
combo.setValue('2')
Run Code Online (Sandbox Code Playgroud)
这给出了一个未定义的结果。有什么帮助吗?
这里的问题是您试图将值0设置为组合,该值在商店中不可用。因此,该值未设置为 0,并且 getValue() 返回 null 而不是 0,因此解决方案是检查值是否存在,而不是将其与 0 进行比较。
afterrender: function (combo) {
var store = combo.getStore();
if (!combo.getValue()) {
combo.setValue(store.getAt(1).get(combo.valueField));
}
}
Run Code Online (Sandbox Code Playgroud)
对于 setValue 调用,您传递了错误的数据类型,因此未选择该值。它应该是combo.setValue(2)而不是combo.setValue('2')