bernhardh / nova-dynamic-views
用于简化Laravel Nova中自定义头部和工具栏覆盖的工具
Requires
- php: >=7.1.0
README
此存储库已被存档且不再受支持。它不与Nova 4兼容。请查看https://github.com/shuvroroy/nova-dynamic-views中的表单。
Nova动态视图
此包旨在替代您自己覆盖custom-index-header
、custom-index-toolbar
、custom-detail-header
、custom-detail-toolbar
等。它提供了一个更简单的API,并允许您多次使用这些“占位符”组件而不相互覆盖。
安装
使用composer要求此包
composer require bernhardh/nova-dynamic-views
在您的\App\Providers\NovaServiceProvider
中的tools
方法中注册此工具
use Bernhardh\NovaDynamicViews\NovaDynamicViews; ... public function tools() { return [ new NovaDynamicViews() ]; }
使用
假设您想向所有index
视图的toolbar
添加自定义按钮。只需创建一个vue组件,就像您使用custom-index-header
一样(如果您不知道如何创建自定义组件,请参阅“创建自定义组件”部分)。让我们称它为my-index-toolbar-btn
。现在您要做的就是将其注册到您的\App\Ņova\Resource
类中,在新的方法customIndexToolbarComponents
内,该方法返回一个\Bernhardh\NovaDynamicViews\CustomComponents
对象
public function customIndexToolbarComponents() { return CustomComponents::make() ->addItem('my-index-toolbar-btn'); }
就是这样。现在您应该在工具栏中看到您组件的内容。
提供额外数据
如果您想向组件添加额外数据(例如标签)(不进行额外请求),只需将其作为数组添加到addItem
方法的第二个参数。
public function customIndexToolbarComponents() { return CustomComponents::make() ->addItem('my-index-toolbar-btn', [ 'label' => 'My label' ]); }
访问资源数据
您可以通过使用$this
在所有方法中访问资源类。在detail
和edit
组件中,您可以通过使用request('id')
访问当前模型的ID。因此,如果您需要在customDetailhHeaderComponents
、customDetailToolbarComponents
或您的customUpdateHeaderComponents
中获取模型本身,您可以像这样查询:
public function customDetailToolbarComponents() { $model = $this->model()->query()->where('id', request('id'))->first(); //... }
向容器添加(tailwind)类
如果您想向某个部分(例如向customIndexToolbarComponents
部分添加flex w-full justify-end items-center mx-3
)的容器div添加额外的CSS类,请在make
函数中添加class
(或使用setClass
方法)。
public function customIndexToolbarComponents() { return CustomComponents::make('flex w-full justify-end items-center mx-3') ->addItem('my-index-toolbar-btn'); }
完整使用示例
class Resource extends \Laravel\Nova\Resource { ... /** * Using the `custom-index-toolbar` placeholder component * * @return array[] */ public function customIndexToolbarComponents() { 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 array[] */ public function customDetailHeaderComponents() { $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 占位符(除 custom-dashboard-header
之外)都可作为以 Components
后缀的驼峰式方法使用
customAttachHeaderComponents
customCreateHeaderComponents
customDetailhHeaderComponents
customDetailToolbarComponents
customIndexHeaderComponents
customIndexToolbarComponents
customLensHeaderComponents
customUpdateAttachHeaderComponents
customUpdateHeaderComponents
创建自定义组件
这只是一个关于此的入门级文档。有关更多信息,请参阅 https://nova.laravel.net.cn/docs/3.0/customization/resource-tools.html
使用 artisan 创建新的资源工具
php artisan nova:resource-tool acme/my-index-toolbar-btn
并回答提示的所有问题。现在您可以在您的 customXXXComponents
(例如 customIndexToolbarComponents
)中(位于 nova-components/my-index-toolbar-btn
)使用此组件。