pavloniym / nova-action-buttons
Laravel Nova 字段。
v1.1.1
2023-03-24 17:59 UTC
Requires
- php: ^7.3|^8.0
README
这个 Laravel Nova 包允许您直接在资源表视图中执行操作。
需求
php: >=8.0
laravel/nova: ^4.1
安装
使用 Composer 在 Laravel Nova 项目中安装此包
composer require pavloniym/nova-action-buttons
使用方法
单个按钮
use Pavloniym\ActionButtons\ActionButton; public function fields(Request $request) { return [ // ... Nova default fields ActionButton::make('') // Name in resource table column ->icon('<svg></svg>') // Svg icon (optional) ->title('Refresh') // Title (optional) ->styles([]) // Custom css styles (optional) ->classes([]) // Custom css classes (optional) ->action(new RefreshAction, $this->resource->id) // Provide action instance and resource id ->asToolbarButton(), // Display as row toolbar button (optional) // ... Nova default fields ]; }
按钮集合
use Pavloniym\ActionButtons\ActionButton; public function fields(Request $request) { return [ // ... Nova default fields ActionButtons::make()->collection([ ActionButton::make('')->action(), ActionButton::make('')->action(), ActionButton::make('')->action(), ]) // ... Nova default fields ]; }
这些字段(包括
ActionButton
和ActionButtons
)在索引和详细视图中都可用(感谢 @CosminBd)
注意事项
- 目前,为了使用此字段,您仍需要在资源
actions()
方法中声明操作。 - 仅在
confirm-action-modal
操作上进行了测试 - 您应该在按钮的
action()
方法中提供动作实例 - 如果您有依赖于资源实例的操作字段 -> 您应该在动作构造函数中注入资源,因为 Nova 不在索引行的
fields
方法上提供NovaRequest
实例
class RefreshAction extends Action { private Torrent $torrent /** * @param Torrent $torrent */ public function __construct(Torrent $torrent) { $this->torrent = $torrent; } /** * Get the fields available on the action. * * @param NovaRequest $request * @return array|null */ public function fields(NovaRequest $request): ?array { // $request is empty if action is called from index row (or inline) // so use instance injected to action constructor $torrent = (fn(): ?Torrent => $request?->selectedResources()?->first())(); $torrent = $torrent ?? $this->torrent; if ($torrent) { return [ File::make('File')->creationRules(['required']) ]; } return null; } }
要隐藏索引或详细视图中的操作,您可以在操作声明中添加相应的方法
ActionButton::make('My action') ->action((new RefreshAction)->onlyOnDetail(), $this->resource?->id) ->icon('') ->asToolbarButton(),
这适用于动作按钮和动作按钮组,并在动作组中的单个操作上工作
要运行不带确认的操作,您可以将 $withoutConfirmation = true
属性添加到 Laravel Nova 动作中,或者在声明动作按钮时将其作为方法提供
ActionButton::make('My action') ->action((new RefreshAction)->withoutConfirmation(), $this->resource?->id) ->icon('') ->asToolbarButton(),
许可证
此项目是开源软件,根据 MIT 许可证 许可。