laravel admin grid 模型表格行操作

羊舌高明
2023-12-01

laravel-admin 关闭(新增,删除,批量操作,导入导出,修改)按钮

$grid->actions(function ($actions) {    // 去掉查看    $actions->disableView();});   //去掉复选框  $grid->disableRowSelector();  //禁用行操作  $grid->disableActions();
//禁用单选框$grid->disableRowSelector();//禁用新增按钮$grid->disableCreation();
$grid->actions(function ($actions) {    //关闭行操作 删除    $actions->disableDelete();});//关闭批量操作$grid->tools(function ($tools) {    $tools->batch(function ($batch) {        $batch->disableDelete();    });});$form

        //表单bottom
        $form->disableReset();
        $form->disableEditingCheck();
        $form->disableViewCheck();

        //表单右上角
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });

$grid
     // 表单右上角
         $grid->disableExport();
        $grid->disableCreation();
        // 操作按钮
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $resDemand = Demand::find($actions->getKey());
            $edit_html='';
            //通过
            if($resDemand->status==1){
                $edit_html.="<span class='mb-5'><a class='grid-row-pass' title='通过' data-id='{$actions->getKey()}'><i class='glyphicon glyphicon-ok'></i></a></span>";
  
            $actions->append($edit_html);
        });
//  批量操作
        $grid->tools(function (Grid\Tools $tools) {
            $tools->batch(function (Grid\Tools\BatchActions $actions) {
                $actions->disableDelete();
            });
        });
$show
        //表单右上角
        $show->panel()->tools(function ($tools){
            $tools->disableDelete();
            $tools->disableEdit();
        });

模型表格行操作

model-grid默认有三个行操作编辑、删除和详情,可以通过下面的方式关闭它们:

 $grid->actions(function ($actions) {
 	//关闭删除
    $actions->disableDelete();
    //关闭编辑
    $actions->disableEdit();
     //关闭详情
    $actions->disableView();
});

可以通过传入的$actions参数来获取当前行的数据:

 $grid->actions(function ($actions) {
    
    // 当前行的数据数组
    $actions->row;
    
    // 获取当前行主键值
    $actions->getKey();
});

如果有自定义的操作按钮,可以通过下面的方式添加:

$grid->actions(function ($actions) {
    
    // append一个操作
    $actions->append('<a href=""><i class="fa fa-eye"></i></a>');

    // prepend一个操作
    $actions->prepend('<a href=""><i class="fa fa-paper-plane"></i></a>');
}

如果有比较复杂的操作,可以参考下面的方式:

先定义操作类

<?php

namespace App\Admin\Extensions;

use Encore\Admin\Admin;

class CheckRow
{
    protected $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

    protected function script()
    {
        return <<<SCRIPT

$('.grid-check-row').on('click', function () {
    
    // Your code.
    console.log($(this).data('id'));
    
});

SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());

        return "<a class='btn btn-xs btn-success fa fa-check grid-check-row' data-id='{$this->id}'></a>";
    }
    
    public function __toString()
    {
        return $this->render();
    }
}

然后添加操作:

$grid->actions(function ($actions) {
    
    // 添加操作
    $actions->append(new CheckRow($actions->getKey()));
});
 类似资料: