datomatic/nova-detached-actions

一个Laravel Nova工具,允许将操作放置在Nova工具栏中,与复选框选择机制分离。

2.1.2 2024-05-24 12:52 UTC

This package is auto-updated.

Last update: 2024-09-24 13:38:26 UTC


README

Latest Version on Packagist Total Downloads

Laravel Nova Detached Actions工具

一个Laravel Nova工具,允许将操作放置在Nova工具栏中,与复选框选择机制分离。

操作与资源表中的行选择复选框分离,因此您将不会有一个模型集合来迭代。分离操作旨在独立于表中的选择。

这是从gobrightspot/nova-detached-actions废弃仓库分支而来,我使用vue 3组合式API重写了大部分代码。

⚠️不支持枢轴操作,并且尚未进行测试。

actions

移动端兼容性

mobile actions

安装

您可以通过composer将此包安装到使用Nova的Laravel应用程序中

composer require datomatic/nova-detached-actions

工具将通过ToolServiceProvider自动注册

用法

创建自定义Nova操作文件

php artisan nova:action ExportUsers

与其用Laravel\Nova\Actions\Action类扩展ExportUsers类,不如用Datomatic\Nova\Tools\DetachedActions\DetachedAction类替换它。

由于我们不会收到$models集合,您可以从handle方法中删除该变量,以便签名为public function handle(ActionFields $fields)

注册后,分离操作将出现在索引工具栏上。

以下是一个完整示例

<?php

namespace App\Nova\Actions;

use Datomatic\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;
    
    public function name(): string
    {
        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 [];
    }
}

在您的资源上注册操作

/**
 * Get the actions available for the resource.
 *
 * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
 * @return array
 */
public function actions(NovaRequest $request)
{
    return [
        new App\Nova\Actions\ExportUsers // or App\Nova\Actions\ExportUsers::make() 
    ];
}

自定义按钮

可见按钮与不可见按钮

默认情况下,组件将显示前3个按钮,并将其余按钮放入下拉菜单中。在下拉菜单中,您不能添加图标或额外类,因为它使用nova下拉组件。如果您想更改每个资源可见按钮的数量,您可以在资源类中使用additionalInformation方法,如下所示

public static function additionalInformation(Request $request)
{
    return [
        'visibleActionsLimit' => 2
    ];
}

invisible actions

自定义按钮类

该包附带一些常见的默认HTML类,这些类应用于操作按钮。在组件中,我们自动分配以下

hover:bg-gray-200 dark:hover:bg-gray-900 flex-shrink-0 rounded focus:outline-none focus:ring inline-flex items-center font-bold px-3 h-9 text-sm flex-shrink-0

开发人员可以动态添加类,使用DetachedAction类上的extraClasses()方法。

extraClasses()方法

您可以使用任何tailwind/nova类。

return [
   (new ImportUsers)->extraClasses('bg-primary-500 text-white hover:black')
];

$defaultClasses变量

您也可以扩展DetachedAction类并更改$defaultClasses变量中的默认类。

例如,您可以添加与nova主按钮相等的类

shadow ring-primary-200 dark:ring-gray-600 bg-primary-500 hover:bg-primary-400 dark:hover:bg-primary-400 active:bg-primary-600 text-white dark:text-gray-800

添加图标

您可以通过指定小写图标名称使用104 Heroicon图标

return [
   (new ImportUsers)->icon('add')
];

您还可以使用iconClasses自定义该图标的显示

return [
   (new ImportUsers)->icon('upload')->iconClasses('mr-3 -ml-2')
];

chunking和重复调用handle()

如果您在后台启动操作,Nova会将总记录数分成多个块,并为每个块调用您DetachedAction的handle()函数。这可能会对性能产生意外的负面影响,因为系统将为每个记录块执行您的操作。这种情况发生在\Laravel\Nova\Actions\Action.phphandleRequest()函数中。

为了防止这种情况,最简单的方法是在您的DetachedAction中覆盖此函数。以下是一个简单的示例,仅调度一个作业,不进行任何检查或其他逻辑:

/** @return array<int,string> */
public function handleRequest(ActionRequest $request): array
{
    dispatch(new GenerateTicketReport($request->resolveFields()));
    return DetachedAction::message('Nice job!');
}

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件