zTree实现访问到第一节点在相同水平当前所选节点数目

1、实现源代码

<!DOCTYPE html>
<html>
<head>
	<title>zTree实现基本树</title>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" type="text/css" href="../../../css/demo.css">
	<link rel="stylesheet" type="text/css" href="../../../css/zTreeStyle/zTreeStyle.css">
	<script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
	<script type="text/javascript" src="../../../js/jquery.ztree.core-3.5.js"></script>
	<script type="text/javascript">
		<!--
		var setting = {
			data: {
				simpleData: {
					enable: true
				}
			}
		};

		var zNodes =[
			{ id:1, pId:0, name:"湖北省", open:true},
			{ id:11, pId:1, name:"武汉市", open:true},
			{ id:111, pId:11, name:"汉口"},
			{ id:112, pId:11, name:"汉阳"},
			{ id:113, pId:11, name:"武昌"},
			{ id:12, pId:1, name:"黄石市"},
			{ id:121, pId:12, name:"黄石港区"},
			{ id:122, pId:12, name:"西塞山区"},
			{ id:123, pId:12, name:"下陆区"},
			{ id:124, pId:12, name:"铁山区"},
			{ id:13, pId:1, name:"黄冈市"},
			{ id:131, pId:13, name:"黄州区"},
			{ id:132, pId:13, name:"麻城市"},
			{ id:133, pId:13, name:"武穴市"},
			{ id:134, pId:13, name:"团风县"},
			{ id:135, pId:13, name:"浠水县"},
			{ id:136, pId:13, name:"罗田县"},
			{ id:137, pId:13, name:"英山县"},
			{ id:2, pId:0, name:"湖南省", open:true},
			{ id:21, pId:2, name:"长沙市", open:true},
			{ id:211, pId:21, name:"芙蓉区"},
			{ id:212, pId:21, name:"天心区"},
			{ id:213, pId:21, name:"岳麓区"},
			{ id:214, pId:21, name:"开福区"},
			{ id:22, pId:2, name:"株洲市"},
			{ id:221, pId:22, name:"天元区"},
			{ id:222, pId:22, name:"荷塘区"},
			{ id:223, pId:22, name:"芦淞区"},
			{ id:224, pId:22, name:"石峰区"}
		];

		$(document).ready(function(){
			$.fn.zTree.init($("#baseTree"), setting, zNodes);
		});

		/**
		 * 获取当前选中的第一个节点在同级节点中的序号
		 */
		function indexNodes()
		{
		    var treeObj = $.fn.zTree.getZTreeObj("baseTree");
		    //获取选中的节点
			var nodes = treeObj.getSelectedNodes();
			if (nodes.length>0)
			{
			    for(var i=0;i<nodes.length;i++)
			    {
			        var index = treeObj.getNodeIndex(nodes[i]);
			        alert("获取当前选中的第一个节点在同级节点中的序号:"+index);
			    }
			}
		}
		//-->
	</script>
 </head>

<body>
<div class="content_wrap">
	<div class="zTreeDemoBackground left">
		<ul id="baseTree" class="ztree" style="height: 300px; width:200px; overflow-y: auto"></ul>
		<input type="button" id="btn" onclick="indexNodes()" value="获取当前选中的第一个节点在同级节点中的序号"/>
	</div>
</div>
</body>
</html>

2、实现结果

(1)初始化

(2)选择“汉口”时

(3)选择“汉阳”时

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveW91MjNoYWk0NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="585" height="348" border="1" >

3、源代码说明

var index = treeObj.getNodeIndex(nodes[i]);
alert("获取当前选中的第一个节点在同级节点中的序号:"+index);

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-12-19 12:02:19

zTree实现访问到第一节点在相同水平当前所选节点数目的相关文章

Ztree _ 横向显示子节点、点击文字勾选、去除指定元素input的勾选状态

前些天项目需要树结构表现数据,需求ztree就能满足所以直接使用ztree只是踩了些小坑... 1.ztree子节点横向显示(下图): 效果说明:第三级子节点按需求横向显示其他竖向显示,每行最多显示5个(修改位置在zTreeStyle.css里面哦,在外面设置不上的,页面也获取不到想要设置样式的节点...如果你能还望赐教)代码如下: 1 .ztree>li>ul>li>ul>li>ul{overflow:hidden;} 2 .ztree>li>ul>

zTree实现单独选中根节点中第一个节点

1.实现源码 <!DOCTYPE html> <html> <head> <title>zTree实现基本树</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href=&

jquery zTree 查找所有的叶子节点

// 保存所有叶子节点 10 为初始化大小,并非数组上限 var arrayObj = new Array([10]); /* treeNode: 根节点 result: 用于保存叶子节点的数组 */ // 获取所有叶子节点 function getAllLeafNodes(treeNode, result) { if (treeNode.isParent) { var childrenNodes = treeNode.children; if (childrenNodes) { for (va

zTree实现删除树子节点

1.实现源码 <!DOCTYPE html> <html> <head> <title>zTree实现基本树</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href=&

使用zTree和json构建树节点

我们常常碰到需要构建树结构展示的情况,我推荐使用zTree和JSON. 例如: <?php /** * * 使用zTree和json构建树节点 * */ $arr = array( 0=>array( 'id' => 1, 'pid' => 0, 'name' => 'china', 'son' => array( array( 'id' => 3, 'pid' => 1, 'name' => 'Beijing', ), array( 'id' =&g

zTree自动点击第一个节点

var setting = { view: { dblClickExpand: true,//双击展开 showLayer: false, selectedMulti: false,//是否允许多选 showIcon: showIconForTree//显示节点小图标 }, data: { simpleData: { enable: true, idKey: "id", pIdKey: "pId", rootPId: "0" } }, callb

zTree自动点击第一个节点(转载)

var setting = { view: { dblClickExpand: true,//双击展开 showLayer: false, selectedMulti: false,//是否允许多选 showIcon: showIconForTree//显示节点小图标 }, data: { simpleData: { enable: true, idKey: "id", pIdKey: "pId", rootPId: "0" } }, callb

ztree获取最顶层的节点

<!DOCTYPE html> <html> <head> <title>zTree实现基本树</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="../../../css/demo.css"

zTree判断是否为父节点

var treeObj = $.fn.zTree.getZTreeObj("tree"); var nodes = treeObj.getSelectedNodes(); if(typeof nodes[0].children.length>0){ //为父节点 }