使用Js对象,动态生成Combo,给Combo赋值

做了一个前端任务,因为使用的条件很多,点击一个条件,后边跟着的下拉框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
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值