multiTree.html 2.78 KB
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('部门树选择')" />
	<th:block th:include="include :: ztree-css" />
</head>
<style>
	body{height:auto;font-family: "Microsoft YaHei";}
	button{font-family: "SimSun","Helvetica Neue",Helvetica,Arial;}
</style>
<body class="hold-transition box box-main">
	<input id="treeId" name="treeId" type="hidden" th:value="${multiDept.ids}"/>
	<input id="treeName" name="treeName"  type="hidden" th:value="${multiDept.names}"/>
	<div class="wrapper"><div class="treeShowHideButton" onclick="$.tree.toggleSearch();">
		<label id="btnShow" title="显示搜索" style="display:none;"></label>
		<label id="btnHide" title="隐藏搜索"></label>
	</div>
	<div class="treeSearchInput" id="search">
		<label for="keyword">关键字:</label><input type="text" class="empty" id="keyword" maxlength="50">
		<button class="btn" id="btn" onclick="$.tree.searchNode()"> 搜索 </button>
	</div>
	<div class="treeExpandCollapse">
		<a href="javascript:;" onclick="$.tree.expand()">展开</a> /
		<a href="javascript:;" onclick="$.tree.collapse()">折叠</a>
	</div>
	<div id="tree" class="ztree treeselect"></div>
	</div>
	<th:block th:include="include :: footer" />
	<th:block th:include="include :: ztree-js" />
	<script th:inline="javascript">
	    var prefix = ctx + "system/user"
	    var deptIds = $("#treeId").val();
	    var deptNames = $("#treeName").val();

		var checks = [];
		$(function() {
			var url = prefix + "/multiDeptData/" + deptIds;
			var options = {
		        url: url,
		        expandLevel: 2,
				check: {
					enable: true,//显示复选框
					chkStyle: "checkbox"
				},
				onCheck : zOnCheck
		    };
			$.tree.init(options);

			initChecks();
		});

		function initChecks() {
			if (deptIds && deptIds == "100") {
				return;
			}
			if (deptIds && deptNames) {
				var idArray = deptIds.split("-");
				var nameArray = deptNames.split("、");
				if (idArray.length == nameArray.length) {
					for (var i = 0; i < idArray.length; i++) {
						checks.push({id: idArray[i], name: nameArray[i]});
					}
				}
			}
		}

		function zOnCheck(event, treeId, treeNode) {
			if (treeNode.checked) {
				checks.push({id: treeNode.id, name: treeNode.name});
			} else {
				removeCheck(treeNode.id);
			}
			setValue();
		}

		function setValue() {
			var ids = "";
			var names = "";
			$.each(checks, function (idx, item) {
				ids += item.id + "-";
				names += item.name + "、";
			});
			$("#treeId").val(ids.substring(0, ids.lastIndexOf('-')));
			$("#treeName").val(names.substring(0, names.lastIndexOf('、')));
		}

		function removeCheck(chkid) {
			$.each(checks, function (idx, item) {
				if (item != undefined && item.id == chkid) {
					checks.splice(idx, 1);
				}
			})
		}
	</script>
</body>
</html>