sxtuwy 2011-08-29 09:25:00 5168次浏览 1条回复 0 0 0

之前在官方的extensions里发布过,现在提供第二个版本,主要修改了移动时左右节点出错的bug。同时增加了TreeSelectWidget。

具体使用如下: model里

public function behaviors()
{
    return array(
        'TreeBehavior' => array(
             'class' => 'ext.tree.behaviors.TreeBehavior',
             'id'=>'id',
             'parent_id'=>'parent_id',
             'left'=>'lft',
             'right'=>'rht',
             'level'=>'level',
             'name'=>'name'
        ),
    );
}

controller里

public function actions()
{
        return array(
            'treeMoveUp'=>'ext.tree.actions.TreeMoveUpAction',
            'treeMoveDown'=>'ext.tree.actions.TreeMoveDownAction',
            'treeCreate'=>'ext.tree.actions.TreeCreateAction',
            'treeUpdate'=>'ext.tree.actions.TreeUpdateAction',
            'treeLoad'=>'ext.tree.actions.TreeLoadAction',
            'treeDelete'=>'ext.tree.actions.TreeDeleteAction',
            'treeChildren'=>'ext.tree.actions.TreeChildrenAction',
            'treeChildrenParent'=>'ext.tree.actions.TreeChildrenParentAction',
        );
}
public function actionAdmin()
{
    $this->render('admin');
}

默认的树表结构

CREATE TABLE IF NOT EXISTS `tree` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `lft` int(11) NOT NULL,
  `rgt` int(11) NOT NULL,
  `level` int(11) NOT NULL,
  `parent_id` int(11) NOT NULL,
  `name` varchar(256) NOT NULL,
  PRIMARY KEY (`id`) 

TreeSelectWidget的用法 假如你现在你有一信息表(info),同时有一区域表(area),区域表是无限级分类的,区域Model的behaviors请参考以上写法。信息表又是属于(BELONGSTO)区域表,如某条信息是属于浙江省-温州市。那么信息表里可能存在两字段(province_id,city_id)都指向区域Model,表单如下:

//注:$model代表的是信息
echo $form->labelEx($model,'name');//信息名称
$this->widget('myext.tree.widgets.TreeSelectWidget',array(
    'model'=>$model,
    'modelName'=>'Area',
    'route'=>'info/areaSelect',
    'attributes'=>array('province_id','city_id')
));//信息所属区域

然后在您的InfoController的actions方法里加

public function actions()
{
    return array(
        'areaSelect'=>'myext.tree.actions.TreeSelectAction',
    );
}

好了,马上尝试一下吧! 下载此示例[attach]323[/attach]

您需要登录后才可以回复。登录 | 立即注册