這篇文章主要為大家展示了“zTree樹插件如何實(shí)現(xiàn)全國(guó)五級(jí)地區(qū)點(diǎn)擊后加載”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“zTree樹插件如何實(shí)現(xiàn)全國(guó)五級(jí)地區(qū)點(diǎn)擊后加載”這篇文章吧。
成都創(chuàng)新互聯(lián)專業(yè)提供四川移動(dòng)機(jī)房托管服務(wù),為用戶提供五星數(shù)據(jù)中心、電信、雙線接入解決方案,用戶可自行在線購(gòu)買四川移動(dòng)機(jī)房托管服務(wù),并享受7*24小時(shí)金牌售后服務(wù)。
zTree 樹插件官網(wǎng)簡(jiǎn)介
zTree 是一個(gè)依靠 jQuery 實(shí)現(xiàn)的多功能 “樹插件”。優(yōu)異的性能、靈活的配置、多種功能的組合是 zTree 最大優(yōu)點(diǎn)。
zTree 樹插件官網(wǎng)地址
http://www.treejs.cn/v3/main.php#_zTreeInfo
功能實(shí)現(xiàn)代碼
數(shù)據(jù)庫(kù)地區(qū)表基本結(jié)構(gòu):
regionType 地區(qū)級(jí)別 path 地區(qū)編碼 name 地區(qū)名稱 parentRegion 上級(jí)地區(qū)
頁(yè)面代碼:
效果:
js代碼:
$(document).ready(function() { // zTree 參數(shù)配置 var setting = { view: { showIcon: false,//是否顯示節(jié)點(diǎn)的圖標(biāo) selectedMulti: false //設(shè)置是否允許同時(shí)選中多個(gè)節(jié)點(diǎn)。默認(rèn)值: true。 }, data: { simpleData: { enable: true, //是否采用簡(jiǎn)單數(shù)據(jù)模式 (Array)。默認(rèn)值:false idKey: "path", //節(jié)點(diǎn)數(shù)據(jù)中保存唯一標(biāo)識(shí)的屬性名稱。 pIdKey: "parentRegion", //節(jié)點(diǎn)數(shù)據(jù)中保存其父節(jié)點(diǎn)唯一標(biāo)識(shí)的屬性名稱。 rootPid: "10000000000000" //用于修正根節(jié)點(diǎn)父節(jié)點(diǎn)數(shù)據(jù),即 pIdKey 指定的屬性值。 } }, callback: { // 用于捕獲節(jié)點(diǎn)被點(diǎn)擊的事件回調(diào)函數(shù) onClick: function(event, treeId, treeNode, clickFlag) { var treeObj = $.fn.zTree.getZTreeObj(treeId); //根據(jù) treeId 獲取 zTree 對(duì)象 // 這里判斷節(jié)點(diǎn)被點(diǎn)擊時(shí),如果有已經(jīng)加載下級(jí)節(jié)點(diǎn),則不用請(qǐng)求服務(wù)器 if((treeNode.children == null || treeNode.children == "undefined")){ if(!$("#"+treeNode.tId+"_switch").hasClass("center_docu") && !$("#"+treeNode.tId+"_switch").hasClass("bottom_docu")){ // 請(qǐng)求服務(wù)器,獲得點(diǎn)擊地區(qū)的下級(jí)地區(qū) $.ajax({ type: "get", async: false, url: "tRegion/ajaxArea", data:{ path:treeNode.path }, dataType:"json", success: function(data){ if(data != null && data.length != 0){ //添加新節(jié)點(diǎn) var newNodes = treeObj.addNodes(treeNode, data); $(newNodes).each(function(i,n){ var id = n.tId+"_switch"; if($("#"+id).hasClass("center_docu")){ $("#"+id).removeClass("center_docu"); $("#"+id).addClass("center_close"); } if($("#"+id).hasClass("bottom_docu")){ $("#"+id).removeClass("bottom_docu"); $("#"+id).addClass("bottom_close"); } }); }else{ var id = treeNode.tId+"_switch"; if($("#"+id).hasClass("center_close")){ $("#"+id).removeClass("center_close"); $("#"+id).addClass("center_docu"); } if($("#"+id).hasClass("bottom_close")){ $("#"+id).removeClass("bottom_close"); $("#"+id).addClass("bottom_docu"); } } }, error:function(event, XMLHttpRequest, ajaxOptions, thrownError){ result = true; toastr.error("請(qǐng)求失敗!"); } }); } }else{ // 展開(kāi)當(dāng)前節(jié)點(diǎn) treeObj.expandNode(treeNode); } } } }; // 顯示區(qū)域樹,加載頂級(jí)節(jié)點(diǎn) $.ajax({ type: "get", url: "tRegion/ajaxArea", data: {path:"10000000000000"}, success: function(data, status) { if (status == "success") { // 初始化區(qū)域樹 $.fn.zTree.init($("#treeRegion"), setting, data); // 獲得zTree對(duì)象 var treeObj = $.fn.zTree.getZTreeObj("treeRegion"); // 獲得初始化的所有節(jié)點(diǎn),即頂級(jí)節(jié)點(diǎn) var nodes = treeObj.getNodes(); $(nodes).each(function(i,n){ var id = n.tId+"_switch"; if($("#"+id).hasClass("roots_docu")){ $("#"+id).removeClass("roots_docu"); $("#"+id).addClass("roots_close"); } if($("#"+id).hasClass("center_docu")){ $("#"+id).removeClass("center_docu"); $("#"+id).addClass("center_close"); } if($("#"+id).hasClass("bottom_docu")){ $("#"+id).removeClass("bottom_docu"); $("#"+id).addClass("bottom_close"); } }); } }, error : function() { toastr.error('Error'); }, }); }); function showRegion(type){ // 顯示模態(tài)框 $('#regionModal').modal('show'); $("#regionModalType").val(type); } // 選擇地區(qū)確認(rèn) function confimRegion(){ var type = $("#regionModalType").val(); var treeObj = $.fn.zTree.getZTreeObj("treeRegion"); var node = treeObj.getSelectedNodes(); //選中節(jié)點(diǎn) var regionType = node[0].regionType; if(Number(regionType) >= 5){ $("#"+type+"-text").val(node[0].name); $("#"+type).val(node[0].path); $('#regionModal').modal('hide'); } }
實(shí)現(xiàn)效果:
以上是“zTree樹插件如何實(shí)現(xiàn)全國(guó)五級(jí)地區(qū)點(diǎn)擊后加載”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!