Heim > Backend-Entwicklung > PHP-Tutorial > Ztree + PHP 无限极节点 递归查找节点法_PHP教程

Ztree + PHP 无限极节点 递归查找节点法_PHP教程

WBOY
Freigeben: 2016-07-13 10:13:31
Original
902 Leute haben es durchsucht

Ztree + PHP 无限极节点 递归查找节点法

一、前言

简单的描述一下,实习几个原理,思想,其实写很多东西,思想算是最重要的。

1、目标:将写一个无限节点的树形目录结构,如下图

步骤:

1、你的下载 插件  ztree。然后布置在你的项目中。

<ol class="dp-c"><li class="alt"><span><span><script src=</span><span class="string">"__PUBLIC__/js/jquery-1.4.4.min.js"</span><span>></script> </span></span></li><li><span><script src=<span class="string">"__PUBLIC__/js/jquery.ztree.core-3.5.js"</span><span>></script> </span></span></li></ol>
Nach dem Login kopieren

2、相关CSS

<ol class="dp-css"><li class="alt"><span><span><link rel=</span><span class="string">"stylesheet"</span><span> href=</span><span class="string">"__PUBLIC__/css/zTreeStyle/zTreeStyle.css"</span><span> type=</span><span class="string">"text/css"</span><span>> </span></span></li><li><span><link rel=<span class="string">"stylesheet"</span><span> href=</span><span class="string">"__PUBLIC__/css/zTree.css"</span><span> type=</span><span class="string">"text/css"</span><span>> </span></span></li></ol>
Nach dem Login kopieren

以上CSS 和JS 以你自己的为准。

3、目录结构DIV

<ol class="dp-css"><li class="alt"><span><span><div class=</span><span class="string">"content_wrap"</span><span>  style=</span><span class="string">"background:#666;"</span><span>> </span></span></li><li><span>    <div class=<span class="string">"zTreeDemoBackground left"</span><span>> </span></span></li><li class="alt"><span>        <ul id=<span class="string">"treeDemo"</span><span> class=</span><span class="string">"ztree"</span><span>></ul> </span></span></li><li><span>    </div> </span></li><li class="alt"><span></div> </span></li><li><span><div class=<span class="string">"content-text"</span><span> id=</span><span class="string">"text"</span><span>></div> </span></span></li></ol>
Nach dem Login kopieren

4,自己单独js中的代码

<ol class="dp-c"><li class="alt"><span><span><SCRIPT  src=</span><span class="string">"__PUBLIC__/js/ztreeonload.js"</span><span>></SCRIPT> </span></span></li></ol>
Nach dem Login kopieren

里面写的相关功能 及配置!

<ol class="dp-c"><li class="alt"><span><span class="comment">//配置项</span><span> </span></span></li><li><span><span class="keyword">var</span><span> setting = { </span></span></li><li class="alt"><span>     isSimpleData : true,              <span class="comment">//数据是否采用简单 Array 格式,默认false  性  </span><span> </span></span></li><li><span>     showLine : true,                  <span class="comment">//是否显示节点间的连线  </span><span> </span></span></li><li class="alt"><span>     checkable : true,    </span></li><li><span>     callback: { </span></li><li class="alt"><span>         onClick: zTreeOnClick       </span></li><li><span>     } </span></li><li class="alt"><span> }; </span></li><li><span> </span></li><li class="alt"><span> <span class="keyword">var</span><span> zNodes;</span><span class="comment">//数据变量</span><span> </span></span></li><li><span>   </span></li><li class="alt"><span> <span class="comment">//ajax提交数据,请求后台PHP处理返回出目录结构json数据</span><span> </span></span></li><li><span> $.ajax({ </span></li><li class="alt"><span>     url:<span class="string">"/admin.php/Ztree"</span><span>, </span></span></li><li><span>     type: <span class="string">"get"</span><span>, </span></span></li><li class="alt"><span>     async: false, </span></li><li><span>     dataType:<span class="string">"json"</span><span>,   </span></span></li><li class="alt"><span>     success: <span class="keyword">function</span><span> (data) { </span></span></li><li><span>             <span class="comment">//alert(data);</span><span> </span></span></li><li class="alt"><span>             zNodes=data;    <span class="comment">//将请求返回的数据存起来</span><span> </span></span></li><li><span>              <span class="comment">//alert(zNodes);</span><span> </span></span></li><li class="alt"><span>     }, </span></li><li><span>     error: <span class="keyword">function</span><span> (){</span><span class="comment">//请求失败处理函数  </span><span> </span></span></li><li class="alt"><span>         alert(<span class="string">'请求失败'</span><span>);   </span></span></li><li><span>     },   </span></li><li class="alt"><span> }) </span></li><li><span>   </span></li><li class="alt"><span> <span class="comment">//初始化ztree目录结构视图!</span><span> </span></span></li><li><span> $(document).ready(<span class="keyword">function</span><span>(){ </span></span></li><li class="alt"><span>     <span class="comment">//alert("111");</span><span> </span></span></li><li><span>     $.fn.zTree.init($(<span class="string">"#treeDemo"</span><span>), setting, zNodes); </span></span></li><li class="alt"><span> }); </span></li></ol>
Nach dem Login kopieren

