Table of Contents
Ztree + PHP 无限极节点 递归查找节点法
Home php教程 php手册 Ztree + PHP 无限极节点 递归查找节点法

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

Jun 13, 2016 am 09:20 AM
php

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>
Copy after login

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>
Copy after login

以上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>
Copy after login

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>
Copy after login

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

<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>
Copy after login

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>
Copy after login

注意:由于我是用的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>
Copy after login

转码成 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>
Copy after login

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

你就可以开始考虑,动态的向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>
Copy after login

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

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles