功能:选择行,点击批量审批按钮,触发弹窗,输入审批意见,选择通过或退回,提交。如下图展示:
主要分三部分: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()]); //表格中添加批量下拉按钮