select2 及tree用法,直接代码

其中
cache.do?getList 返回一个简单的bean json串



include.jsp


<script	src="${webPath}/plug-in/common/js/jquery-1.8.3.min.js"></script>
<link rel="stylesheet" type="text/css" href="${webPath}/plug-in/select2/css/select2.min.css" />
<link rel="stylesheet" type="text/css" href="${webPath}/plug-in/select2/css/select2-bootstrap.min.css" />
<script src="${webPath}/plug-in/select2/js/select2.min.js"></script>
<script src="${webPath}/plug-in/select2/js/i18n/zh-CN.js"></script>
<script src="${webPath}/plug-in/select2/js/select2tree.js"></script>

=============
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>select2示例</title>
<%@ include file="/plug-in/common/include.jsp"%>


<script>
	$(function() {
		$(".select2").select2({
			language : "zh-CN",
			placeholder : {
				id : '-1', // the value of the option
				text : '请选择重要程度...'
			},
		});
		//placeholder可能与jquery1.83不兼容
		$("#sajax").select2({
			language : "zh-CN",


			allowClear : true,
			ajax : {
				url : $webPath + 'cache.do?getList',
				dataType : 'json',
				delay : 250,
				data : function(params) {
					var query = {
						search : params.term,
						page : params.page || 1,
						rows : 30,
						key : 'AGRITECH_TYPE',
						bNeedAll :false
					}
					// Query paramters will be ?search=[term]&page=[page]
					return query;
				},
				processResults : function(data, params) {
					params.page = params.page || 1;
					var rows = data;
					for ( var i in rows) {
						var row = rows[i];
						row.id = row.itemCode;
						row.text = row.itemValue;
					}
					return {
						results : rows,
						pagination : {
							more : (params.page * 30) < data.total
						}
					};
				},
				cache : true
			},
			escapeMarkup : function(markup) {
				return markup;
			},
			templateResult : formatRepo,
			templateSelection : formatRepoSelection
		});
		$("#stree").select2tree();


		var data = [ {
			id : 0,
			text : 'enhancement',
			other : "userdata"
		}, {
			id : 1,
			text : 'bug'
		}, {
			id : 2,
			text : 'duplicate'
		}, {
			id : 3,
			text : 'invalid'
		}, {
			id : 4,
			text : 'wontfix'
		} ];


		$(".js-example-data-array").select2({
			data : data
		});


		$(".js-example-data-array-selected").select2({
			data : data
		});
	});
	function formatRepo(repo) {
		return repo.text
	}
	function formatRepoSelection(repo) {
		return repo.text
	}


	function getSelect() {
		var res = $("#Importance").select2("data")[0]; //单选
		var reslist = $("#multiple").select2("data"); //多选
		var sajax = $("#sajax").select2("data")[0]; 
		
		if (res == undefined) {
			alert("你没有选中Importance");
		} else {
			alert("Importance:" + res.id + res.text);
		}
		if (reslist.length) {
			alert("multiple:" + reslist[0].id + reslist[0].text);
		} else {
			alert("你没选中multiple");
		}
		if (sajax == undefined) {
			alert("你没有选中sajax");
		} else {
			alert("sajax:" + sajax.id + sajax.text);
		}		
	}
	function clearSelect() {
		//清空选择
		$("#Importance").val(null).trigger("change");
		$("#multiple").val(null).trigger("change");
		//disabled
		$(".js-example-data-array").prop("disabled", true);//不可用
	}
</script>
</head>
<body>
	<div>
		<select id="Importance" class="select2"
			style="width: 200px;margin-left: 10%;float:left;">
			<option></option>
			<option value="1">非常重要</option>
			<option value="2">重要</option>
			<option value="3">一般</option>
		</select> <select id="multiple" class="select2" multiple="multiple"
			style="width: 200px;margin-left: 10%;float:left;">
			<option value="A">非常重要</option>
			<option value="B">重要</option>
			<option value="C">一般</option>
		</select> <select id="sajax" style="width: 200px;margin-left: 10%;float:left;">
		</select> <select id="stree" multiple="multiple"
			style="width: 300px;margin-left: 10%;float:right;">
			<option value="12" parent="1">12</option>
			<option value="1" disabled="disabled">1</option>
			<option value="13" parent="1">13</option>
			<option value="11" parent="1">11</option>
			<option value="131" parent="13">131</option>
			<option value="132" parent="13">132</option>
			<option value="2" disabled="disabled">2</option>
			<option value="21" parent="2">21</option>
			<option value="32" parent="3">32</option>
			<option value="33" parent="3">33</option>
			<option value="3" disabled="disabled">3</option>
			<option value="31" parent="3">31</option>
			<option value="22" parent="2">22</option>
			<option value="133" parent="13">133</option>
			<option value="23" parent="2">23</option>
		</select>
	</div>


	<select class="js-example-data-array"></select>


	<select class="js-example-data-array-selected">
		<option value="2" selected="selected">duplicate</option>
	</select>
	<input type="button" οnclick="getSelect()" value="取值">
	<input type="button" οnclick="clearSelect()" value="清除">


</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值