公司最近的項目需要用到樹形下拉框,在網上找了挺多源碼,最後還是采用了zTree來實現,因為代碼的移植性比較高,而且數據的獲取比較容易。廢話不多說,直接上代碼。
index.jsp
. 代碼如下:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<link rel="stylesheet" href="<c:url value='/js/ztree/css/demo.css'/>" type="text/css"></link>
<link rel="stylesheet" href="<c:url value='/js/ztree/css/zTreeStyle/zTreeStyle.css'/>" type="text/css"></link>
<script type="text/javascript" src="<c:url value='/js/ztree/js/jquery-1.4.4.min.js'/>"></script>
<script type="text/javascript" src="<c:url value='/js/ztree/js/jquery.ztree.core-3.5.js'/>"></script>
<SCRIPT type="text/javascript">
<!--
var setting = {
view: {
dblClickExpand: false
},
data: {
simpleData: {
enable: true
}
},
callback: {
beforeClick: beforeClick,
onClick: onClick
}
};
//json數據源,也可以從後台讀取json字符串,並轉換成json對象,如下所示
//var strNodes = '${jsonList}';
//var zNodes = eval("("+strNodes+")"); //將json字符串轉換成json對象數組,strNode一定要加"()",不然轉不成功
var zNodes =[
{id:1, pId:0, name:"北京"},
{id:2, pId:0, name:"天津"},
{id:3, pId:0, name:"上海"},
{id:6, pId:0, name:"重慶"},
{id:4, pId:0, name:"河北省", open:true},
{id:41, pId:4, name:"石家莊"},
{id:42, pId:4, name:"保定"},
{id:43, pId:4, name:"邯鄲"},
{id:44, pId:4, name:"承德"},
{id:5, pId:0, name:"廣東省", open:true},
{id:51, pId:5, name:"廣州"},
{id:52, pId:5, name:"深圳"},
{id:53, pId:5, name:"東莞"},
{id:54, pId:5, name:"佛山"},
{id:6, pId:0, name:"福建省", open:true},
{id:61, pId:6, name:"福州"},
{id:62, pId:6, name:"廈門"},
{id:63, pId:6, name:"泉州"},
{id:64, pId:6, name:"三明"}
];
function beforeClick(treeId, treeNode) {
var check = (treeNode && !treeNode.isParent);
if (!check) alert("只能選擇城市...");
return check;
}
function onClick(e, treeId, treeNode) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
nodes = zTree.getSelectedNodes(),
v = "";
nodes.sort(function compare(a,b){return a.id-b.id;});
for (var i=0, l=nodes.length; i<l; i++) {
v += nodes[i].name + ",";
}
if (v.length > 0 ) v = v.substring(0, v.length-1);
var cityObj = $("#citySel");
cityObj.attr("value", v);
}
function showMenu() {
var cityObj = $("#citySel");
var cityOffset = $("#citySel").offset();
$("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
$("body").bind("mousedown", onBodyDown);
}
function hideMenu() {
$("#menuContent").fadeOut("fast");
$("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
hideMenu();
}
}
$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"), setting, zNodes);
});
//-->
</SCRIPT>
</head>
<body>
<div class="zTreeDemoBackground left">
<ul class="list">
<li class="title"> <span class="highlight_red">選擇城市時,按下 Ctrl 或 Cmd 鍵可以進行多選</span></li>
<li class="title"> 城市:<input id="citySel" type="text" readonly value="" style="width:120px;"/>
<a id="menuBtn" href="#" onclick="showMenu(); return false;">選擇</a></li>
</ul>
</div>
<div id="menuContent" class="menuContent" style="display:none; position: absolute;">
<ul id="treeDemo" class="ztree" style="margin-top:0; width:160px;"></ul>
</div>
</body>
</html>
spring後台
json數據類
. 代碼如下:
public class EquipTypeJson {
private String id;
private String pId;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPId() {
return pId;
}
public void setPId(String pId) {
this.pId = pId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
controller頁面
. 代碼如下:
public ModelAndView List(HttpServletRequest request, HttpServletResponse response,Product prod) throws Exception {
Map map=new HashMap();
List<EquipTypeJson> list = testService.getAllEquipType();//數據庫中獲取源數據
JSONArray jsonArray = JSONArray.fromObject(list); //將list數據轉為json對象
String json = jsonArray.toString(); //將json對象轉為字符串
map.put("jsonList", json);
return new ModelAndView("equip/List").addAllObjects(map);
}