最近要做一个树结构,就使用了jQuery 的树插件,感觉还不错,做个随笔
页面的基本结构是这样的
这里的样式是使用了metroStyle文件夹里的类bootstrap风格,当然首先需要下载ztree插件,直接百度即可,下载完成后由对应的api和一些例子,这里使用的是复选框模板
jQuery zTree树的nofollow" target="_blank" href="http://www.treejs.cn/v3/main.PHP#_zTreeInfo">下载链接
页面jsp如下:
<script type="text/javascript">
var setting = {
check: {
enable: true
},data: {
simpleData: {
enable: true
}
},callback:{
onCheck:onCheck
}
};
// var zNodes =[
// { id:1,pId:0,name:"随意勾选 1",open:true},// { id:11,pId:1,name:"随意勾选 1-1",open:false},// { id:111,pId:11,name:"随意勾选 1-1-1"},// { id:112,name:"随意勾选 1-1-2"},// { id:12,name:"随意勾选 1-2",// { id:121,pId:12,name:"随意勾选 1-2-1"},// { id:122,name:"随意勾选 1-2-2"},// { id:2,name:"随意勾选 2",checked:false,// { id:21,pId:2,name:"随意勾选 2-1"},// { id:22,name:"随意勾选 2-2",// { id:221,pId:22,name:"随意勾选 2-2-1",checked:false},// { id:222,name:"随意勾选 2-2-2"},// { id:23,name:"随意勾选 2-3"},// { id:3,name:"随意勾选 3",// { id:231,pId:3,name:"随意勾选 3-1"},// { id:232,name:"随意勾选 3-2",// { id:2321,pId:232,name:"随意勾选 3-2-1",// { id:2322,name:"随意勾选 3-2-2"},// { id:233,name:"随意勾选 3-3"}
// ];
var code;
function setCheck() {
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),py = $("#py").attr("checked")? "p":"",sy = $("#sy").attr("checked")? "s":"",pn = $("#pn").attr("checked")? "p":"",sn = $("#sn").attr("checked")? "s":"",type = { "Y":py + sy,"N":pn + sn};
zTree.setting.check.chkBoxType = type;
// showCode('setting.check.chkBoxType = { "Y" : "' + type.Y + '","N" : "' + type.N + '" };');
// showCode('setting.check.chkBoxType = { "Y" : "","N" : "" };');
showCode('setting.check.chkBoxType = { "Y" : "s","N" : "ps" };');
// setting.check.chkBoxType = { "Y" : "","N" : "" };
}
function showCode(str) {
if (!code) code = $("#code");
code.empty();
code.append("
}
var zNodes =[];
$(document).ready(function(){
var t = $("#treeDemo");
$.ajax({
type: "POST",url: "/Units/ListTree",dataType: 'json',success: function(result) {
console.log(result);
$.extend( true,zNodes,result );
console.log(zNodes);
t = $.fn.zTree.init(t,setting,zNodes);
// demoIframe = $("#testIframe");
}
});
});
$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"),zNodes);
setCheck();
$("#py").bind("change",setCheck);
$("#sy").bind("change",setCheck);
$("#pn").bind("change",setCheck);
$("#sn").bind("change",setCheck);
});
function onCheck(e,treeId,treeNode){
var treeObj=$.fn.zTree.getZTreeObj("treeDemo"),nodes=treeObj.getCheckedNodes(true),v="";
for(var i=0;i<nodes.length;i++){
v+=nodes[i].name + ",";
alert(nodes[i].id); //获取选中节点的值
}
}