zTree 樹插件實(shí)現(xiàn)全國五級地區(qū)點(diǎn)擊后加載的示例
在項(xiàng)目功能中需要錄入戶籍地和現(xiàn)居住地,為減少用戶輸入量,將使用樹插件選擇全國五級地區(qū)+輸入框輸入詳細(xì)地址。這里優(yōu)先使用了zTree樹插件。為了以后使用學(xué)習(xí),在這里進(jìn)行相關(guān)記錄。當(dāng)然在實(shí)現(xiàn)過程中參考各大神的文章是必不可少的,可以結(jié)合了自己的實(shí)際需求進(jìn)行快速解決問題。
zTree 樹插件官網(wǎng)簡介
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ù)庫地區(qū)表基本結(jié)構(gòu):
regionType 地區(qū)級別 path 地區(qū)編碼 name 地區(qū)名稱 parentRegion 上級地區(qū)
頁面代碼:
<!-- 戶籍地、現(xiàn)居住地 -->
<tr>
<td colspan="3">
<div class="form-group">
<label style="display: block;">戶籍地</label>
<input type="hidden" name="domiciliary" id="domiciliary">
<input type="text" class="form-control" style="width:300px;float:left;" id="domiciliary-text" value="" onclick="showRegion('domiciliary')" placeholder="點(diǎn)擊選擇地區(qū)" maxlength="20" readonly="readonly">
<input type="text" class="form-control" style="width:320px;float:left;" name="domiciliaryAddress" value="" placeholder="詳細(xì)地址" maxlength="100">
</div>
</td>
</tr>
<tr>
<td colspan="3">
<div class="form-group">
<label style="display: block;">現(xiàn)居住地址</label>
<input type="hidden" name="bide" id="bide">
<input type="text" class="form-control" style="width:300px;float:left;" id="bide-text" value="" onclick="showRegion('bide')" placeholder="點(diǎn)擊選擇地區(qū)" maxlength="20" readonly="readonly">
<input type="text" class="form-control" style="width:320px;float:left;" name="bideAddress" value="" placeholder="詳細(xì)地址" maxlength="100">
</div>
</td>
</tr>
<!-- bootstrap 模態(tài)框(Modal) -->
<div class="modal fade" id="regionModal" tabindex="-1" role="dialog" aria-hidden="true">
<input type="hidden" id="regionModalType" />
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<!-- zTree 的容器 -->
<ul id="treeRegion" class="ztree"></ul>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
<button type="button" class="btn btn-primary" onclick="confimRegion()">確認(rèn)</button>
</div>
</div>
</div>
</div>
效果:

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, //是否采用簡單數(shù)據(jù)模式 (Array)。默認(rèn)值:false
idKey: "path", //節(jié)點(diǎn)數(shù)據(jù)中保存唯一標(biāo)識的屬性名稱。
pIdKey: "parentRegion", //節(jié)點(diǎn)數(shù)據(jù)中保存其父節(jié)點(diǎn)唯一標(biāo)識的屬性名稱。
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 對象
// 這里判斷節(jié)點(diǎn)被點(diǎn)擊時(shí),如果有已經(jīng)加載下級節(jié)點(diǎn),則不用請求服務(wù)器
if((treeNode.children == null || treeNode.children == "undefined")){
if(!$("#"+treeNode.tId+"_switch").hasClass("center_docu") && !$("#"+treeNode.tId+"_switch").hasClass("bottom_docu")){
// 請求服務(wù)器,獲得點(diǎn)擊地區(qū)的下級地區(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("請求失敗!");
}
});
}
}else{
// 展開當(dāng)前節(jié)點(diǎn)
treeObj.expandNode(treeNode);
}
}
}
};
// 顯示區(qū)域樹,加載頂級節(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對象
var treeObj = $.fn.zTree.getZTreeObj("treeRegion");
// 獲得初始化的所有節(jié)點(diǎn),即頂級節(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)全國五級地區(qū)點(diǎn)擊后加載的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery封裝placeholder效果實(shí)現(xiàn)方法,讓低版本瀏覽器支持該效果
下面小編就為大家?guī)硪黄猨Query封裝placeholder效果實(shí)現(xiàn)方法,讓低版本瀏覽器支持該效果。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
input file樣式修改以及圖片預(yù)覽刪除功能詳細(xì)概括(推薦)
這篇文章主要介紹了input file樣式修改以及圖片預(yù)覽刪除功能,input file 按鈕改成自己想要的樣式以及.圖片預(yù)覽功能的實(shí)現(xiàn),具體操作步驟大家可查看下文的詳細(xì)講解,感興趣的小伙伴們可以參考一下。2017-08-08
基于jQuery實(shí)現(xiàn)的仿百度首頁滑動選項(xiàng)卡效果代碼
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)的仿百度首頁滑動選項(xiàng)卡效果代碼,涉及jQuery響應(yīng)鼠標(biāo)事件實(shí)現(xiàn)頁面元素動態(tài)變換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
點(diǎn)擊表單提交時(shí)出現(xiàn)jQuery沒有權(quán)限的解決方法
擊表單提交的時(shí)候會出現(xiàn) jQuery 沒有權(quán)限,試了一下jquery自帶的json方式提交成功2014-07-07
jQuery實(shí)現(xiàn)簡單漂亮的Nav導(dǎo)航菜單效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)簡單漂亮的Nav導(dǎo)航菜單效果,涉及jQuery響應(yīng)鼠標(biāo)事件動態(tài)遍歷與操作頁面元素屬性的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03

