{"id":100392,"date":"2018-05-24T16:28:31","date_gmt":"2018-05-24T08:28:31","guid":{"rendered":"http:\/\/www.ilezhi.cn\/?p=100392"},"modified":"2018-05-31T00:18:02","modified_gmt":"2018-05-30T16:18:02","slug":"php-%e6%a0%91%e5%bd%a2%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/www.ilezhi.cn\/?p=100392","title":{"rendered":"php \u6811\u5f62\u5217\u8868"},"content":{"rendered":"<p>TreeView.php<\/p>\n<pre class=\"lang:php decode:true \" title=\"TreeView.php\">&lt;?php\r\nclass TreeView {\r\n    public $childName = \"child\";\r\n    public $parentid;\r\n    public $id;\r\n    public $nameColumn;\r\n    function __construct($parentid, $id,$nameColumn) {\r\n        $this-&gt;parentid = $parentid;\r\n        $this-&gt;id = $id;\r\n        $this-&gt;nameColumn=$nameColumn;\r\n    }\r\n    \r\n    public function display($Nodes) {\r\n        echo '&lt;div class=\"TreeView\"&gt;&lt;ul&gt;';\r\n        $GroupNodes = $this-&gt;buildTree($Nodes-&gt;list, $this-&gt;parentid, $this-&gt;id);\r\n        $this-&gt;showTree($GroupNodes);\r\n        echo '&lt;\/ul&gt;';\r\n    }\r\n\r\n    function buildTree($temp, $parentid, $idKey) {\r\n        $grouped = $this-&gt;reGroup($temp, $parentid, $idKey);\r\n        $fnBuilder = function ($siblings) use(&amp;$fnBuilder, $grouped, $idKey) {\r\n            foreach ($siblings as $k =&gt; $sibling) {\r\n                $id = $sibling[$idKey];\r\n                if (isset($grouped[$id])) {\r\n                    $sibling[$this-&gt;childName] = $fnBuilder($grouped[$id]);\r\n                }\r\n                $siblings[$k] = $sibling;\r\n            }\r\n            return $siblings;\r\n        };\r\n        $tree = $fnBuilder($grouped[0]);\r\n        return $tree;\r\n    }\r\n\r\n\r\n    function showTree($grouped) {\r\n       foreach($grouped as $subGroupKey=&gt;$subGroup){\r\n           if(is_array($subGroup)){              \r\n               if($subGroupKey===$this-&gt;childName){\r\n                   \/\/\u5f53\u524d\u6709\u5b50\u96c6\r\n                   echo '&lt;ul&gt;';\r\n                   $this-&gt;showTree($subGroup);\r\n                   echo '&lt;\/ul&gt;&lt;\/li&gt;';\r\n               }else{\r\n                   echo '&lt;\/li&gt;';\r\n                   $this-&gt;showTree($subGroup);\r\n               }\r\n           }else{\r\n               if($subGroupKey===$this-&gt;nameColumn){\r\n                   echo '&lt;li&gt;&lt;div class=\"clickOnCollpose\"&gt;&lt;div class=\"title\"&gt;&lt;a&gt;'.$subGroup.'&lt;\/a&gt;&lt;\/div&gt;&lt;\/div&gt;';\r\n               }\r\n           }\r\n       }\r\n    }\r\n   \r\n    function reGroup($temp, $parentid, $id) {\r\n        $group = array();\r\n        foreach ($temp as $item) {\r\n            $key = $item[$parentid];\r\n            $group[$key][] = $item;\r\n        }\r\n        return $group;\r\n    }\r\n    \r\n}\r\nclass Nodes{\r\n    public $list;\r\n\r\n    function __construct(){\r\n        $this-&gt;list=array();\r\n    }\r\n\r\n    public function add($Node) {\r\n        array_push($this-&gt;list, $Node);\r\n    }\r\n    public function removeByNode($Node) {\r\n        for ($i = 0;$i &lt; count($this-&gt;list);$i++) {\r\n            if ($this-&gt;list[$i] === $Node) {\r\n                $this-&gt;remove($i);\r\n            }\r\n        }\r\n    }\r\n    public function removeByIndex($startIndex, $length = 0) {\r\n        array_splice($this-&gt;list, $startIndex, $lenght);\r\n    }\r\n    public function clear() {\r\n        unset($this-&gt;list);        \r\n    }    \r\n}\r\n\r\n?&gt;<\/pre>\n<p>TreeView.css<\/p>\n<pre class=\"lang:css decode:true \" title=\"TreeView.css\">ul{\r\n    list-style:none;\r\n    padding-left: 1em;\r\n}\r\n.clickOnCollpose{\r\n    display: inline-block;\r\n    background-color: #eee;\r\n    width:100%;\r\n    margin:4px 0px 4px 0px;\r\n    border-radius: 6px;\r\n    border-width: 1px;\r\n    border-color: #aaa;\r\n    border-style: solid;\r\n    padding:4px;\r\n}\r\n\r\n.clickOnCollpose:hover{\r\n    background-color: #337ABC;\r\n    color:#FFFFFF;\r\n    padding:4px;\r\n    border-color:#FFF;\r\n}\r\n\r\n.title{\r\n    float:left;\r\n    text-indent: 10px;\r\n}\r\n.count-near{\r\n    background-color:#FF0000;\r\n    border-radius:20px;\r\n    color:#FFF;\r\n    font-weight:bold;\r\n    width:20px;\r\n    height:20px;\r\n    text-align:center;\r\n    float:left;\r\n    margin-left:20px;\r\n}\r\n.count-far{\r\n    background-color:#FF0000;\r\n    border-radius:20px;\r\n    color:#FFF;\r\n    font-weight:bold;\r\n    width:20px;\r\n    height:20px;\r\n    text-align:center;\r\n    float:right;\r\n}\r\n\r\n\r\nli{\r\n    cursor:hand;\r\n}\r\n.treeview{\r\n   width:100%;\r\n}<\/pre>\n<p>TreeView.js<\/p>\n<pre class=\"lang:js decode:true \" title=\"TreeVIew.js\">$(document).ready(function(){\r\n    \/\/\u9ed8\u8ba4\u4ece\u7b2c2\u7ea7\u9690\u85cf\r\n    $(\".treeView ul li\").find(\"ul\").toggle();\r\n \r\n    $(\".clickOnCollpose\").on('click',function(){    \r\n        \/\/\u5c55\u5f00\/\u6536\u7f29\u4e0b\u7ea7\r\n        $(this).parent().find(\"ul\").toggle();  \r\n \r\n        \/\/\u5c55\u5f00\/\u6536\u7f29\u4e0b\u4e0b\u7ea7\r\n        $(this).parent().find(\"ul li ul\").toggle();        \r\n    })\r\n \r\n    $(\".clickOnCollpose\").mouseenter(function(){\r\n        var count=$(this).next().children().length;\r\n        \/\/if(count&gt;0){\r\n            \/\/class\u4fee\u6539\u6210 count-near \u66f4\u6539\u6837\u5f0f\r\n            $(this).append(\"&lt;div class='count-far'&gt; \"+count+\" &lt;\/div&gt;\");       \r\n        \/\/}\r\n    })\r\n \r\n    $(\".clickOnCollpose\").mouseleave(function(){     \r\n        \/\/class\u4fee\u6539\u6210 count-near \u66f4\u6539\u6837\u5f0f\r\n         $(this).find(\".count-far\").remove();\r\n    })\r\n \r\n})<\/pre>\n<p>==============================================================<\/p>\n<p>Demo.php<\/p>\n<pre class=\"lang:php decode:true \">&lt;?php\r\nrequire_once(\"TreeView.php\");\r\n?&gt;\r\n\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;\u6d4b\u8bd5&lt;\/title&gt;\r\n&lt;link rel=\"stylesheet\" href=\".\/TreeView.css\" type=\"text\/css\"\/&gt;\r\n&lt;script src=\"http:\/\/libs.baidu.com\/jquery\/2.1.4\/jquery.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script src=\".\/TreeView.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;?php\r\n\r\n$n=new Nodes();\r\n$n-&gt;add( [\"id\" =&gt; 1, \"name\" =&gt; \"name0-1\", \"parentid\" =&gt; 0]);\r\n$n-&gt;add( [\"id\" =&gt; 2, \"name\" =&gt; \"name1-2\", \"parentid\" =&gt; 1]);\r\n$n-&gt;add( [\"id\" =&gt; 3, \"name\" =&gt; \"name1-3\", \"parentid\" =&gt; 1]);\r\n$n-&gt;add( [\"id\" =&gt; 4, \"name\" =&gt; \"name2-4\", \"parentid\" =&gt; 2]);\r\n$n-&gt;add( [\"id\" =&gt; 5, \"name\" =&gt; \"name2-5\", \"parentid\" =&gt; 2]);\r\n$n-&gt;add( [\"id\" =&gt; 6, \"name\" =&gt; \"name4-6\", \"parentid\" =&gt; 4]);\r\n$n-&gt;add( [\"id\" =&gt; 7, \"name\" =&gt; \"name1-7\", \"parentid\" =&gt; 1]);\r\n$n-&gt;add( [\"id\" =&gt; 8, \"name\" =&gt; \"name2-8\", \"parentid\" =&gt; 2]);\r\n$n-&gt;add( [\"id\" =&gt; 9, \"name\" =&gt; \"name2-9\", \"parentid\" =&gt; 2]);\r\n$n-&gt;add( [\"id\" =&gt; 10, \"name\" =&gt; \"name2-10\", \"parentid\" =&gt; 2]);\r\n$n-&gt;add( [\"id\" =&gt; 11, \"name\" =&gt; \"name8-11\", \"parentid\" =&gt; 8]);\r\n$n-&gt;add( [\"id\" =&gt; 12, \"name\" =&gt; \"name8-12\", \"parentid\" =&gt; 8]);\r\n$n-&gt;add( [\"id\" =&gt; 13, \"name\" =&gt; \"name11-13\", \"parentid\" =&gt; 11]);\r\n$n-&gt;add( [\"id\" =&gt; 14, \"name\" =&gt; \"name0-14\", \"parentid\" =&gt; 0]);\r\n$n-&gt;add( [\"id\" =&gt; 15, \"name\" =&gt; \"name0-15\", \"parentid\" =&gt; 0]);\r\n\r\n\r\n$treeview=new TreeView(\"parentid\",\"id\",\"name\");\r\n\/\/\u53ef\u5199\u4f5c$treeview=new TreeView(2,0,1);\r\n$treeview-&gt;display($n);\r\n?&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TreeView.php &lt;?php class TreeView { public $childNam &hellip; <a href=\"http:\/\/www.ilezhi.cn\/?p=100392\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">php \u6811\u5f62\u5217\u8868<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,4],"tags":[122,121,119,120],"class_list":["post-100392","post","type-post","status-publish","format-standard","hentry","category-share","category-original","tag-css","tag-jquery","tag-php","tag-tree"],"_links":{"self":[{"href":"http:\/\/www.ilezhi.cn\/index.php?rest_route=\/wp\/v2\/posts\/100392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ilezhi.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ilezhi.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ilezhi.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ilezhi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=100392"}],"version-history":[{"count":0,"href":"http:\/\/www.ilezhi.cn\/index.php?rest_route=\/wp\/v2\/posts\/100392\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.ilezhi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=100392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ilezhi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=100392"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ilezhi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=100392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}