您好,登錄后才能下訂單哦!
一、節點模糊搜索功能:搜索成功后,自動高亮顯示并定位、展開搜索到的節點。
二、節點異步加載:1、點擊展開時加載數據;2、選中節點時加載數據。
前臺代碼如下:
js:
<script type="text/javascript"> //ztree設置 var setting = { view: { fontCss: getFontCss }, check: { enable: true }, data: { simpleData: { enable: true, idKey: "id", pIdKey: "pId", rootPId: 0 } }, async: { enable: true, url: "#{getStudentsJsonUrl}", autoParam: ["id", "level"] }, callback: { beforeCheck: zTreeBeforeCheck, onNodeCreated: zTreeOnNodeCreated, beforeExpand: zTreeBeforeExpand } }; var reloadFlag = false; //是否重新異步請求 var checkFlag = true; //是否選中 //節點展開前 function zTreeBeforeExpand(treeId, treeNode) { reloadFlag = false; return true; }; //節點創建后 function zTreeOnNodeCreated(event, treeId, treeNode) { var zTree = $.fn.zTree.getZTreeObj(treeId); if (reloadFlag) { if (checkFlag) { zTree.checkNode(treeNode, true, true); } if (!treeNode.children) { zTree.reAsyncChildNodes(treeNode, "refresh"); } } }; //選中節點前 function zTreeBeforeCheck(treeId, treeNode) { var zTree = $.fn.zTree.getZTreeObj(treeId); if (!treeNode.children) { reloadFlag = true; checkFlag = true; zTree.reAsyncChildNodes(treeNode, "refresh"); } return true; } //頁面加載完成 _run(function () { require(['zTree/js/jquery.ztree.all-3.5'], function () { $.ajax({ type: "POST", url: "#{getStudentsJsonUrl}", success: function (data) { if (data && data.length != 0) { //如果結果不為空 $.fn.zTree.init($("#tree"), setting, data); } else { //搜索不到結果 } } }); }); //提交 $("#inputSubmit").click(function () { var zTree = $.fn.zTree.getZTreeObj("tree"); var nodes = zTree.getCheckedNodes(true); var ids = ""; var names = ""; for (var i = 0; i < nodes.length; i++) { //遍歷選擇的節點集合 if (!nodes[i].isParent) { ids += nodes[i].id.replace("level" + nodes[i].level, "") + ","; names += nodes[i].name + ","; } } Simpo.ui.box.hideBox(); parent.$(".boxFrm").contents().find("#inputRange").val(names.substr(0, names.length - 1)); parent.$(".boxFrm").contents().find("#hidRange").val(ids.substr(0, ids.length - 1)); }) }); //查找節點 var lastNodeList = []; var lastKey; function searchNode() { var zTree = $.fn.zTree.getZTreeObj("tree"); var key = $.trim($("#inputSearchNode").val()); if (key != "" && key != lastKey) { nodeList = zTree.getNodesByParamFuzzy("name", key); for (var i = 0, l = lastNodeList.length; i < l; i++) { //上次查詢的節點集合取消高亮 lastNodeList[i].highlight = false; zTree.updateNode(lastNodeList[i]); } zTree.expandAll(false); //全部收縮 if (nodeList.length > 0) { for (var i = 0, l = nodeList.length; i < l; i++) { //遍歷找到的節點集合 if (nodeList[i].getParentNode()) { zTree.expandNode(nodeList[i].getParentNode(), true, false, false); //展開其父節點 } nodeList[i].highlight = true; zTree.updateNode(nodeList[i]); } } zTree.refresh(); // 很重要,否則節點狀態更新混亂。 lastNodeList = nodeList; lastKey = key; } } //加載數據 function loadData() { var zTree = $.fn.zTree.getZTreeObj("tree"); var rootNodes = zTree.getNodes(); reloadFlag = true; checkFlag = false; for (var i = 0; i < rootNodes.length; i++) { if (!rootNodes[i].children) { zTree.reAsyncChildNodes(rootNodes[i], "refresh"); //異步加載 } } } //全部收縮 function closeAll() { var zTree = $.fn.zTree.getZTreeObj("tree"); if ($("#inputCloseAll").val() == "全部收縮") { zTree.expandAll(false); $("#inputCloseAll").val("全部展開") } else { zTree.expandAll(true); $("#inputCloseAll").val("全部收縮") } } //高亮樣式 function getFontCss(treeId, treeNode) { return (treeNode.highlight) ? { color: "#A60000", "font-weight": "bold"} : { color: "#333", "font-weight": "normal" }; } </script>
html:
<div > <ul id="tree" class="ztree"> </ul> </div>
后臺代碼(后臺返回Json數據):
public void SelStudent() { set("getStudentsJsonUrl", to(GetStudentsJson)); } public void GetStudentsJson() { List<Dictionary<string, string>> dicList = new List<Dictionary<string, string>>(); string level = ctx.Post("level"); string id = ctx.Post("id"); if (strUtil.IsNullOrEmpty(id)) { #region 加載班級 //獲取當前登錄用戶 Sys_User user = AdminSecurityUtils.GetLoginUser(ctx); //獲取當前用戶關聯的老師 Edu_Teacher teacher = edu_TeacService.FindByUserId(user.Id); //獲取班級集合 List<Edu_ClaNameFlow> list = edu_ClaNameFlowService.GetListByTeacherId(teacher.Id); foreach (Edu_ClaNameFlow item in list) { Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add("id", "level0" + item.Calss.Id.ToString()); dic.Add("pId", "0"); dic.Add("name", item.Gra.Name + item.Calss.Name); dic.Add("isParent", "true"); dicList.Add(dic); } #endregion } else { if (level == "0") { //加載學生 List<Edu_Student> list = edu_StudService.GetListByClassId(id.Replace("level0", "")); foreach (Edu_Student item in list) { Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add("id", "level1" + item.Id.ToString()); dic.Add("pId", id); dic.Add("name", item.Name); dic.Add("isParent", "false"); dicList.Add(dic); } } } echoJson(dicList); }
更多關于ztree控件的內容,請參考專題《jQuery插件ztree使用匯總》 。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。