做了一个前端任务,因为使用的条件很多,点击一个条件,后边跟着的下拉框Store及展示值等都需要换,所以想使用动态方法来解决。
请教了大神后,大神教给了我一个看起来很简洁的办法,以下
listeners:{
change:function(group, newValue, oldValue){
if(newValue != 1){ //下拉框类型搜索
Ext.getCmp('searchtext').hide();
Ext.getCmp('searchcomboCon').show();
var tempStore;
//定义了一个对象,用来定义添加的内容
//例:"2"==》条件对应的数值,2代表我选择的是供应商搜索
// tempDict[0]==》定义了需要展示的内容valueField
// tempDict[1]==》定义了需要传递的值displayField
// tempDict[2]==》定义了需要使用的Store
var tempDict = {
"2": ["lspname","lspid", orderInquiry.supplierStore], //供应商搜索
"3": ["name","ldtid", orderInquiry.daytemplateStore], //模版搜索
"4": ["lsname","lsid", orderInquiry.delivery], //配送中心
"5": ["name","llid", orderInquiry.lineGroupStore], //线路
"6": ["lcname","lvid", orderInquiry.vehiclemanageStore], //车辆
"7": ["lsname","lsid", orderInquiry.shopstore], //门店
"8": ["name","value", orderInquiry.isdepotmoveStore], //是否领用
}
if(tempDict[newValue]){
window.container = tempDict[newValue];
var container = Ext.getCmp('searchcomboCon');
container.removeAll();
container.add({
xtype:'combobox'
,id:'searchcombo'
,displayField: tempDict[newValue][0]
,valueField: tempDict[newValue][1]
,store: tempDict[newValue][2]
,queryMode:'local'
,width:120
})
//生成的控件进行重新加载store并设置默认值
tempDict[newValue][2].load(function(val){
var id = tempDict[newValue][1];
var val = val[0].data[id];
Ext.getCmp("searchcombo").setValue(val);
});
}
}else{ //单据搜索——文本
Ext.getCmp('searchtext').show();
Ext.getCmp('searchcomboCon').hide();
}
}
-- 以下为联动的text文本框和下拉框
{
xtype:'textfield' // 条件中有一个是使用的文本框,其他都是下拉框
,id:'searchtext'
,width:100
},{
xtype:'container', //这里写了一个容器,用来占位,包裹生成的下拉框
id:"searchcomboCon",
hidden:true,
cls: "x-field-default-toolbar",
height:20
}