Home >Backend Development >PHP Tutorial >ThinkPHP uses EasyUI to implement accounting tree menu
This article mainly explains the implementation method of ThinkPHP and EasyUI's ComboTree to generate accounting account tree menu. Share it with everyone for reference. I hope to be helpful.
Assume that the fields of the accounting data table in the database are: id, code, name, islast. They are the auto-increment primary key, account code, account name, and whether it is the last-level account ("1" indicates the last-level account).
Here, recursion is used in the model layer of Thinkphp to build the data required by 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){ //这里加入了元素pid,用来标识父级id的 $len = strlen($v['code']); if($len == 3){ //这里假设长度为3位的科目编码是一级,5位为二级科目,7位为三级,以此类推 $v['pid'] = 0; } else { $v['pid'] = substr($v['code'],0,$len-2); } } return $this->createTree($data); //生成ComboTree所需的数据结构 } //递归的方式生成子树 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['islast'] != 1)?'closed':'open' ); if($v['islast'] != 1){ //如果不是末级科目调用自身生成子数据 $subData = $this->createSubTree($data,$v['code']); if(!empty($subData)){ $newDate['children'] = $subData; } } array_push($tree,$newDate); } } return $tree; } //生成树,先从一级会计科目分类开始创建树形菜单数据 private function createTree($data){ $ret = array(); foreach($data as $k => $v){ if($v['pid'] == 0){ $newDate = array( //创建为ComboTree能够识别的格式,这里我没有更好的办法了,只能这样处理 '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 to the above Data (such as departments, regions, etc.) that need to build a tree menu can basically achieve similar functions by simply modifying the code.
Related recommendations:
Detailed examples of thinkphp5 URL and routing functions
How to connect to a distributed database in Thinkphp
##Replace the entry file in thinkphp3.2
The above is the detailed content of ThinkPHP uses EasyUI to implement accounting tree menu. For more information, please follow other related articles on the PHP Chinese website!