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!