hoangdv / nova-dynamic-views
用于简化在 Laravel Nova 中覆盖自定义头部和工具栏的工具
Requires
- php: ^8.1
- laravel/nova: ^4.32.1
This package is auto-updated.
Last update: 2024-09-20 08:15:49 UTC
README
此包可以帮助在视图的各个部分添加一些自定义占位符组件,如 custom-index-header
、custom-index-toolbar
、custom-detail-header
、custom-detail-toolbar
等。它提供了一个更简单的 API,并允许您多次使用这些“占位符”组件而不会相互覆盖。
支持此项目
要求
- PHP (^8.1 或更高版本)
- Laravel Nova (^4.0 或更高版本)
安装
使用 composer 安装包
composer require shuvroroy/nova-dynamic-views
在 \App\Providers\NovaServiceProvider
的 tools
方法中注册工具
use ShuvroRoy\NovaDynamicViews\NovaDynamicViews; ... public function tools() { return [ new NovaDynamicViews() ]; }
用法
假设您想向所有 index
视图的 toolbar
添加一个自定义按钮。只需创建一个 Vue 组件,就像您使用 custom-index-header
一样(如果您不知道如何创建,请参阅“创建自定义组件”部分)。让我们称它为 my-index-toolbar-btn
。现在您要做的就是将其注册到您的 \App\Nova\Resource
类中,在名为 customIndexToolbarComponents
的新方法中,该方法返回一个 \ShuvroRoy\NovaDynamicViews\CustomComponents
对象
public function customIndexToolbarComponents(): CustomComponents { return CustomComponents::make() ->addItem('my-index-toolbar-btn'); }
就是这样。现在您应该在工具栏中看到您组件的内容。
提供额外数据
如果您想向组件添加额外数据(例如标签),但不想进行额外请求,只需将其作为数组添加到 addItem
方法的第二个参数中
public function customIndexToolbarComponents(): CustomComponents { return CustomComponents::make() ->addItem('my-index-toolbar-btn', [ 'label' => 'My label' ]); }
访问资源数据
您可以通过使用 $this
在所有方法中访问资源类。在 detail
和 edit
组件中,您可以通过 request('id')
访问当前模型的 ID。因此,如果您需要在 customDetailHeaderComponents
、customDetailToolbarComponents
或您的 customUpdateHeaderComponents
中使用模型本身,您可以像这样查询它
public function customDetailToolbarComponents(): CustomComponents { $model = $this->model()->query()->where('id', request('id'))->first(); //... }
向容器添加 (tailwind) 类
如果您想向某个部分的容器 div 添加额外的 CSS 类(例如,向 customIndexToolbarComponents
部分添加 flex w-full justify-end items-center mx-3
),请在 make
函数中添加 class
(或使用 setClass
方法)
public function customIndexToolbarComponents(): CustomComponents { return CustomComponents::make('flex w-full justify-end items-center mx-3') ->addItem('my-index-toolbar-btn'); }
完整用法示例
use ShuvroRoy\NovaDynamicViews\CustomComponents; class Resource extends \Laravel\Nova\Resource { ... /** * Using the `custom-index-toolbar` placeholder component * * @return CustomComponents */ public function customIndexToolbarComponents(): CustomComponents { return CustomComponents::make('flex w-full justify-end items-center mx-3') ->addItem('my-index-toolbar-btn', [ 'title' => 'My first btn' ]) ->addItem('my-index-toolbar-btn', [ 'title' => 'My second btn' ]); } /** * Using the `custom-detail-header` placeholder component * * @return CustomComponents */ public function customDetailHeaderComponents(): CustomComponents { $model = $this->model()->query()->where('id', request('id'))->first(); return CustomComponents::make() ->addItem('my-other-component', [ 'id' => $model->id, 'name' => $model->name ]); } }
仅在特定资源上使用
如果您只想在特定资源上显示此按钮,例如仅对用户显示,只需将此方法添加到 \App\Nova\User
类中。
可用方法和区域
所有 custom-*-*
nova 占位符都作为以 Components
后缀的驼峰式方法可用
- ✅
customIndexHeaderComponents
- ✅
customIndexToolbarComponents
- ✅
customDetailHeaderComponents
- ✅
customDetailToolbarComponents
- ✅
customCreateHeaderComponents
- ✅
customAttachHeaderComponents
- ✅
customUpdateAttachHeaderComponents
- ✅
customUpdateHeaderComponents
- ✅
customLensHeaderComponents
创建自定义组件
这只是此文档的入门指南。有关更多信息,请参阅 https://nova.laravel.net.cn/docs/4.0/customization/resource-tools.html
使用 artisan 创建新的资源工具
php artisan nova:resource-tool acme/my-index-toolbar-btn
并回答所有提示中的所有问题。现在您可以在 customXXXComponents
(例如 customIndexToolbarComponents
)中使用此组件(位于 nova-components/my-index-toolbar-btn
)
鸣谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件