The implementation method of the chart of accounts in the combotree of Thinkphp+easyui

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.