October CMS 提供功能以用于排序和重新排序数据库记录。对于支持结构化列表的行为,你可以定义一个 structure 属性以启用该功能。各种 模型特征 用于支持重新排序包括嵌套集、简单树和可排序模型。
列表控制器 和 关联控制器 后端行为目前支持使用相关定义中的 structure 属性重新排序记录的选项。定义后,页面会显示一个带有拖动句柄的记录列表,允许它们进行排序和重组。
# config_list.yaml
# ...
structure:
showTree: true
showReorder: true
showSorting: false
maxDepth: 2下列配置属性可以使用。
| Property | Description |
|---|---|
| showTree | displays a tree hierarchy for parent/child records. Default: true |
| treeExpanded | if tree nodes should be expanded by default. Default: true |
| showReorder | displays an interface for reordering records. Default: true |
| showSorting | allows sorting records, disables the structure when sorted. Default: true |
| maxDepth | defines the maximum levels allowed for reordering. Default: null |
| dragRow | allow dragging the entire row in addition to the reorder handle. Default: true |
| permissions | the permissions that the current backend user must have to modify the structure. Supports either a string for a single permission or an array of permissions of which only one is needed to grant access. |
根据需求,可以使用不同的模型接口来管理嵌套和排序的记录。其行为将根据所实现的 trait 进行调整。
使用 NestedTree 特性,当需要固定结构时。这包括父子关系,以及当记录需要以特定顺序显示时。
class Category extends Model
{
use \October\Rain\Database\Traits\NestedTree;
}了解有关 NestedTree 特性的更多信息,请查阅 数据库文档。
当需要基本的父子关系时,使用 SimpleTree 特性。
class Category extends Model
{
use \October\Rain\Database\Traits\SimpleTree;
}有关 SimpleTree trait 的更多信息,请参阅数据库文档。
使用 Sortable 特性,当记录需要以特定顺序显示时。
class User extends Model
{
use \October\Rain\Database\Traits\Sortable;
}了解更多关于 Sortable 特性 在 数据库文档。
关联记录的排序可以通过 关系控制器 使用 结构 属性来实现。支持的关系类型如下。
使用 SortableRelation 模型 trait 当记录需要在一个中间表中排序时,例如一个 多对多 关系类型。此 trait 要求 pivotSortable 属性在关系中被定义其值为位于中间表中的可排序列名。
class User extends Model
{
use \October\Rain\Database\Traits\SortableRelation;
/**
* @var array belongsToMany
*/
public $belongsToMany = [
'roles' => [
Role::class,
'table' => 'users_roles',
'pivotSortable' => 'sort_order',
]
];
}然后 在你的关系配置中, 你应该启用 showReorder 属性并禁用 showTree 属性。
roles:
#...
structure:
showReorder: true
showTree: false