ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
![](https://img.kancloud.cn/bc/79/bc79a0bc9fd1c9b752200beca353be3f_1092x615.png) ## 获取构建器实例 1. `ViewBuilder`中获取 ```php $viewBuilder = $this->viewBuilder; $tab = $viewBuilder->tab; ``` 2. `instance()`自身方法 ```php $tabBuilder = TabBuilder::instance(); ``` 3. `new`关键词 ```php $formBuilder= new TabBuilder([ 'id' => '__admin_tab', ]); ``` ##完整示例 ~~~ <?php /** * @link https://ym2.cleverstone.top * @copyright Copyright (c) 2020 Yii Manager Software LLC */ namespace backend\controllers; use builder\helper\D; use builder\table\TableBuilder; use common\models\Admin; use Yii; use builder\base\BaseController; use yii\base\UserException; use yii\helpers\Json; /** * 首页 * @author cleverstone * @since ym2.0 */ class IndexController extends BaseController { public $actionVerbs = [ 'index' => ['GET', 'POST'], 'link' => ['GET'], 'quit' => ['GET', 'POST'], ]; public $guestActions = [ 'index', 'link', ]; /** * @return string * @throws \builder\base\InvalidInstanceException * @throws \builder\base\NotFoundParamsException * @throws \yii\base\InvalidConfigException */ public function actionIndex() { // 选项卡 - 表格1 $table1 = TableBuilder::instance([ 'id' => 'table1_1', ]); // 选项卡 - 表格2 $table2 = TableBuilder::instance([ 'id' => 'table2_1', ]); if ($this->isAjax) { // AJAX switch ($table1->builderId) { case $table1->id: // 表格1 return $table1->setQuery(function () { return Admin::find(); }) ->setOrderBy(['id' => SORT_DESC]) ->setColumns([ 'id', 'username', 'password_hash', 'email', 'mobile', 'status', 'group', 'created_at', 'updated_at', 'ro_delete3' => function ($row) { return $row['id'] % 2; }, ])->render(); case $table2->id: // 表格2 return $table2->setQuery(function () { return Admin::find(); }) ->setOrderBy(['id' => SORT_DESC]) ->setColumns([ 'id', 'username', 'password_hash', 'email', 'mobile', 'status', 'group', 'created_at', 'updated_at', 'ro_delete3' => function ($row) { return $row['id'] % 2; }, ])->render(); } } else { // HTML // 表格1 $table1->setTitle('管理员列表1') ->setAutoRequest(false) //->registerPointcut(H5::alert('测试一下', 'success')) //->registerPointcut(H5::alert('测试一下'), TableBuilder::POS_END) ->setTableHead([ $this->tableHead->checkbox(), $this->tableHead->field('id')->title('ID_1'), $this->tableHead->field('username')->title('账号_1')->editText('admin/edit'), $this->tableHead->field('password_hash')->title('密码_1'), $this->tableHead->field('email')->title('邮箱_1'), $this->tableHead->field('mobile')->title('手机号_1'), $this->tableHead->field('status')->title('状态_1'), $this->tableHead->field('group')->title('分组_1'), $this->tableHead->field('created_at')->title('添加时间_1'), $this->tableHead->field('updated_at')->title('更新时间_1'), $this->tableHead->toolbar()->title('操作项_1'), ]) ->setRowOperation([ // 详情 $this->rowOperation->modal()->title('详情_1')->icon('bi bi-plus-lg f15')->route('admin/detail')->closeBtn(), $this->rowOperation->ajax()->title('删除_1')->icon('bi bi-x-lg')->route('admin/delete')->method('POST')->group(), $this->rowOperation->ajax()->title('我要删除1_1')->icon('bi bi-x-lg')->route('admin/delete')->method('POST')->group(), $this->rowOperation->ajax()->title('我要删除2_1')->icon('bi bi-x-lg')->route('admin/delete')->method('POST')->group(), $this->rowOperation->ajax()->title('我要删除3_1')->id('ro_delete3')->icon('bi bi-x-lg')->route('admin/delete')->method('POST')->group(), ]) ->setToolbarCustom([ // 表单 $this->toolbarCustom->modal()->title('表单_1')->icon('bi bi-plus-lg f15')->route('admin/add')->params(['_bid' => $table1->id]), // 列表 $this->toolbarCustom->modal()->title('列表_1')->icon('bi bi-plus-lg f15')->route('admin/detail')->closeBtn(), // 选项卡 $this->toolbarCustom->modal()->title('选项卡_1')->icon('bi bi-pencil-square f13')->route('admin/edit')->closeBtn(), // AJAX $this->toolbarCustom->page()->title('AJAX_1')->icon('bi bi-lock')->targetBlank()->params(['id'])->route('admin/add'), ]) ->setToolbarFilter([ 'keyword' => $this->toolbarFilter->text()->label('关键词')->placeholder('请输入账号/姓名')->defaultValue()->attribute(['test' => 1]), //'username' => $this->toolbarFilter->datetime()->style(['width' => '800px'])->label('注册时间')->placeholder('请选择注册时间'), //'username' => $this->toolbarFilter->datetime()->label('注册时间_1')->placeholder('请选择注册时间'), //'status' => $this->toolbarFilter->select()->label('用户状态_1')->placeholder('请选择用户状态')->options(['normal' => '正常', 'deny' => '禁用',]), //'link1' => $this->toolbarFilter->selectLink()->label('联动1_1')->placeholder('请选择')->route('admin/link')->initOptions(['a' => 'A', 'b' => 'B'])->targetLink('link2')->start(), //'link2' => $this->toolbarFilter->selectLink()->label('联动2_1')->placeholder('请选择')->route('admin/link')->targetLink('link3')->through(), //'link3' => $this->toolbarFilter->selectLink()->label('联动3_1')->placeholder('请选择')->route('admin/link')->targetLink('link4')->through(), //'link4' => $this->toolbarFilter->selectLink()->label('联动2_1')->placeholder('请选择')->end(), ]); // 表格2 $table2->setTitle('管理员列表2') ->setAutoRequest(false) //->registerPointcut(H5::alert('测试一下', 'success')) //->registerPointcut(H5::alert('测试一下'), TableBuilder::POS_END) ->setTableHead([ $this->tableHead->checkbox(), $this->tableHead->field('id')->title('ID_2'), $this->tableHead->field('username')->title('账号_2')->editText('admin/edit'), $this->tableHead->field('password_hash')->title('密码_2'), $this->tableHead->field('email')->title('邮箱_2'), $this->tableHead->field('mobile')->title('手机号_2'), $this->tableHead->field('status')->title('状态_2'), $this->tableHead->field('group')->title('分组_2'), $this->tableHead->field('created_at')->title('添加时间_2'), $this->tableHead->field('updated_at')->title('更新时间_2'), $this->tableHead->toolbar()->title('操作项_2'), ]) ->setRowOperation([ // 详情 $this->rowOperation->modal()->title('详情_2')->icon('bi bi-plus-lg f15')->route('admin/detail')->closeBtn(), $this->rowOperation->ajax()->title('删除_2')->icon('bi bi-x-lg')->route('admin/delete')->method('POST')->group(), $this->rowOperation->ajax()->title('我要删除1_2')->icon('bi bi-x-lg')->route('admin/delete')->method('POST')->group(), $this->rowOperation->ajax()->title('我要删除2_2')->icon('bi bi-x-lg')->route('admin/delete')->method('POST')->group(), $this->rowOperation->ajax()->title('我要删除3_2')->id('ro_delete3')->icon('bi bi-x-lg')->route('admin/delete')->method('POST')->group(), ]) ->setToolbarCustom([ // 表单 $this->toolbarCustom->modal()->title('表单_2')->icon('bi bi-plus-lg f15')->route('admin/add')->params(['_bid' => $table2->id]), // 列表 $this->toolbarCustom->modal()->title('列表_2')->icon('bi bi-plus-lg f15')->route('admin/detail')->closeBtn(), // 选项卡 $this->toolbarCustom->modal()->title('选项卡_2')->icon('bi bi-pencil-square f13')->route('admin/edit')->closeBtn(), // AJAX $this->toolbarCustom->page()->title('AJAX_2')->icon('bi bi-lock')->targetBlank()->params(['id'])->route('admin/add'), ]) ->setToolbarFilter([ 'keyword' => $this->toolbarFilter->text()->label('关键词')->placeholder('请输入账号/姓名')->defaultValue()->attribute(['test' => 1]), //'username' => $this->toolbarFilter->datetime()->style(['width' => '800px'])->label('注册时间')->placeholder('请选择注册时间'), //'username' => $this->toolbarFilter->datetime()->label('注册时间_2')->placeholder('请选择注册时间'), //'status' => $this->toolbarFilter->select()->label('用户状态_2')->placeholder('请选择用户状态')->options(['normal' => '正常', 'deny' => '禁用',]), //'link1' => $this->toolbarFilter->selectLink()->label('联动1_2')->placeholder('请选择')->route('admin/link')->initOptions(['a' => 'A', 'b' => 'B'])->targetLink('link2')->start(), //'link2' => $this->toolbarFilter->selectLink()->label('联动2_2')->placeholder('请选择')->route('admin/link')->targetLink('link3')->through(), //'link3' => $this->toolbarFilter->selectLink()->label('联动3_2')->placeholder('请选择')->route('admin/link')->targetLink('link4')->through(), //'link4' => $this->toolbarFilter->selectLink()->label('联动2_2')->placeholder('请选择')->end(), ]); } // 视图构建器开始 $viewBuilder = $this->viewBuilder; // 选项卡 $tab = $viewBuilder->tab; $tab->setTheme('column') ->setTitle('选项卡') ->setTabItem([ '自定义' => $this->tabItem->custom('自定义的内容')->switchReloadProperty('')->selected(), '表格1' => $this->tabItem->builder($table1)->switchReload(), '表格2' => $this->tabItem->builder($table2)->switchReload(), ]); return $viewBuilder->render(); } /** * 控制台 * @return string * @throws \builder\base\InvalidInstanceException * @throws \builder\base\NotFoundParamsException */ /* public function actionIndex() { if ($this->isPost) { // return $this->as403('登录已失效!'); return $this->asOk('提交成功!'); } else { $viewBuilder = $this->viewBuilder; $form = $viewBuilder->form; $form->setTitle('控制台') ->setGroup('基本信息', 'base') ->setGroupComment('这是一个基本信息') ->setGroupCol(4) ->setFormControl([ 'a' => $this->formControl->text()->label('文本a')->defaultValue('我是文本"\'' . PHP_EOL)->placeholder('请填写文本a'), 'b' => $this->formControl->number()->label('数字b')->defaultValue(123.15)->placeholder('请填写数字b'), 'c' => $this->formControl->datetime()->placeholder('请选择')->defaultValue('2022-02-01 02:00:00')->label('日期')->range(false)->comment('测试一下')->required(false), 'd' => $this->formControl->date()->placeholder('请选择')->defaultValue('2022-02-01')->label('日期')->range(false)->comment('测试一下')->required(false), 'e' => $this->formControl->year()->placeholder('请选择')->defaultValue('2022')->label('日期')->range(false)->comment('测试一下')->required(false), 'f' => $this->formControl->month()->placeholder('请选择')->defaultValue('2022-02')->label('日期')->range(false)->comment('测试一下')->required(false), 'g' => $this->formControl->time()->placeholder('请选择')->defaultValue('02:00:00')->label('日期')->range(false)->comment('测试一下')->required(false), 'h' => $this->formControl->select()->label('选项')->defaultValue('2')->options(['1' => "正常", '2' => '不正常',]), 'i' => $this->formControl->select()->label('选项多')->multiple()->defaultValue(['1', '2'])->options(['1' => "正常", '2' => '不正常',]), ]) ->setGroup('会员信息', 'member') ->setGroupComment('这是一个会员信息') ->setGroupCol(4) ->setFormControl([ 'j' => $this->formControl->textarea()->label('文本域')->required()->defaultValue('卧槽我\r\n卧槽"\'' . PHP_EOL), 'k' => $this->formControl->checkbox()->defaultValue(['a'])->options(['a' => '我', 'b' => '是', 'c' => '神',])->required()->label('标签')->tag(), 'l' => $this->formControl->checkbox()->defaultValue(['e', 'f'])->options(['d' => '我', 'e' => '是', 'f' => '神',])->required()->label('多选'), 'm' => $this->formControl->sw()->label('开关')->required()->checked(), 'n' => $this->formControl->hidden()->defaultValue('冲冲冲'), 'o' => $this->formControl->password()->label('密码')->required()->defaultValue('123456'), 'p' => $this->formControl->radio()->options(['g' => '我', 'h' => '是', 'i' => '神',])->defaultValue('h')->required()->label('单选'), 'q' => $this->formControl->range()->required()->label('范围')->defaultValue(1), 'r' => $this->formControl->staticText()->label('静态文本')->defaultValue('我是一个神!"\''), ]) ->setGroup('符合条件', 'where') ->setGroupComment('这是一个符合条件') ->setGroupCol(4) ->setFormControl([ 's' => $this->formControl->richText()->label('富文本')->config(['height' => '0'])->defaultValue('<p><a href="##">测试一下</a></p>'), 't' => $this->formControl->file()->label('文件上传')->required(), 'u' => $this->formControl->section()->required()->label('区间')->placeholder('$', '$')->defaultValue(1, 2), 'v' => $this->formControl->section()->placeholder('$', '$')->defaultValue('1111', 4), 'link1' => $this->formControl->selectLink()->label('联动1')->placeholder('请选择')->route('admin/link')->initOptions(['a' => 'A', 'b' => 'B'])->targetLink('link2', 'where')->start(), 'link2' => $this->formControl->selectLink()->label('联动2')->placeholder('请选择')->route('admin/link')->targetLink('link3', 'where')->through(), 'link3' => $this->formControl->selectLink()->label('联动3')->placeholder('请选择')->route('admin/link')->targetLink('link4', 'where')->through(), 'link4' => $this->formControl->selectLink()->label('联动2')->placeholder('请选择')->end(), ]); return $viewBuilder->render(); // return $this->render('console'); } }*/ public function actionLink() { //return $this->as403('登录已失效!'); return $this->asOk('success', [ ['value' => 0, 'text' => '狗'], ['value' => 0, 'text' => '猪'], ['value' => 0, 'text' => '羊'], ]); } /** * 退出登录 * @return \yii\web\Response * @throws UserException */ public function actionQuit() { $isGuest = $this->user->logout(); if ($isGuest) { return $this->goLogin(); } throw new UserException('Logout failure. '); } } ~~~