Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
4
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

@tateren

laravel-admin でRowAction の表示方式を変更する

環境

  • Laravel Framework 8.2.0
  • laravel-admin 1.8.4

経緯

laravel-admin 1.8.4 では RowAction はハンバーガーメニューみたいな形式になってるが、ドキュメントの TOP のスクリーンショットにあるようなリンク形式にしたくて若干ハマった。
2020-09-14 23_40_02-Window.png
こんなの

変更方法

config/admin.php
    /*
    |--------------------------------------------------------------------------
    | The global Grid action display class.
    |--------------------------------------------------------------------------
    */
    'grid_action_class' => \Encore\Admin\Grid\Displayers\DropdownActions::class,

config/admin.php 内の grid_action_class\Encore\Admin\Grid\Displayers\Actions::class に変更するだけで良かった。

改めてドキュメントを見るとこの辺で grid_action_class について書いてるけど古い方式だから(?)触れられていないのかな?
https://laravel-admin.org/docs/en/model-grid-custom-actions#Old%20version%20compatible

もう少しカスタマイズする

Actions::render(View|Edit|Delete) をオーバーライドする。

renderDelete はクラスやデータ属性を下手に消すと削除用のスクリプトが動かなくなるので注意。

CustomActions.php
<?php

namespace App\Admin\Grid\Displayers;

use Encore\Admin\Grid\Displayers\Actions;

class CustomActions extends Actions
{
    protected function renderView()
    {
        return <<<EOT
<a href="{$this->getResource()}/{$this->getRouteKey()}" class="{$this->grid->getGridRowName()}-view btn btn-sm btn-default">
    <i class="fa fa-eye"></i> View
</a>
EOT;
    }

    protected function renderEdit()
    {
        return <<<EOT
<a href="{$this->getResource()}/{$this->getRouteKey()}/edit" class="{$this->grid->getGridRowName()}-edit btn btn-sm btn-default">
    <i class="fa fa-edit"></i> Edit
</a>
EOT;
    }

    protected function renderDelete()
    {
        $this->setupDeleteScript();

        return <<<EOT
<a href="javascript:void(0);" data-id="{$this->getKey()}" class="{$this->grid->getGridRowName()}-delete btn btn-sm btn-default">
    <i class="fa fa-trash"></i> Delete
</a>
EOT;
    }
}

config/admin.php を修正。

     /*
     |--------------------------------------------------------------------------
     | The global Grid action display class.
     |--------------------------------------------------------------------------
     */
-    'grid_action_class' => \Encore\Admin\Grid\Displayers\Actions::class,
+    'grid_action_class' => \App\Admin\Grid\Displayers\CustomActions::class,

127.0.0.1_8000_admin_auth_permissions(iPad).png

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
4
Help us understand the problem. What are the problem?