ThinkPHP + EasyUI: How to Implement the tree menu of accounting subjects in ComboTree, 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, isla ') -> select (); foreach ($ data as & $ v) {// element pid is added here, $ len = strlen ($ v ['code']) used to identify 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 ); // required to generate ComboTree Data structure} // recursively generates the subtree private function createSubTree ($ data, $ pid) {$ 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.
The implementation of the tree menu of accounting subjects in the above ThinkPHP + EasyUI ComboTree is all the content that I have shared with you. I hope to give you a reference and support for the help house.