YiiAJAXCTreeView enables dynamic loading of infinite level trees

Source: Internet
Author: User
YiiAJAXCTreeView enables dynamic loading of infinite level trees -- TABLE structure 'coverage' -- create table if not exists 'coverage' ('id' int (10) unsigned not null AUTO_INCREMENT, 'pid 'int (10) unsigned default null, 'coveragename' varchar (100) default null, 'coveragedesc' varchar (200) default null, primary key ('id '), KEY 'pid '('pid') ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 20; -- data in the table 'coverage' -- insert into 'coverage' ('id', 'pid ', 'coveragename', 'coveragedesc') VALUES (16, NULL, 'category', ''), (17, 16, 'Category 1',''), (18, NULL, 'Category', ''), (19, 17, 'category 2', ''); -- restrict TABLE 'coverage' -- alter table 'coverage' add constraint 'coverage _ ibfk_1 'foreign KEY ('pid ') REFERENCES 'coverage' ('id') ONDELETECASCADE;


Then add the following code to the Controller:

?
1234567891011121314151617181920212223 PublicfunctionactionAjaxFillTree () {if (! Yii: app ()-> request-> isAjaxRequest) {exit () ;}$ parentId = 'null'; if (isset ($ _ GET ['root']) and $ _ GET ['root']! = 'Source') {$ parentId = (int) $ _ GET ['root'];} $ req = Yii: app () -> db-> createCommand ("SELECT m1.id, m1.coverageName AS text, m2.id is not null as hasChildren ". "FROM coverage AS m1 left join coverage AS m2 ON m1.id = m2.pid ". "WHERE m1.pid <=> $ parentId ". "group by m1.id order by m1.coverageName ASC"); $ children = $ req-> queryAll (); echostr_replace ('"hasChildren": "0"', '"hasChildren ": false', CTreeView: saveDataAsJson ($ children),); exit ();}


Then add the following code to views:

?
123456789 $ This-> widget ('ctreview', array ('persist' => 'cooker', 'animated' => 'Save ', 'URL' => array ('ajaxfilltree'), 'htmloption' => array ('id' => 'coveragetree ', 'class' => 'coveragetree ')));

Done!

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.