Home  >  Article  >  Backend Development  >  Implementation method of accounting tree menu in ComboTree of ThinkPHP+EasyUI

Implementation method of accounting tree menu in ComboTree of ThinkPHP+EasyUI

怪我咯
怪我咯Original
2017-06-16 09:55:521087browse

The following editor will bring you an article on how to implement the accounting tree menu in ComboTree of ThinkPHP+EasyUI. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look.

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;
 }  
}

Data similar to the above (such as departments, regions, etc.) can basically achieve similar functions by simply modifying the code to build a tree menu.

The above is the detailed content of Implementation method of accounting tree menu in ComboTree of ThinkPHP+EasyUI. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn