EasyUI——combobox級聯

Easy UI的combobox實現省市縣的級聯選擇


js部分:

<script type="text/javascript">
	$(function(){
		var province = $("#province").combobox({
			valueField:'areaId',
			textField:'name',
			url:'do_cascade.jsp?id=0',
			onChange:function(newValue, oldValue){
	            $.get('do_cascade.jsp',{id:newValue},function(data){
	                city.combobox("clear").combobox('loadData',data);
	                county.combobox("clear");
	            },'json');
	        },
	        //使用onSelect不能實現第一層與第三層的聯動
			/* onSelect:function(rec){
				var url = "do_cascade.jsp?id="+rec.areaId;
				$("#city").combobox('reload',url);
			}, */
			onLoadSuccess:onLoadSuccess
		});
		var city = $("#city").combobox({
			valueField:'areaId',
			textField:'name',
			/* onSelect:function(rec){
				var url = "do_cascade.jsp?id="+rec.areaId;
				$("#county").combobox('reload',url);
			}, */
			onChange:function(newValue, oldValue){
	            $.get('do_cascade.jsp',{id:newValue},function(data){
	                country.combobox("clear").combobox('loadData',data);
	            },'json');
	        },
			onLoadSuccess:onLoadSuccess
		});
		var country = $("#county").combobox({
			valueField:'areaId',
			textField:'name',
			onLoadSuccess:onLoadSuccess
		});
	});
	function onLoadSuccess(){
		var target = $(this);
		var data = target.combobox("getData");
		var options = target.combobox("options");
		if(data && data.length>0){
			var fs = data[0];
			target.combobox("setValue",fs[options.valueField]);
		}
	}
</script>


html部分
<body>
	省份:<input id="province" style="width: 80px;">
	市區:<input id="city" style="width: 80px;">
	縣城:<input id="county" style="width: 80px;">
</body>


jsp處理頁面

	String idStr = request.getParameter("id");
	int id=0;
	if(idStr!=null || !idStr.equals("")){
		id = Integer.parseInt(idStr);
	}
	AreaDao ad = new AreaDaoImpl();
	List<Area> areas = ad.findChildById(id);
	Gson gson = new Gson();
	String json = gson.toJson(areas);
	System.out.print(json);
	out.print(json);

sql語句

select * from area where parentid=? order by vieworder

onLoadSuccess方法實現初始化時加載初始數據

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章