1. 程式人生 > >EXTJS2.2元件Combobox下拉框獲取資料

EXTJS2.2元件Combobox下拉框獲取資料

    var dataPath;
    //遠端連線
    var genderStore = new Ext.data.JsonStore({         
            proxy: new Ext.data.HttpProxy({              
                method: "POST",               
                url: "<%=request.getContextPath()%>/tHarvestTableSdep.do?invoke=listTplMapDsForJson"   
            }),
            //root和fields需和後臺獲取json格式一致   
            root: "data",            
            fields:['dataPath','datasourceName'],    
            id:"departmentStore"
        });
        //這一步必須要,載入資料倉庫連線
        genderStore.load();
        var comboBox =  new Ext.form.ComboBox({
            id:'comboBox',
            mode : 'local',
            store : genderStore,
            width : 300,
            triggerAction: 'all',
            hiddenName:'dataPath',
            //展示資料
            displayField : 'datasourceName',
            //option的value
            valueField : 'dataPath',
            fieldLabel : '資料掛載目錄',
            listeners:{
                'select':function(arg){
                    //獲取選中下拉框的value
                    dataPath = Ext.getCmp("comboBox").getValue();
                }
            }
        });

後臺傳入前端的資料json格式:

{
	"data": [
        {
	    	"dataPath": "cd669a58f7bb454eb970a3403b01c39a",
	    	"datasourceName": "採集RELA"
	    }, {
	    	"dataPath": "f8d886a810f440e398eaa340eff717b0",
	    	"datasourceName": "長生人壽資料標準資料來源"
    	}, {
	    	"dataPath": "be32616ebbbb43c5a6fae9aa1d927cb5",
	    	"datasourceName": "資料標準採集資料來源1008"
	    }
    ]
}

 後臺複雜json格式拼寫方法

參考了https://www.jb51.net/article/137203.htm博主的,感謝

        JSONObject jsonObject = new JSONObject();
        List<JSONObject> jsonList = new ArrayList<>();
        for (THarvestDatasource tHarvestDatasource : list) {
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.element("dataPath",tHarvestDatasource.getDataPath());
           jsonObject2.element("datasourceName",tHarvestDatasource.getDatasourceName());
            jsonList.add(jsonObject2);
        }
        jsonObject.element("data",jsonList);