workup / nova-sortable
这个 Laravel Nova 扩展包允许您通过拖放的方式在 Nova 资源索引视图中重新排列模型(基于 optimistdigital/nova-sortable 分支开发)。
Requires
- php: >=8.0
- laravel/nova: ^4.24.0
- outl1ne/nova-translations-loader: ^5.0
- spatie/eloquent-sortable: ^3.10.0|^4.0
- dev-master
- 3.4.4.001
- v2.x-dev
- 2.4.0.001
- 2.4.0
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.17
- 1.4.16
- 1.4.15
- 1.4.14
- 1.4.13
- 1.4.12
- 1.4.11
- 1.4.10
- 1.4.9
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- dev-dependabot/npm_and_yarn/nth-check-2.0.1
This package is auto-updated.
Last update: 2024-09-28 10:41:28 UTC
README
这个 Laravel Nova 扩展包允许您通过拖放的方式在 Nova 资源索引视图中重新排列模型。
内部使用 Spatie 的 eloquent-sortable。
要求
php: >=8.0laravel/nova: ^4.24.0
功能
- 拖放重新排序(在索引视图或 HasMany 视图中)
- 支持 BelongsTo/MorphsTo 重新排序,带有枢纽表
- 移动到开始和结束箭头(使项目成为第一个/最后一个)
- 来自 eloquent-sortable 的所有功能
- 本地化
截图
安装
通过 Composer 在 Laravel Nova 项目中安装此包
# Install package
composer require workup/nova-sortable
用法
创建迁移
使用 Laravel 迁移在模型中添加一个排序字段
// Add order column to the model Schema::table('some_model', function (Blueprint $table) { $table->integer('sort_order'); }); // Set default sort order (just copy ID to sort order) DB::statement('UPDATE some_model SET sort_order = id');
实现 eloquent-sortable
实现 Spatie 的 eloquent-sortable 接口并应用特质
use Spatie\EloquentSortable\Sortable; use Spatie\EloquentSortable\SortableTrait; class SomeModel extends Eloquent implements Sortable { use SortableTrait; public $sortable = [ 'order_column_name' => 'sort_order', 'sort_when_creating' => true, ]; ... }
当模型没有排序配置时,将使用默认的 eloquent-sortable 配置。
应用到 Nova 资源
将此包中的 HasSortableRows 特质应用到资源上
use Workup\NovaSortable\Traits\HasSortableRows; class MyResource extends Resource { use HasSortableRows; ... }
注意!这会覆盖 indexQuery() 方法。
按请求/资源禁用排序
您可以通过覆盖资源方法中的 canSort() 来禁用按请求或按资源排序,如下所示
public static function canSort(NovaRequest $request, $resource) { // Do whatever here, ie: // return user()->isAdmin(); // return $resource->id !== 5; return true; }
注意!这需要您禁用缓存(见下文)。
禁用缓存
如果您想因为使用 canSort 或运行测试而禁用缓存,可以在具有 HasSortableRows 特质的资源上将 sortableCacheEnabled 设置为 false。以下是一个示例
class Artist extends Resource { use HasSortableRows; public static $sortableCacheEnabled = false; }
或者,如果您想临时禁用可排序缓存(用于测试),可以在资源上调用 Resource::disableSortabilityCache()。
自定义可排序选项
Nova 排序顺序
要按不同的顺序在 Nova 中排序您的资源,可以在 $sortable 数组中将 nova_order_by 标志设置为 DESC(默认为 ASC)。
class SomeModel extends Eloquent implements Sortable { use SortableTrait; public $sortable = [ 'order_column_name' => 'sort_order', 'sort_when_creating' => true, 'nova_order_by' => 'DESC', ]; ... }
忽略策略
如果您有一个具有 authorizedToUpdate 为 false 的资源,但希望用户仍能对其进行排序,可以使用 ignore_policies 标志,如下所示
class SomeModel extends Eloquent implements Sortable { use SortableTrait; public $sortable = [ 'order_column_name' => 'sort_order', 'sort_when_creating' => true, 'ignore_policies' => true, ]; ... }
在 HasMany 关系上排序
注意!资源只能在 索引视图 或 HasMany 列表视图 上进行排序,但不能同时使用两种视图!
在 HasMany 上排序很简单。在模型的 $sortable 数组中添加 'sort_on_has_many' => true。如下所示
public $sortable = [ 'order_column_name' => 'sort_order', 'sort_when_creating' => true, 'sort_on_has_many' => true, ];
对 HasMany 的排序配置可以按模型逐个应用,也可以添加到 eloquent-sortable 的所有模型配置中。
return [ // Spatie sortable configuration /** * Add sort on has many in all the models. **/ 'sort_on_has_many' => true, ];
在 ManyToMany 关系上排序
在 BelongsToMany 和 MorphToMany 关系上排序是可用的,但需要特殊步骤。
有关详细信息,请参阅以下文档: 排序 ManyToMany 关系(带枢纽表)。
本地化
可以使用以下发布命令发布翻译文件
php artisan vendor:publish --provider="Workup\NovaSortable\ToolServiceProvider" --tag="translations"
您可以通过创建一个具有区域名称的新翻译文件(例如 et.json)并将 JSON 从现有的 en.json 复制到其中来将您的翻译添加到 resources/lang/vendor/nova-sortable/。
其他用例
使用 indexQuery
此包覆盖了资源的 indexQuery,如果您仍然想使用它,可以按照以下方式操作
use HasSortableRows { indexQuery as indexSortableQuery; } public static function indexQuery(NovaRequest $request, $query) { // Do whatever with the query // ie $query->withCount(['children', 'descendants', 'modules']); return parent::indexQuery($request, static::indexSortableQuery($request, $query)); }
致谢
许可证
Nova Sortable 是开源软件,许可协议为 MIT 许可协议。
