jquery zTree異步加載、模糊搜索簡單實(shí)例分享
來源:易賢網(wǎng) 閱讀:3029 次 日期:2016-07-16 13:26:06
溫馨提示:易賢網(wǎng)小編為您整理了“jquery zTree異步加載、模糊搜索簡單實(shí)例分享”,方便廣大網(wǎng)友查閱!

本文實(shí)例為大家講解了jquery zTree樹插件的基本使用方法,具體內(nèi)容如下

一、節(jié)點(diǎn)模糊搜索功能:搜索成功后,自動(dòng)高亮顯示并定位、展開搜索到的節(jié)點(diǎn)。

二、節(jié)點(diǎn)異步加載:1、點(diǎn)擊展開時(shí)加載數(shù)據(jù);2、選中節(jié)點(diǎn)時(shí)加載數(shù)據(jù)。

前臺(tái)代碼如下:

<script type="text/javascript">

 //ztree設(shè)置

 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; //是否重新異步請(qǐng)求

 var checkFlag = true; //是否選中

 //節(jié)點(diǎn)展開前

 function zTreeBeforeExpand(treeId, treeNode) {

 reloadFlag = false;

 return true;

 };

 //節(jié)點(diǎn)創(chuàng)建后

 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");

 }

 }

 };

 //選中節(jié)點(diǎn)前

 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) { //如果結(jié)果不為空

  $.fn.zTree.init($("#tree"), setting, data);

  }

  else { //搜索不到結(jié)果

  }

 }

 });

 });

 //提交

 $("#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++) { //遍歷選擇的節(jié)點(diǎn)集合

 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));

 })

 });

 //查找節(jié)點(diǎn)

 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++) { //上次查詢的節(jié)點(diǎn)集合取消高亮

 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++) { //遍歷找到的節(jié)點(diǎn)集合

  if (nodeList[i].getParentNode()) {

  zTree.expandNode(nodeList[i].getParentNode(), true, false, false); //展開其父節(jié)點(diǎn)

  }

  nodeList[i].highlight = true;

  zTree.updateNode(nodeList[i]);

 }

 }

 zTree.refresh(); // 很重要,否則節(jié)點(diǎn)狀態(tài)更新混亂。

 lastNodeList = nodeList;

 lastKey = key;

 }

 }

 //加載數(shù)據(jù)

 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>

--------------------------------------------------------------------------

<div style="width: 200px; height: 260px; overflow: auto; border: solid 1px #666;">

 <ul id="tree" class="ztree">

 </ul>

 </div>

后臺(tái)代碼(后臺(tái)返回Json數(shù)據(jù)):

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 加載班級(jí)

//獲取當(dāng)前登錄用戶

Sys_User user = AdminSecurityUtils.GetLoginUser(ctx);

//獲取當(dāng)前用戶關(guān)聯(lián)的老師

Edu_Teacher teacher = edu_TeacService.FindByUserId(user.Id);

//獲取班級(jí)集合

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")

{

 //加載學(xué)生

 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);

}

三、基于cookie實(shí)現(xiàn)zTree樹刷新后,展開狀態(tài)不變

1、除了引用jQuery和zTree的JS外,引用cookie的JS:

復(fù)制代碼 代碼如下:

<script type="text/javascript" src="~/Scripts/jquery-treeview/lib/jquery.cookie.js"></script>

2、JS代碼:

$(function () {

 //ztree設(shè)置

 var setting = {

  data: {

   simpleData: {

    enable: true,

    idKey: "id",

    pIdKey: "pId",

    rootPId: null

   }

  },

  callback: {

   onExpand: onExpand,

   onCollapse: onCollapse

  }

 };

 $.ajax({

  type: "POST",

  url: "/Tech/TemplateTypeManage/GetData",

  success: function (data) {

   if (data && data.length != 0) {

    $.fn.zTree.init($("#tree"), setting, data);

    var treeObj = $.fn.zTree.getZTreeObj("tree");

    var cookie = $.cookie("z_tree" + window.location);

    if (cookie) {

     z_tree = JSON2.parse(cookie);

     for (var i = 0; i < z_tree.length; i++) {

      var node = treeObj.getNodeByParam('id', z_tree[i])

      treeObj.expandNode(node, true, false)

     }

    }

   }

  }

 });

});//end $

function onExpand(event, treeId, treeNode) {

 var cookie = $.cookie("z_tree" + window.location);

 var z_tree = new Array();

 if (cookie) {

  z_tree = JSON2.parse(cookie);

 }

 if ($.inArray(treeNode.id, z_tree) < 0) {

  z_tree.push(treeNode.id);

 }

 $.cookie("z_tree" + window.location, JSON2.stringify(z_tree))

}

function onCollapse(event, treeId, treeNode) {

 var cookie = $.cookie("z_tree" + window.location);

 var z_tree = new Array();

 if (cookie) {

  z_tree = JSON2.parse(cookie);

 }

 var index = $.inArray(treeNode.id, z_tree);

 z_tree.splice(index, 1);

 for (var i = 0; i < treeNode.children.length; i++) {

  index = $.inArray(treeNode.children[i].id, z_tree);

  if (index > -1) z_tree.splice(index, 1);

 }

 $.cookie("z_tree" + window.location, JSON2.stringify(z_tree))

}

以上就是關(guān)于樹插件zTree異步加載、模糊搜索簡單實(shí)例講解,希望對(duì)大家的學(xué)習(xí)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:jquery zTree異步加載、模糊搜索簡單實(shí)例分享
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)