netdak 2011-06-27 16:30:08 4333次浏览 5条回复 2 0 0

首先建立表coverage

-- 表的结构 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 ;

--

-- 转存表中的数据 coverage

INSERT INTO `coverage` (`id`, `pid`, `coverageName`, `coverageDesc`) VALUES
(16, NULL, '类别', ''),
(17, 16, '类别一', ''),
(18, NULL, '分类', ''),
(19, 17, '类别二', '');

--

-- 限制导出的表

--

-- 限制表 coverage

ALTER TABLE `coverage`
ADD CONSTRAINT `coverage_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `coverage` (`id`) ON DELETE CASCADE;

然后才Controller中添加下面的代码:

public function actionAjaxFillTree()
{
    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();
    echo str_replace(
        '"hasChildren":"0"',
        '"hasChildren":false',
        CTreeView::saveDataAsJson($children)
    );
    exit();
}

然后在views中添加如下代码:

<?php 
$this->widget('CTreeView',array(
    'persist'=>'cookie',
    'animated'=>'fast',
    'url' => array('ajaxFillTree'),
    'htmlOptions'=>array(
        'id'=>'coverageTree',
        'class'=>'coverageTree'
        )
));
?>
您需要登录后才可以回复。登录 | 立即注册