The following small series for everyone to bring a thinkphp+easyui in the combotree of the accounts of the tree menu implementation method. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
Assume that the field of the ledger account data table in the database is: Id,code,name,islast. is the self-increment primary key, the account code, the account name, whether the last level ("1" represents the last level account).
Here, the data needed to build combotree in the model layer of thinkphp, with the following code and annotations:
Namespace Home\model;use think\model;class Accountmodel extends model{public Function gettreelist () {$data = $this-&G T;field (' Id,code,name,islast ')->select (); foreach ($data as & $v) {//The element PID is added here to identify the $len of the parent id = strlen ($v [' Code ']); if ($len = = 3) {//This assumes that the 3-bit length of the account code is one level, 5 bits is a two-level account, 7 bits is three, and so on $v [' pid '] = 0; } else {$v [' pid '] = substr ($v [' Code '],0, $len-2); }} return $this->createtree ($data); Generate Combotree required data structure}//recursive way to generate subtree Private function Createsubtree ($data, $pid) {$tree = array (); foreach ($data as $k + $v) {if ($v [' pid '] = = $pid) {$newDate = array (' id ' = ' = ' $v [' id '], ' text ' = = $v [' Nam E '], ' state ' = ($v [' islast ']! = 1)? ' Closed ': ' Open '); if ($v [' islast ']! = 1) {//If not the last account called itself generates sub-data $subData = $this->createsubtree ($data, $v [' Code ']); if (!empty ($subData)) {$newDate [' children '] = $subData; }} array_push ($tree, $newDate); }} return $tree; }//Spanning tree, start with a first-level ledger account classification to create a tree-shaped dishSingle Data Private function Createtree ($data) {$ret = array (); foreach ($data as $k + $v) {if ($v [' pid '] = = 0) {$newDate = array (//created as Combotree can recognize the format, here I have no better way, can only deal with ' I d ' = = $v [' id '], ' text ' and ' = ' $v [' name '], ' state ' = ' closed '); $subData = $this->createsubtree ($data, $v [' Code ']); if (!empty ($subData)) {$newDate [' children '] = $subData; } $ret [] = $newDate; }} return $ret; } }
Similar data, such as departments, regions, and so on, require a simple modification of the tree menu to achieve similar functionality.