composer require wodrow/yii2-wwtree dev-master
public function behaviors()
{
return [
'tree' => [
'class' => \wodrow\yii2wwtree\TreeBehavior::class,
'primaryKeyAttribute' => 'your_pk_id_filed_name',
'parentKeyAttribute' => 'your_parent_id_filed_name',
'nameKeyAttribute' => 'your_name_filed_name',
'sortKeyAttribute' => 'your_sort_filed_name',
'iconKeyAttribute' => 'your_icon_filed_name',
'iconColorKeyAttribute' => 'your_icon_color_filed_name',
],
];
}
<?=wodrow\yii2wwtree\TreeWidget::widget([
'treeModelClass' => "your model class",
// 'custom_field_view' => "@your/extend/view",
// 'ajaxUrl' => "",
]) ?>
数据表必须要有id, pid(父级), name, sort, icon, icon_color这六个字段,字段名可以是TreeBehavior配置的字段名;
其他字段根据需要自己添加, 在custom_field_view视图里可以处理添加的字段,使用它生成的模型。
如果使用ajaxUrl,推荐在控制器添加actions
public function actions()
{
return [
...,
'to-your-ajax-url' => [
'class' => \wodrow\yii2wwtree\SearchAction::class,
'searchClass' => TestTree::class,
'idKeyAttr' => 'your id field key',
'textKeyAttr' => 'your text field key',
],
];
}