Extjs在自定义或者用系统combo时,有时候store数据是远程异步加载,此时,若是需要进入页面,combo加载第一条数据。
需要在store做个on事件监听,然后再事件监听中进行setValue()即可
比如从后端返回的json如下
[{"id":1,"batchName":"2016第一批次","year":2016},{"id":9,"batchName":"2016第二批次","year":2016,"createTime":"2016-08-23 00:00:00","createUser":2}]
需要combo默认选中第一条json
Ext.define('CUMT_CX.train.field.BatchCombo', {
extend : 'Ext.form.field.ComboBox',
requires : ['CUMT_CX.numas.train.model.NewNurseBatch'],
alias: ['widget.batchcombo'],
displayField : 'batchName',
valueField : 'id',
emptyText:'请选择培训批次...',
initComponent : function(){
var me = this;
var store = Ext.create('Ext.data.Store', {
model: 'CUMT_CX.train.model.NewNurseBatch',
autoLoad : true,
proxy: {
type: 'ajax',
url: CUMT_CX.ctx + '/numas/newNurseTrain/batchList',
listeners: {
exception: function(proxy, response, operation, eOpts) {
Lianfan.MessageBox.error(response);
}
}
}
});
store.on('load', function(store, record, opts) {
var combo = me;
var firstValue = store.getAt(0).get('id');// 这种方法可以获得第一项的值
me.setValue(firstValue);// 选中
});
Ext.apply(this, {
forceSelection: true,
store : store,
triggerAction : 'all',
matchFieldWidth : true,
anyMatch : true,
plugins: ['clearbutton'],
});
this.callParent();
}
});