Tree menu of accounting subjects in the ComboTree of ThinkPHP + EasyUI, thinkphpcombotree
Assume that the fields of the account data table in the database are id, code, name, and islast. They are auto-incrementing primary keys, subject encoding, and subject names respectively. Are they the last-level ("1" indicates the last-level subjects ). Here we use recursion in the Thinkphp model layer to construct the data required for ComboTree. The code and annotations are as follows:
Namespace Home \ Model; use Think \ Model; class AccountModel extends Model {
Public function getTreeList () {$ data = $ this-> field ('Id, code, name, islast')-> select (); foreach ($ data as & $ v) {// The element pid is added here to identify the $ len = strlen ($ v ['code']) of the parent id; if ($ len = 3) {// assume that the subject code with a length of 3 characters is Level 1, 5 characters are level 2, 7 characters are level 3, and so on $ v ['pid '] = 0 ;} else {$ v ['pid '] = substr ($ v ['code'], 0, $ len-2) ;}return $ this-> createTree ($ data ); // generate the data structure required by ComboTree} // generate the subtree private function createSubTree ($ data, $ pid) recursively {$ tree = Array (); foreach ($ data as $ k => $ v) {if ($ v ['pid '] = $ pid) {$ newDate = array ('id' => $ v ['id'], 'text' => $ v ['name'], 'State' => ($ v ['isla']! = 1 )? 'Closed ': 'Open'); if ($ v ['isla']! = 1) {// if the subData is not generated by calling the last-level account, $ subData = $ this-> createSubTree ($ data, $ v ['code']); if (! Empty ($ subData) {$ newDate ['children '] = $ subData ;}} array_push ($ tree, $ newDate) ;}} return $ tree ;} // generate the tree. First, create the tree menu data private function createTree ($ data) {$ ret = array () from the first accounting subject category (); foreach ($ data as $ k =>$ v) {if ($ v ['pid '] = 0) {$ newDate = array (// create a format that can be recognized by ComboTree. I have no better solutions here, so I can only handle 'id' => $ v ['id'], 'text' => $ 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 and regions) can be implemented after simple code modifications to build a tree menu.