夜火笔记

dcat admin 运用工具表单在列表中制作一个弹出编辑框

2024-06-20
笔记 phplaraveldcat-admin
4分钟
703字

注:以下内容基于php 8.1,laravel 10, dcat admin 2

上一篇[[dcat admin中新建方法页面丢失样式的问题]] 说到如何创建一个独立的表单页面来更新数据,实现特定的用户充值功能。

这次说说如何不创建独立的页面(数据表单),直接在列表页弹出modal模态框,进行表单提交,数据更新。

主要用到工具表单动作 ,在laravel-admin 中分别是 数据表单 , 自定义行&批量操作 - 行操作 可以互相参考。

工具表单 tools-form

使用命令创建

Terminal window
1
php artisan admin:form UserRechargeForm

生成工具表单文件 app/Admin/Forms/UserRechargeForm.php ,参照文档将上一篇单独页面的代码挪到这里。

1
namespace App\Admin\Forms;
2
3
use App\Models\User;
4
use Dcat\Admin\Widgets\Form;
5
use Dcat\Admin\Traits\LazyWidget;
6
use Dcat\Admin\Contracts\LazyRenderable;
7
8
class UserRechargeForm extends Form implements LazyRenderable
9
{
10
    public $title = '充值';
11
    use LazyWidget;
12
13
    public function handle(array $input)
14
    {
15
        // dump($input);
41 collapsed lines
16
17
        // 获取外部传递参数
18
        $id = $this->payload['id'] ?? null;
19
20
        $recharge_money = $input['recharge_money'] ?? null;
21
        $recharge_address = $input['recharge_address'] ?? null;
22
        $transaction_id = $input['transaction_id'] ?? null;
23
24
        // ... 充值的逻辑代码
25
26
        // return $this->response()->error('Your error message.');
27
28
        return $this
29
                ->response()
30
                ->success('Processed successfully.')
31
                ->refresh();
32
    }
33
34
    public function form()
35
    {
36
        $id = $this->payload['id'] ?? null;
37
38
        // 输入字段
39
        $this->text('money', '充值金额')
40
            ->required()
41
            ->rules(['numeric', 'min:0']);
42
        // ...
43
    }
44
45
    public function default()
46
    {
47
        $id = $this->payload['id'] ?? null;
48
        $user = User::find($id);
49
50
        return [
51
            'id' => $id,
52
            'name'  => $user->name,
53
            // ...
54
        ];
55
    }
56
}

这里文档中给的使用范例 Modal::make(),一直没搞懂在那里使用的,直到看到后面的代码

动作 action

命令

Terminal window
1
php artisan admin:action

2 grid-row ,然后输入要创建的 action 类名 UserRecharge

在创建的文件中编辑

app\Admin\Actions\Grid\UserRecharge.php
1
// ...
2
use Dcat\Admin\Widgets\Modal;
3
use App\Admin\Forms\UserRechargeForm;
4
5
class UserRecharge extends RowAction
6
{
7
    protected $title = '充值';
8
9
    public function render(){
10
        // 实例化表单类并传递自定义参数
11
        $form = UserRechargeForm::make()->payload(['id' => $this->getKey()]);
12
13
        return Modal::make()
14
            ->lg()
5 collapsed lines
15
            ->title($this->title)
16
            ->body($form)
17
            ->button($this->title);
18
    }
19
    // ...

将action动作按钮放入数据表格列表中

回到控制器,主要是 $grid->actions 这一句

app/Admin/Controllers/UserController.php
1
// ...
2
use App\Admin\Actions\Grid\UserRecharge;
3
4
class UserController extends AdminController
5
{
6
// ...
7
    protected function grid()
8
    {
9
return Grid::make(Administrator::with(['roles']), function (Grid $grid) {
10
// ...
11
$grid->actions([new UserRecharge()]);
12
// ...

添加好之后,即可在列表的操作栏看到充值的文字选项了。

本文标题:dcat admin 运用工具表单在列表中制作一个弹出编辑框
文章作者:夜火/xloong
发布时间:2024-06-20
Copyright 2026
站点地图