ThinkPHP + EasyUI: How to Implement the tree menu of accounting subjects in ComboTree, thinkphpcombotree

Source: Internet
Author: User

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.

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.