Dcat Admin 批量操作/批量审批功能(弹窗)实现

本文介绍了如何在Dcat Admin中实现批量审批功能。用户选择行后,点击批量审批按钮,弹窗显示,输入审批意见并选择通过或退回,最后提交。主要涉及批量审批按钮的添加、弹窗及数据处理文件的创建,以及相应的动作文件编写。

功能:选择行,点击批量审批按钮,触发弹窗,输入审批意见,选择通过或退回,提交。如下图展示:

 

主要分三部分:1、添加批量审批按钮 2、弹窗及提交后的数据处理文件  3.动作文件

一、弹窗及提交后的数据处理文件

建立Batchspform.php文件,位置看命名空间

<?php

namespace App\Admin\Forms;

use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Contracts\LazyRenderable;
use App\Models\HrOvertime; 
use App\Models\HrUsersp; 
use App\Models\Hrop; 
class Batchspform extends Form implements LazyRenderable
{
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    use LazyWidget;
    public function handle(array $input)
    {
        
        //接收弹窗提交过来的数据,进行处理
       
        $id = explode(',', $input['id'] ?? null); //处理提交过来的批量选择的行的id
      
         if (!$id) {
            return $this->response()->error('参数错误');
        }

        $opinion = $input['opinion'] ?? null;  //form提交过来的审批意见
        $flag = $input['flag'] ?? null;    //form提交过来的通过还是退回
         
        //写你的处理逻辑
        foreach ($id as $key => $value) {
        XXXXX::update(['flag'=>$flag,'opinion'=>$opinion]);
       
}
             return $this->response()->success('提交成功')->refresh(); 
       
      
    }

    /**
     * Build a form here.
     */
    public function form()     
    {
         //弹窗界面
        $this->textarea('opinion','意见');
        $this->radio('flag','审批')->options(['1'=>'同意','0'=>'退回'])->required();
        //批量选择的行的值怎么传递看下面
        $this->hidden('id')->attribute('id', 'batchsp-id'); //批量选择的行的id通过隐藏元素 提交时一并传递过去
       
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        //设置默认值
       return [
            'opinion'  =>'属实',
          
        ];
    }
}

二、动作文件

  建立 Batchsp.php  内容如下

<?php

namespace App\Admin\Actions\Grid;

use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\BatchAction;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Dcat\Admin\Widgets\Modal;
use App\Admin\Forms\Batchspform as Batchspform;  //引入弹窗文件

class Batchsp extends BatchAction
{
    /**
     * @return string
     */
	protected $title = '批量审批';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
   
public function render()
    {
        // 实例化表单类并传递自定义参数
        $form = Batchspform::make();

        return Modal::make()
            ->lg()
            ->title($this->title)
            ->body($form)
            ->onLoad($this->getModalScript())
            ->button($this->title);
    }


  protected function getModalScript()
    {
       // 弹窗显示后往隐藏的id表单中写入批量选中的行ID
        return <<<JS
// 获取选中的ID数组
var key = {$this->getSelectedKeysScript()}
//batchsp-id 与 之前弹窗隐藏的绑定的id一致
$('#batchsp-id').val(key);    
JS;
    }





 
}

三、添加按钮

use App\Admin\Actions\Grid\Batchsp;  //头部引入动作

$grid->batchActions([new Batchsp()]);   //表格中添加批量下拉按钮

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值