企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
![](https://img.kancloud.cn/50/8a/508adf83940f1df32f646910c4fffd30_562x680.png) ## 获取构建器实例 1. `ViewBuilder`中获取 ```php $viewBuilder = $this->viewBuilder; $tree = $viewBuilder->tree; ``` 2. `instance()`自身方法 ```php $tree = TreeBuilder::instance(); ``` 3. `new`关键词 ```php $tree= new TreeBuilder([ 'id' => '__admin_tree', ]); ``` ## 完整示例 ~~~ <?php /** * @link https://ym2.cleverstone.top * @copyright Copyright (c) 2020 Yii Manager Software LLC */ namespace backend\controllers; use builder\base\BaseController; use builder\helper\H5; use builder\tree\TreeBuilder; use common\models\Admin; /** * 树组件demo * @author cleverstone * @since ym2.0 */ class TreeController extends BaseController { public $guestActions = [ 'index', 'test', ]; /** * demo * @return string * @throws \Throwable * @throws \yii\base\InvalidConfigException */ public function actionIndex() { $treeBuilder = TreeBuilder::instance(); if ($this->isAjax) { return $treeBuilder->setData(function () { $all = Admin::query()->limit(10)->all(); $result = []; foreach ($all as $index => $item) { array_push($result, [ 'title' => '醉里挑灯看剑', 'id' => $item['id'], 'field' => 'username', 'test' => $index, 'children' => [ [ 'title' => '梦回吹角连营', 'id' => 'i' . $index, 'field' => 'ii'. $index, 'test' => $index, 'children' => [ [ 'title' => '八百里分麾下炙', 'id' => 'c' . $index, 'field' => 'cc' . $index, 'test' => $index, ], [ 'title' => '五十弦翻塞外声', 'id' => 'd' . $index, 'field' => 'dd' . $index, 'test' => $index, ] ], ] ], ]); } return $result; })->render(); } else { return $treeBuilder->setTitle('树组件构建') ->registerPointcut(H5::alert('头部切点')) ->setDefaultText('') ->setOnlyIconControl(false) ->setSwitchCheckedBtn() ->setSwitchSpreadBtn() ->setShowLine(false) ->setOperationItem([ $this->operationItem->modal()->btnClass('layui-btn-primary layui-border-red')->title('模态框')->params(['id', 'test' => 2, 'a' => ':field'])->closeBtn()->route('tree/test'), $this->operationItem->page()->btnClass('layui-btn-primary layui-border-orange')->title('单页')->params(['id'])->route('tree/test'), //功能:组件交互 $this->operationItem->ajax()->positionBottom()->title('确认提交')->params(['id'])->route('tree/test'), ]) ->setRootLayout([ 'class' => 'col-md-3' ]) ->setTreeLayout([ 'style' => ['height' => '500px', 'overflow' => 'auto'], ])->render(); } } public function actionTest() { return $this->asOk('success'); } } ~~~