PHP 使用編碼樹,產生easyui中的tree樣式

來源:互聯網
上載者:User
產生樹的時候,資料庫中一般設計的都為無級數,即為:父子節點的樹,例如:基本的資料表設計為:

nodecode 節點編碼

parentnodecode 父節點編碼

nodename 節點名稱

這樣的形式,在產生樹的時候,根據子找父,再找爺爺的關係,一直找到最頂級,來確定一個從屬關係,如果由父級找子級再找孫子級,那麼,就可以有無限個孩子級,所以這種情況,適合不限分類的一種寫法,但是這種寫法帶來的結果就是,效率比較低,處理的時間長,所以在開發過程中,就出現了一種編碼樹的形式:基本的資料表設計為:

nodecode 節點編碼

nodename 節點名稱

這種編碼樹,子節點,帶著父節點的資訊,比如說節點編碼為:001 名稱為:1級 節點編碼為:001001 名稱為:1_1級,從此可以看出,我只要看到001001就能知道他的父級是誰,也能知道他的子集是誰,在資料庫中只要使用 likt '001001%',即可得到,以nodecoder的長度為varchar(30)為例,則最多產生10個子集,即001001001001001001001001001001這樣的關係,如果以3位長度為一個節點的長度,則可產生999個同級分類,如果以4個節點長度,則可產生9999個節點,所以隨著每個節點的長度的增長,可以容納更多的節點和子集,此適合不是無限級分類的使用。

使用無級樹,在網上有許多產生easyui中tree使用Json的方法,下面這樣方法是根據編碼生產生json的方式,僅為簡單的一個實現:

 1     public function getTree($root,$text="typename",$value="typecode", $fileLength = 3) { 2         $childNode = array(); 3 foreach ($root as $key=>$node) { 4 unset($childNode); 5 foreach ($root as $k => $v) { 6 if ($this->startWith($v["typecode"], $node["typecode"]) && $v["typecode"] != $node["typecode"]) { 7 $childNode[] = $v; 8  } 9  } 10 $treeNode = array(); 11 if (strlen($node["typecode"]) == $fileLength) { 12 $treeNode['children'] = $this->getTree($childNode,$text,$value, $fileLength + 3); 13 $treeNode["id"] = $node[$value]; 14 $treeNode["text"] = $node[$text]; 15 $treeNode["value"] = $node[$value]; 16 $treeNode["checked"] = "false"; 17 // $node["attributes"] = $node; 18 $treeNode["state"] = "open"; 19 $tree[] = $treeNode; 20  } 21  } 22 return $tree; 23  } 24 private function startWith($str, $needle) { 25 return strpos($str, $needle) === 0; 26 }
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.