Giter Site home page Giter Site logo

springboardvr / nova-detached-actions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gobrightspot/nova-detached-actions

0.0 1.0 1.0 1.21 MB

A Laravel Nova tool to allow for placing actions in the Nova toolbar detached from the checkbox selection mechanism.

License: MIT License

CSS 2.15% Vue 20.56% JavaScript 16.56% PHP 60.73%

nova-detached-actions's Introduction

Laravel Nova Detached Actions Tool

A Laravel Nova tool to allow for placing actions in the Nova toolbar, detached from the checkbox selection mechanism.

⚠️ Keep in mind, since the action is detached from the row selection checkboxes in the resource table, you will not have a collection of models to iterate over. Detached actions are intended to be independent of the selction in the table. ⚠️ Also, keep in mind, pivot actions are not supported and have not been tested.

screenshot

Installation

You can install the package in to a Laravel app that uses Nova via composer:

composer require gobrightspot/nova-detached-actions

The tool will be automatically registered via the ToolServiceProvider - Thanks @milewski

Usage

Create a custom Nova Action file:

php artisan nova:action ExportUsers

Instead of extending the ExportUsers class with the Laravel\Nova\Actions\Action class, swap it with the Brightspot\Nova\Tools\DetachedActions\DetachedAction class.

Since we won't receive a collection of $models, you can remove the variable from the handle method, so that the signature is public function handle(ActionFields $fields).

You can also customize the button label, by overriding the label() method. If you do not override the label, it will 'humanize' the class name, in the example ExportUsers would become Export Users.

By default the detached action will only appear on the Index Toolbar.

If you want to also show the action on the resource index view (when users select a row with a checkbox), set the $public $showOnIndex = true; If you want to also show the action on the resource detail view (when user selects the action from the dropdown), set the $public $showOnDetail = true;

Here's a full example:

<?php

namespace App\Nova\Actions;

use Brightspot\Nova\Tools\DetachedActions\DetachedAction;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Laravel\Nova\Fields\ActionFields;

class ExportUsers extends DetachedAction
{
    use InteractsWithQueue, Queueable, SerializesModels;
    
    /**
     * Get the displayable label of the button.
     *
     * @return string
     */
    public function label()
    {
        return __('Export Users');
    }

    /**
     * Perform the action.
     *
     * @param  ActionFields  $fields
     *
     * @return mixed
     */
    public function handle(ActionFields $fields)
    {
        // Do work to export records

        return DetachedAction::message('It worked!');
    }

    /**
     * Get the fields available on the action.
     *
     * @return array
     */
    public function fields()
    {
        return [];
    }
}

Register the action on your resource:

<?php
...
    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [
            new App\Nova\Actions\ExportUsers
        ];
    }
...

Usage with the Laravel Nova Excel DownloadExcel action

You can easily integrate the DetachedAction tool with the Laravel Nova Excel DownloadExcel action by simply passing some additional data along using withMeta().

<?php
...
    /**
      * Get the actions available for the resource.
      *
      * @param  \Illuminate\Http\Request  $request
      * @return array
      */
     public function actions(Request $request)
     {
         return [
             (new DownloadExcel)->withHeadings()->askForWriterType()->withMeta([
                 'detachedAction' => true,
                 'label' => 'Export',
                 'showOnIndexToolbar' => true
             ])->confirmButtonText('Export'),
         ];
     }
 ...

License

The MIT License (MIT). Please see License File for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.