laravel-admin自定义行操作 批量行操作 按钮菜单等 ,老年人记忆系列!!!
自定义行操作
右键显示菜单
//引入命名
use Encore\Admin\Grid\Displayers\ContextMenuActions;
//grid()方法
$grid->setActionClass(ContextMenuActions::class);
Controller
//引入命名
use App\Admin\Actions\Yun\isok;
use App\Admin\Actions\Yun\isokno;
//gird()方法
$grid->actions(function ($actions) {
if(!$actions->getAttribute('status')||$actions->getAttribute('status')==1){
$grid->actions(function ($actions) {
$actions->disableEdit();
$actions->disableview();
$status=$actions->getAttribute('status');
if($status==1){
$actions->add(new isok);
$actions->add(new isokno);
}elseif($status==2){
$actions->add(new isokno);
}elseif($status==3){
$actions->add(new isok);
}
});
App\Admin\Actions\Yun\isok;
<?php
namespace App\Admin\Actions\Yun;
use Encore\Admin\Actions\RowAction;
class isok extends RowAction
{
public $name = '通过';
/**
* @return string
*/
public function handle()
{
// $model ...
$res=\DB::table('yuns')->where('id',$this->getKey())->update(['status'=>2]);
if($res){
return $this->response()->success('已通过')->refresh();
} else{
return $this->response()->error('失败请重新尝试.')->refresh();
}
}
}
use App\Admin\Actions\Yun\isokno;
<?php
namespace App\Admin\Actions\Yun;
use Encore\Admin\Actions\RowAction;
class isokno extends RowAction
{
public $name = '拒绝';
/**
* @return string
*/
public function handle()
{
// $model ...
$res=\DB::table('yuns')->where('id',$this->getKey())->update(['status'=>3]);
if($res){
return $this->response()->success('已拒绝')->refresh();
} else{
return $this->response()->error('失败请重新尝试.')->refresh();
}
}
}
自定义批量行操作
Controller
//引入命名
use App\Admin\Actions\Yun\setok;
use App\Admin\Actions\Yun\setokno;
//grid()方法
$grid->batchActions(function ($batch) {
$batch->add(new setok());
$batch->add(new setokno());
});
use App\Admin\Actions\Yun\setok;
<?php
namespace App\Admin\Actions\Yun;
use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;
class setok extends BatchAction
{
public $name = '批量通过';
public function handle(Collection $collection)
{
foreach ($collection as $model) {
// ...
$res=\DB::table('yuns')->where('id',$model->id)->where('status',1)->update(['status'=>2]);
}
return $this->response()->success('已通过')->refresh();
}
public function dialog()
{
$this->confirm('确定通过?');
}
}
use App\Admin\Actions\Yun\setokno;
<?php
namespace App\Admin\Actions\Yun;
use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;
class setokno extends BatchAction
{
public $name = '批量拒绝';
public function handle(Collection $collection)
{
foreach ($collection as $model) {
// ...
$res=\DB::table('yuns')->where('id',$model->id)->where('status',1)->update(['status'=>3]);
}
return $this->response()->success('已拒绝')->refresh();
}
public function dialog()
{
$this->confirm('确定拒绝?');
}
}
自定义按钮
//引入命名
use App\Admin\Actions\Yun\Delall;
//grid()方法
$grid->tools(function (Grid\Tools $tools) {
$tools->append(new Delall());
});
use App\Admin\Actions\Yun\Delall;
<?php
namespace App\Admin\Actions\Yun;
use Encore\Admin\Actions\Action;
use Illuminate\Http\Request;
class Delall extends Action
{
protected $selector = '.report-posts';
public function handle(Request $request)
{
// $request ...
$url="http://xxx.com";
$res=$this->getapktj($url);
if($res){
return $this->response()->success('删除成功')->refresh();
}else{
return $this->response()->error('删除失败')->refresh();
}
}
//跨服务器高级用法
public function getapktj($url){
$method ="GET";
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$ret = curl_exec($curl);
$all=json_decode($ret,true);
return $all;
//返回数组
}
public function dialog(){
$this->confirm('确定删除全部用户上传图片?');
}
public function html(){
return <<<HTML
<a class="btn btn-sm btn-danger report-posts">一键删除</a>
HTML;
}
}