//样式自行定义
<input id="orgName" class="org-select u-input input5" onclick="showTree()" readonly>
<!-- 模拟select点击框 以及option的text值显示-->
<i class="trg" style="position: absolute;"></i>
<!-- 模拟select右侧倒三角 -->
<input id="orgCode" type="hidden" name="orgCode" />
<!-- 存储 模拟select的value值 -->
<!-- zTree树状图 相对定位在其下方 -->
<div class="ztree" style="display:none; position: relative;border:1px solid #4aa5ff;width:300px;left: 80px;top: 0px;">
<ul id="treeDemo"></ul>
</div>
var setting = {
data: {
simpleData: {
enable: true
}
},
//回调
callback: {
onClick: zTreeOnClick
},
view: {
fontCss: { fontSize: "14px" }
}
};
//节点点击事件
function zTreeOnClick(event, treeId, treeNode) {
console.log(treeNode)
$('#orgName').val(treeNode.name);
$('#orgCode').val(treeNode.id)
hideTree();
};
function tree(){
//初始组织树状图
//渲染数据的字段是由于后端传给前端的字段不一致才改的,可以根据实际情况改
$.ajax({
type: 'get',
url: 'json/tree.json', //请求数据的地址
dataType: "json", //返回数据形式为json
success: function(param) {
var data=[]
for(var i=0;i<param.length;i++){
data.push({id:param[i].id,pId:param[i].pid,name:param[i].oname})
}
$.fn.zTree.init($("#treeDemo"), setting, data);
},
})
}
//下拉框显示 隐藏
function showTree(){
if($('.ztree').css('display') == 'none'){
$('.ztree').css('display','block');
} else{
$('.ztree').css('display','none');
}
$("body").bind("mousedown", onBodyDownByActionType);
}
function hideTree() {
$('.ztree').css('display','none');
$("body").unbind("mousedown", onBodyDownByActionType);
return false;
}
//区域外点击事件
function onBodyDownByActionType(event) {
if (event.target.id.indexOf('treeDemo') == -1){
if(event.target.id != 'selectDevType'){
hideTree();
}
}
}
json格式
[
{
"id":1,
"pid":0,
"oname":"网服"
},
{
"id":2,
"pid":1,
"oname":"广州分公司"
},
{
"id":3,
"pid":2,
"oname":"前端"
},
{
"id":4,
"pid":1,
"oname":"后端"
}
]
jq&&ztree树形下拉框
最新推荐文章于 2023-05-15 09:10:31 发布