lupennat / nova-expandable-many
Laravel Nova - 可展开多行
v3.3.0
2024-03-12 17:29 UTC
Requires
- php: ^7.4|^8.0
- laravel/nova: >=4.32.6
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.16
README
要求
php: ^7.4 | ^8
laravel/nova: ^4
安装
composer require lupennat/nova-expandable-many:^3.0
使用
在资源上全局注册特性 HasExpandableMany
。
该特性包括对
indexFields
方法的覆盖,它调用FieldCollection
上的withoutListableFieldsNotExpandable
而不是withoutListableFields
;如果你已经注册了自定义的indexFields
覆盖,你可以手动进行,而无需使用提供的特性。
ExpandableMany 包自动为所有多关系字段启用新的 expandable
方法。
- 多对多关系
- 一对多关系
- 通过中间表的一对多关系
- 多态多对多关系
- 多态一对多关系
- 多态多对多关系
关系表(不包括任何自定义操作和透镜)将在索引页上以可折叠行的形式显示。
use Laravel\Nova\Fields\HasMany; use Laravel\Nova\Http\Requests\NovaRequest; class User extends Resource { use Lupennat\ExpandableMany\HasExpandableMany; public function fields(Request $request) { return [ HasMany::make('User Post', 'posts', Post::class)->expandable(); ]; } }
默认情况下,可展开使用 显示
和 隐藏
作为标签,你可以使用元数据来更改标签。
HasMany::make('User Post', 'posts', Post::class)->expandable() ->withMeta([ 'expandableShowLabel' => 'Custom Show', 'expandableHideLabel' => 'Custom Hide', ])
你也可以使用自定义HTML来定义
HasMany::make('User Post', 'posts', Post::class)->expandable() ->withMeta([ 'expandableShowHtml' => '<strong>Custom Show Html</strong>', 'expandableHideHtml' => '<strong>Custom Hide Html</strong>', ])
如果同时定义了自定义标签和HTML,则将使用HTML
默认情况下,可展开不会在浏览器历史记录中存储任何状态,你可以使用元数据来更改它
HasMany::make('User Post', 'posts', Post::class)->expandable() ->withMeta([ // 'expandableStoreStatus' => 'full', // will store status also for relationships 'expandableStoreStatus' => 'accordion', // will store status only for accordion 'expandableStoreStatus' => '', // will not store any status ])
可展开可以跳过,并显示空字段(默认为false)
HasMany::make('User Post', 'posts', Post::class)->expandable() ->withMeta([ 'expandableSkip' => true, 'expandableSkipLabel' => '', // default is '—' ])
可展开可以禁用标准操作(默认启用)
HasMany::make('User Post', 'posts', Post::class)->expandable() ->withMeta([ 'expandableUseStandardActions' => false, // disable create/edit/view/delete/restore ])
显示回调
可展开多行将为每个资源解析显示回调,你可以用它来动态地操作元属性。
HasMany::make('User Post', 'posts', Post::class) ->expandable(function(HasMany $field, $resource) { $resource->loadCount('posts'); $field->withMeta([ 'expandableShowLabel' => 'Show ' . $resource->posts_count, 'expandableSkip' => $resource->posts_count === 0 ]); })
默认情况下,可展开不解析关系,通过
$resource
访问关系属性将通过数据库执行查询来加载所有相关模型。
透镜
如果你想在透镜中使用可展开多行,你还需要在你的透镜中注册特性 HasExpandableManyLens
。
use Laravel\Nova\Fields\HasMany; use Laravel\Nova\Http\Requests\NovaRequest; use Laravel\Nova\Lenses\Lens; class UserLens extends Lens { use Lupennat\ExpandableMany\HasExpandableManyLens; public function fields(Request $request) { return [ HasMany::make('User Post', 'posts', Post::class)->expandable(); ]; } }
致谢
本包基于来自 Nova Expandable Row 的原始想法。