jQuery使用zTree插件實(shí)現(xiàn)樹形菜單和異步加載
本文實(shí)例講解了jQuery使用zTree插件實(shí)現(xiàn)樹形菜單和異步加載,并且可以進(jìn)行編輯,分享給大家供大家參考,具體內(nèi)容如下
效果圖:

一、HTML代碼
<html>
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="zTree/css/demo.css" rel="stylesheet" />
<link href="zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
<script src="zTree/js/jquery-1.4.4.min.js"></script>
<script src="zTree/js/jquery.ztree.core-3.5.js"></script>
<script src="zTree/js/jquery.ztree.excheck-3.5.js"></script>
<script src="zTree/js/jquery.ztree.exedit-3.5.js"></script>
<script type="text/javascript">
var setting = {
async: {
enable: true,
url: "AjaxPage/GetAjax.aspx?z=sdfww234edfsd",
autoParam: ["id"],
dataFilter: filter,
contentType: "application/json",
type:"get"
},
view: {
expandSpeed: "",
addHoverDom: addHoverDom,
removeHoverDom: removeHoverDom,
selectedMulti: false
},
check: {
enable: true
},
edit: {
enable: true
},
data: {
simpleData: {
enable: true
}
},
callback: {
beforeRemove: beforeRemove,
beforeRename: beforeRename,
}
};
function filter(treeId, parentNode, childNodes) {
if (!childNodes) return null;
for (var i = 0, l = childNodes.length; i < l; i++) {
childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
}
return childNodes;
}
function beforeRemove(treeId, treeNode) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.selectNode(treeNode);
return confirm("確認(rèn)刪除 節(jié)點(diǎn) -- " + treeNode.name + " 嗎?");
}
function beforeRename(treeId, treeNode, newName) {
if (newName.length == 0) {
alert("節(jié)點(diǎn)名稱不能為空.");
return false;
}
return true;
}
var newCount = 1;
function addHoverDom(treeId, treeNode) {
var sObj = $("#" + treeNode.tId + "_span");
if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
+ "' title='add node' onfocus='this.blur();'></span>";
sObj.after(addStr);
console.log("add " + "#addBtn_" + treeNode.id);
var btn = $("#addBtn_" + treeNode.tId);
if (btn) btn.bind("click", function () {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.addNodes(treeNode, { id: (100 + newCount), pId: treeNode.id, name: "new node" + (newCount++) });
return false;
});
};
function removeHoverDom(treeId, treeNode) {
console.log("remove " + "#addBtn_" + treeNode.id);
$("#addBtn_" + treeNode.tId).unbind().remove();
};
$(document).ready(function () {
$.fn.zTree.init($("#treeDemo"), setting);
});
</script>
</head>
<body>
<ul id="treeDemo" class="ztree" style="width: 560px; overflow: auto;"></ul>
</body>
</html>
二、異步請求的后臺數(shù)據(jù):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CssStudyWeb.AjaxPage
{
public partial class GetAjax : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["z"] == "sdfww234edfsd")//根據(jù)會員卡號,查詢會員卡信息
{
StringBuilder sb = new StringBuilder();
sb.Append("[");
sb.Append("{\"id\":\"1\",\"name\":\"銷售單管理\",\"pId\":\"0\"},");
sb.Append("{\"id\":\"101\",\"name\":\"銷售單列表\",\"pId\":\"1\"},");
sb.Append("{\"id\":\"102\",\"name\":\"銷售單綜合查詢\",\"pId\":\"1\"},");
sb.Append("{\"id\":\"2\",\"name\":\"系統(tǒng)管理\",\"pId\":\"0\"},");
sb.Append("{\"id\":\"103\",\"name\":\"權(quán)限組管理\",\"pId\":\"2\"},");
sb.Append("{\"id\":\"104\",\"name\":\"權(quán)限菜單管理\",\"pId\":\"2\"}");
sb.Append("]");
Response.Write(sb.ToString());
}
}
}
}
更多關(guān)于ztree控件的內(nèi)容,請參考專題《jQuery插件ztree使用匯總》
以上就是zTree插件實(shí)現(xiàn)樹形菜單和異步加載的全部代碼,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
jQuery使用$.ajax進(jìn)行即時(shí)驗(yàn)證的方法
這篇文章主要介紹了jQuery使用$.ajax進(jìn)行即時(shí)驗(yàn)證的方法,較為詳細(xì)的分析了jQuery基于ajax實(shí)現(xiàn)前臺驗(yàn)證與后臺數(shù)據(jù)交互的相關(guān)技巧,非常簡單實(shí)用,需要的朋友可以參考下2015-12-12
CSS+Jquery實(shí)現(xiàn)頁面圓角框方法大全
前不久做項(xiàng)目,要用到大量的頁面圓角的框塊,以前實(shí)現(xiàn)的時(shí)候都是用圖片做背景之類的方法,那種方法對于少數(shù)的還是比較可行的,但是當(dāng)涉及到整個項(xiàng)目都要用 到這樣的效果時(shí)就顯得不夠優(yōu)化和簡練了。2009-12-12
基于jquery實(shí)現(xiàn)漂亮的動態(tài)信息提示效果
這個效果是在頁面頭部出現(xiàn)的,出現(xiàn)一下會自動隱藏,上面有x那個提示就要手動關(guān)閉一下,其他的都是自動隱藏的!2011-08-08
JS實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能兼容所有瀏覽器(推薦)
這篇文章主要介紹了JS實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能兼容所有瀏覽器(推薦)的相關(guān)資料,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
jQuery實(shí)現(xiàn)仿Alipay支付寶首頁全屏焦點(diǎn)圖切換特效
這篇文章主要介紹了jQuery實(shí)現(xiàn)仿Alipay支付寶首頁全屏焦點(diǎn)圖切換特效,涉及jQuery插件jquery.kinMaxShow的相關(guān)使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05
jQuery動態(tài)效果顯示人物結(jié)構(gòu)關(guān)系圖的方法
這篇文章主要介紹了jQuery動態(tài)效果顯示人物結(jié)構(gòu)關(guān)系圖的方法,涉及jQuery操作json結(jié)構(gòu)數(shù)據(jù)及鼠標(biāo)事件的技巧,需要的朋友可以參考下2015-05-05
jquery.form.js實(shí)現(xiàn)將form提交轉(zhuǎn)為ajax方式提交的方法
這篇文章主要介紹了jquery.form.js實(shí)現(xiàn)將form提交轉(zhuǎn)為ajax方式提交的方法,涉及jQuery插件實(shí)現(xiàn)form表單的Ajax提交技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04

