Ztree + PHP Infinite pole node recursive lookup node method
First, preface
A simple description, the practice of a few principles, thinking, actually write a lot of things, thought is the most important.
1, Target: Will write an infinite node tree directory structure, such as
Steps:
1, your download plugin ztree. And then decorate it in your project.
2. Related CSS
- "stylesheet" href= "__public__/css/ztreestyle/ztreestyle.css" type= "Text/css"
- "stylesheet" href="__public__/css/ztree.css " type=" Text/css "
The above CSS and JS to your own prevail.
3. Directory Structure div
- "Content_wrap" style="background: #666;"
- "Ztreedemobackground Left"
-
"Treedemo" class= "Ztree" >
-
- "Content-text" id= "Text"
4, own the code in the JS alone
The relevant functions and configuration written inside!
- //Configuration Items
- var setting = {
- Issimpledata:true, //Data used in simple Array format, default false
- Showline:true , //whether the connection between nodes is displayed
- Checkable:true,
- Callback: {
- Onclick:ztreeonclick
- }
- };
- var Znodes; //Data variables
-
- //ajax submit data, request background PHP processing to return the directory structure JSON data
- $.ajax ({
- URL:"/admin.php/ztree",
- Type: "get",
- Async:false,
- DataType:"JSON",
- Success: function (data) {
- //alert (data);
- Znodes=data; //Save the data returned by the request.
- //alert (znodes);
- },
- Error: function () {//request failed handler
- Alert (' request failed ');
- },
- })
-
- //Initialize the ZTREE directory structure view!
- $ (document). Ready (function() {
- //alert ("111");
- $.fn.ztree.init ($ ("#treeDemo"), setting, znodes);
- });
5, the background php recursive algorithm, from the database to find the directory structure and generate JSON data
Address: As in 4, Ajax requested/admin.php/ztree "I am here to use the thinkphp framework, so the URL is like this, to your own interface file as the subject!"
-
- //Parent node Array
- $arr = Array ();
- $arr _str0 = Array ("name" = "function library query ',' children '="$this ->selectson (1)); //parent node pid=1;
- $arr _str1 = Array ("name" =' database query',' children '= $this->selectson (2)); //parent node pid=2;
-
- Array_push ($arr, $arr _str0);
- Array_push ($arr, $arr _str1); //This is a 2 parent node.
-
- Echo (Json_encode ($arr)); //This is the return data JSON data that was returned to the page, which is returned to the AJAX request
- ? >
-
- //Here is just a method, a call to the Selectson () method that returns an array of collections! But it uses recursion!
-
- //Find child node pid= parent Node ID
- Private function selectson ($Pid) {
-
- $m =m (' ztree ');
-
- if ($info=$m->where ("pid= ' $Pid '") Select ())) //Find the child ID under the parent ID
- {
- $data = Array ();
- for ($i=0; $i < Count ($info); $i ++)
- {
- $da = Array ("name" =$info[$i] [' name '] ,' children '=$this->selectson ($info[ $i[' id '])); //recursive algorithm!
-
- Array_push ($data, $da); //Join a child node array
- };
-
- return $data; //Returns an array of child nodes at once, they become siblings.
- }
- Else
- {
- return null;
- }
-
- }
- ?>
Note: Because I am using the thinkphp framework. So in the method call some different, pure PHP file, the idea should be the same,
The first is: Write an array. An array of parent nodes.
Second: Write a method, pass the parameter is the ID of the parent node, query its child nodes, after querying in the child node, recursively continue to find child nodes of child nodes, until the final query is complete, return the array to the calling method of the parent node array. and then
- Echo (Json_encode ($arr
Transcoding it into JSON to output it for Ajax asynchronous access to get JSON data.
After that, go back to just the JS function code, directly initialize the tree directory structure, the JSON data passed to OK.
Summarize:
Its main ideas are divided into 2 steps. The first step is how the directory can be generated. When testing first, you can use static data. Similar to
- Var
- {Name:' parent node ', children:[{name:' child node', Children:null},{name:' sibling child node '
-
After you build the directory structure with fixed data
You can start thinking about the dynamic transfer of data from the directory structure to node. is what we call Ajax request PHP to get JSON data,
In PHP processing, I used a recursive algorithm to return JSON data. and completed. Directory structure.
Oh, that's right.
- $m =m (' Ztree '
This code is a thinkphp instantiated data manipulation object.
Used to query whether a node exists in the database. is the existence of child nodes, the return to the sub-node array, a few are added to the sub-node array, the query is done. Then they return at once, and they are the same child nodes.
http://www.bkjia.com/PHPjc/915111.html www.bkjia.com true http://www.bkjia.com/PHPjc/915111.html techarticle Ztree + PHP infinite pole node recursive lookup node method one, preface simple description, practice several principles, thought, actually write a lot of things, thought is the most important. 1. Target: ...