5、后台PHP 递归算法,从数据库中查找目录结构并且生成 JSON数据

地址:如4中,AJAX所请求的 /admin.php/Ztree】我这里是用的ThinkPHP框架,所以url是这个样子,以你自己的接口文件为准!

<ol class="dp-c"><li class="alt"><span><span><?php </span></span></li><li><span>            <span class="comment">//父节点数组</span><span> </span></span></li><li class="alt"><span>            <span class="vars">$arr</span><span>=</span><span class="keyword">array</span><span>(); </span></span></li><li><span>            <span class="vars">$arr_str0</span><span> = </span><span class="keyword">array</span><span>(</span><span class="string">"name"</span><span> =></span><span class="string">'函数库查询'</span><span>,</span><span class="string">'children'</span><span>=></span><span class="vars">$this</span><span>->SelectSon(1));       </span><span class="comment">//父节点  Pid=1;</span><span> </span></span></li><li class="alt"><span>            <span class="vars">$arr_str1</span><span> = </span><span class="keyword">array</span><span>(</span><span class="string">"name"</span><span> =></span><span class="string">'数据库查询'</span><span>,</span><span class="string">'children'</span><span>=></span><span class="vars">$this</span><span>->SelectSon(2));       </span><span class="comment">//父节点  Pid=2;</span><span> </span></span></li><li><span>  </span></li><li class="alt"><span>            <span class="func">array_push</span><span>(</span><span class="vars">$arr</span><span>, </span><span class="vars">$arr_str0</span><span>); </span></span></li><li><span>            <span class="func">array_push</span><span>(</span><span class="vars">$arr</span><span>, </span><span class="vars">$arr_str1</span><span>);</span><span class="comment">//这里是2个父节点。</span><span> </span></span></li><li class="alt"><span>              </span></li><li><span>            <span class="func">echo</span><span>(json_encode(</span><span class="vars">$arr</span><span>)); </span><span class="comment">//这是最后返回给页面,也就是返回给AJAX请求后所得的返回数据 JSON数据</span><span> </span></span></li><li class="alt"><span>?> </span></li><li><span>  </span></li><li class="alt"><span><span class="comment">//这里仅仅是一个方法,一个调用SelectSon()方法,返回一个数组集合!但其中用的是递归!</span><span> </span></span></li><li><span><?php </span></li><li class="alt"><span>        <span class="comment">//查找子节点        Pid=父节点ID</span><span> </span></span></li><li><span>        <span class="keyword">private</span><span> </span><span class="keyword">function</span><span> SelectSon(</span><span class="vars">$Pid</span><span>){ </span></span></li><li class="alt"><span>  </span></li><li><span>            <span class="vars">$m</span><span>=M(</span><span class="string">'ztree'</span><span>); </span></span></li><li class="alt"><span>  </span></li><li><span>            <span class="keyword">if</span><span>((</span><span class="vars">$info</span><span>=</span><span class="vars">$m</span><span>->where(</span><span class="string">"Pid='$Pid'"</span><span>)->select())) </span><span class="comment">//查找该父ID下的子ID</span><span> </span></span></li><li class="alt"><span>            { </span></li><li><span>                <span class="vars">$data</span><span>=</span><span class="keyword">array</span><span>(); </span></span></li><li class="alt"><span>                <span class="keyword">for</span><span> (</span><span class="vars">$i</span><span>=0; </span><span class="vars">$i</span><span> < </span><span class="func">count</span><span>(</span><span class="vars">$info</span><span>) ; </span><span class="vars">$i</span><span>++)  </span></span></li><li><span>                {  </span></li><li class="alt"><span>                    <span class="vars">$da</span><span>=</span><span class="keyword">array</span><span>(</span><span class="string">"name"</span><span> =></span><span class="vars">$info</span><span>[</span><span class="vars">$i</span><span>][</span><span class="string">'name'</span><span>],</span><span class="string">'children'</span><span>=></span><span class="vars">$this</span><span>->SelectSon(</span><span class="vars">$info</span><span>[</span><span class="vars">$i</span><span>][</span><span class="string">'id'</span><span>]));  </span><span class="comment">//递归算法!</span><span> </span></span></li><li><span>                      </span></li><li class="alt"><span>                    <span class="func">array_push</span><span>(</span><span class="vars">$data</span><span>, </span><span class="vars">$da</span><span>);</span><span class="comment">//加入子节点数组</span><span> </span></span></li><li><span>                }; </span></li><li class="alt"><span>                  </span></li><li><span>                <span class="keyword">return</span><span> </span><span class="vars">$data</span><span>;</span><span class="comment">//一次性返回子节点数组,他们成为同级子节点。</span><span> </span></span></li><li class="alt"><span>            } </span></li><li><span>            <span class="keyword">else</span><span> </span></span></li><li class="alt"><span>            { </span></li><li><span>                <span class="keyword">return</span><span> null; </span></span></li><li class="alt"><span>            } </span></li><li><span>              </span></li><li class="alt"><span>        } </span></li><li><span>?> </span></li></ol>
Nach dem Login kopieren

