Skip to content

介绍

使用 Form Builder 组件的时候,需要遵循一些约定。本人在实践过程中总结提炼,然后梳理成文档,这样便于你更好的维护和使用。

以 Commom 模块为例

首先我们在 Common 创建一个 Tables(命名根据实际情况自定义) 文件夹

Tables 文件加下创建一个抽象基类 Dynamic (类名也是你自己定义)

php
namespace Modules\Common\Tables;

abstract class Dynamic
{
    public function __invoke($key = null)
    {
        if ($key) {
            return [
                $key => $this->{$key}()
            ];
        }

        return [
            'form' => $this->form(),
            'table' => $this->table()
        ];
    }


    abstract protected function form();

    abstract protected function table();
}

抽象类只做一个公共方法 __invoke 魔术方法,子类必须实现 formtable 两个方法。来看一下 角色表单 的实现

php
namespace Modules\Common\Tables;


use CatchForm\Components\Rules\Control;
use CatchForm\Form;
use CatchForm\Table\Table;
use Modules\Permissions\Enums\DataRange;
use Modules\Permissions\Enums\MenuType;
use Modules\Permissions\Models\Departments;
use Modules\Permissions\Models\Roles;

class Role extends Dynamic
{
    // table 实现
    public function table()
    {
        return Table::make('permissions/roles')->columns(function (Table $table) {
            $table->column('角色名称', 'role_name');

            $table->column('角色标识', 'identify');

            // 角色描述
            $table->column('角色描述', 'description');

            // 创建时间
            $table->column('创建时间', 'created_at');

            $table->column('操作')->type('operate');
        })->dialog(800)->rowKey();

    }

    // form 实现
    protected function form()
    {
        return Form::make(function (Form $form) {
            $form->treeSelect('parent_id', '父级角色')->data(
                Roles::query()->get(['id as value', 'role_name as label', 'parent_id'])->toTree(id: 'value')->toArray()
            )->class('w-full')->emitChange()->checkStrictly(true);

            $form->text('role_name', '角色名称ssss')->maxlength(30)->showWordLimit()->required();

            $form->text('identify', '角色标识')->maxlength(30)->showWordLimit()->required();

            $form->textarea('description', '角色描述')->maxlength(200)->showWordLimit();

            $form->select('data_range', '数据权限')->options(DataRange::class)
                ->whenEqual(DataRange::Personal_Choose->value(), function (Control $control){
                        $control->hide('departments');
                });

            $form->treeSelect('departments', '自定义权限')->data(
                Departments::query()->get()->toTree()->toArray()
            )->toProps([
                'label' => 'department_name',
                'value' => 'id'
            ])->showCheckbox(true)->required()->multiple()->valueKey('id');

            $form->custom('permissions', '权限')->type('div')
                ->subs(function (Form $form){
                    $form->tree('permissions', '')->data([])->toProps([
                        'label' => 'permission_name',
                        'value' => 'id'
                    ])->showCheckbox(true)->loadData('permissionsOption', 'props.data')->class('w-full');
                })->class(['w-full h-40 pt-2 pl-2 overflow-auto border border-gray-300 rounded']);
        });
    }
}

在控制器处理显示

php
namespace Modules\Common\Http\Controllers;

use Catch\Base\CatchController;
use Illuminate\Http\Request;
use Modules\Common\Tables\Role;

/**
 * @group 公共模块
 *
 * @subgroup 公共演示
 * @subgroupDescription CatchAdmin 后台公共演示
 */
class DynamicController extends CatchController
{
    public function role(Role $role, Request $request)
    {
        return $role($request->get('key'));
    }
}

总结

这么做,可以很好的管理和约束,防止多人合作导致 tableform 随意存放,难以维护