需求
批量发货时,需要可以修改每个发货单中的收货地址,所以需要点击批量生成后生成一个表单,并获取到选取的ids
遇到的问题
加载form时无法获取ids
解决方法
- 重写Modal类
GridBatchModal.php
namespace App\Admin\Actions;
use Dcat\Admin\Widgets\Modal;
class GridBatchModal extends Modal
{
protected function getRenderableScript() {
if (!$this->getRenderable()) {
return;
}
$url = $this->renderable->getUrl();
return <<<JS
target.on('{$this->target}:load', function () {
var key = Dcat.grid.selected('')
Dcat.helpers.asyncRender('{$url}&ids='+key, function (html) {
body.html(html);
{$this->loadScript}
target.trigger('{$this->target}:loaded');
});
});
JS;
}
}
- 使用modal
CommonGridBatchAction.php
<?php
namespace App\Admin\Actions\Grid;
use App\Admin\Actions\GridBatchModal;
use App\Admin\Forms\BatchSureSendForm;
use Dcat\Admin\Grid\BatchAction;
class CommonGridBatchAction extends BatchAction
{
/**
* @return string
*/
protected $title = 'Title';
protected $function;
public function __construct($title = null,$function = null)
{
$this->title = $title;
$this->function = $function;
}
public function render()
{
$function = $this->function;
return $this->$function();
}
public function batch_erp_out_order_send(){
$keys = $this->getKey();
$form = BatchSureSendForm::make()->payload([ ]);
$modal = GridBatchModal::make()
->xl()
->title($this->title)
->body($form)
->button($this->title);
return $modal;
}
}
- 使用Form
BatchSureSendForm.php
<?php
namespace App\Admin\Forms;
use App\Models\ExpressOrder;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;
class BatchSureSendForm extends Form implements LazyRenderable
{
use LazyWidget;
/**
* Handle the form request.
*
* @param array $input
*
* @return mixed
*/
public function handle(array $input)
{
DB::beginTransaction();
//发货流程
if($res == true){
DB::commit();
return $this
->response()
->success('发货成功')
->refresh();
}else{
DB::rollBack();
return $this
->response()
->error($msg);
}
}
/**
* Build a form here.
*/
public function form()
{
//或者$this->payload['ids']
dump(request('ids'));
}