# select 樹選擇
這個可參考系統管理模塊中的節點編輯實例

這個可以在分類模型中直接參考節點管理模塊中的方法
~~~
在模型 mdoel/ Category.php中添加 如下方法,表中需要有id,pid,name字段
/**
* 獲取樹形角色
* @param null $id 需要隱藏的角色id
* @param string $default 默認第一個菜單項,默認為“頂級角色”,如果為false則不顯示,也可傳入其他名稱
* @author gyhong <63453409@qq.com>
* @return mixed
*/
public static function getTree($id = null, $default = '')
{
$result[0] = '頂級分類';
$where['status'] = ['eq', 1];
// $where['status'] = ['egt', 0];
// 排除指定菜單及其子菜單
if ($id !== null) {
$hide_ids = array_merge([$id], self::getChildsId($id));
$where['id'] = ['notin', $hide_ids];
}
// 獲取菜單
$roles = Tree::config(['title' => 'name'])->toList(self::where($where)->column('id,pid,name'));
foreach ($roles as $role) {
$result[$role['id']] = $role['title_display'];
}
// 設置默認菜單項標題
if ($default != '') {
$result[0] = $default;
}
// 隱藏默認菜單項
if ($default === false) {
unset($result[0]);
}
return $result;
}
~~~
然后在table中或者form中使用方法如下
~~~
return ZBuilder::make('form')
->setPageTitle('導入指定型號配件信息')
->addFormItem('select', 'model', '選擇導出型號', '',\app\pros\model\Category::getTree(),0)
->setBtnTitle('submit', '導出')
->hideBtn('back')
->isAjax(false)
->fetch();
table中類似,略
~~~