注意:由于我是用的thinkphp框架。所以在方法调用上 有些不同,纯PHP文件中,思路应该是一样的,

首先是: 写一个数组。一个父节点的数组。

其次: 写一个方法,传递的参数是 父节点的ID,查询其子节点,在子节点中查询之后,用递归的方式继续查找子节点的子节点,直到最后查询完毕之后,返回数组给调用方法的父节点数组。然后再

<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span>(json_encode(</span><span class="vars">$arr</span><span>)); </span></span></li></ol>
Nach dem Login kopieren

转码成 JSON 将其输出,以便于AJAX异步访问,得到JSON数据。

得到之后,回到刚刚的JS功能代码中,直接初始化树目录结构,将其JSON数据传入OK。

总结:

其主要思想分2步走。第一步,是如何能把目录生成出来。先测试时,可以用静态数据。类似于

<ol class="dp-c"><li class="alt"><span><span class="keyword">var</span><span> node=[ </span></span></li><li><span>    {name:<span class="string">'父节点'</span><span>,children:[{name:</span><span class="string">'子节点'</span><span>,children:null},{name:</span><span class="string">'同级子节点'</span><span>,children:null}]} </span></span></li><li class="alt"><span>] </span></li><li><span>  </span></li><li class="alt"><span>先分析一下,这串数据,他有什么规律。你就会发现。其实很有规律。无限节点,其实就是每个json中,有children,而且 </span></li><li><span>还有同级子节点。 </span></li></ol>
Nach dem Login kopieren

你先用固定数据 生成目录结构之后

你就可以开始考虑,动态的向node传目录结构的数据了。就是我们后面所谓的 AJAX请求 PHP得到JSON数据,

PHP处理中,我用的是递归算法,返回JSON数据。及完成了。目录结构。

哦对了。

<ol class="dp-c"><li class="alt"><span><span class="vars">$m</span><span>=M(</span><span class="string">'ztree'</span><span>); </span></span></li></ol>
Nach dem Login kopieren

这句代码是thinkphp 实例化 数据操作对象的。

用来查询数据库中,节点是否存在。就是存在子节点,就返回给子节点数组,有几个就加入子节点数组中,查询完了。然后一次性返回,他们就成了同级子节点了。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/915111.htmlTechArticleZtree + PHP 无限极节点 递归查找节点法 一、前言 简单的描述一下,实习几个原理,思想,其实写很多东西,思想算是最重要的。 1、目标:...
Verwandte Etiketten:
php
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage