Custom Actions

row actions and batch action of the data grid, and the row actions of the data are reconstructed into drop-down menu actions:

In this form, it is more convenient to extend the row action. The mechanism after refactoring can make you more elegant and custom data actions.

By default, the data table has 3 row actions Edit, View and Delete and a Batch Delete action. Refer to the following to customize your data action.

Row action

Suppose you want to add a copy action to the article list. After clicking and copying the data of this line, run the following command to generate the action class:

Php artisan admin:action Post\\Replicate --grid-row --name="copy"

The class name is specified as Post\\Replicate, just to put this action class under the Post namespace, you can specify any space according to your module.

The above command will generate the class file app/Admin/Actions/Post/Replicate.php:

<?php

namespace App\Admin\Actions\Post;

use Ezadev\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;

class Replicate extends RowAction
{
    public $name = 'copy';

    public function handle(Model $model)
    {
        // $model ...

        return $this->response()->success('Success message.')->refresh();
    }
}

The commit of the action will handle by the handle method in the class, so we only need to simply modify the logic of the handle method:

The first parameter $model of the handle method is the Eloquent model of the current row data, which can be manipulated directly.

The final step is to add to the grid:

Page redirection

If your row action just click to jump to another page:

In the href method, you can use $this->getResource() to get the current resource root path. You can use $this->getKey() to get the primary key value of the current row.

Batch action

Suppose you want to add a batch copy action, first run the following command to generate a batch action class:

Generate the class file app/Admin/Actions/Post/BatchReplicate.php:

Similarly, we only need to simply modify the logic of the handle method:

Add to the batch action:

Then select a few rows of data in the list, click on it to see the new batch copy:

Interaction

When you click on an action, you will often need to make some confirmations or fill in additional data. There are two ways to interact:

pop-up dialogue box

For example, the above copy action, after clicking, you need to pop up a dialog box to confirm the action, add a dialog method to the class:

In this way, after clicking Copy, the following confirmation box will pop up, and the action will be submitted after confirmation.

Suppose that the 'topic list' has a report action. After clicking, you need to pop up a form, fill in the type and reason of the report, and refer to the following method:

Add the following code to the action class:

When the action is clicked, a form form pops up:

In the handle method, we need to add a second parameter to get the form value:

The current method support for the form reference:

Grid Tools

Custom actions, in addition to being displayed in the action column of the data table and in the drop-down menu of the batch action, can also be displayed in the tool area of ​​the table (Filter button line)

Batch action

If you want to put the button for the batch action in the tool area instead of the drop-down menu for the batch action, you need to make some changes to the action class:

The value of $selector corresponds to the button CSS selector in the html method. Click the 'Report' button to submit the action.

Then add it to the toolbar of the form:

Normal action

Suppose you need to add an 'Import Data` button to the header of the form. After clicking, pop up the form upload file to import the data. Refer to the steps below.

Create a normal action class by running the following command:

The generated class file app/Admin/Actions/Post/ImportPost.php:

Modify this class to provide the ability to upload files and add logic to process the data:

Response

Here's an example of the simplest return:

After the action class is processed, return a `Success! The success of ``s tips, here are a few other types of returns:

Refresh the page after returning:

Redirect to other pages after returning:

Download the file after returning:

Last updated