Tree menu of accounting subjects in the ComboTree of ThinkPHP + EasyUI, thinkphpcombotree

Source: Internet
Author: User

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.

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.