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. |
根据需求,可以使用不同的模型接口来管理嵌套和排序的记录。其行为将根据实现哪个特质而调整。
使用 NestedTree 特性 当需要固定结构时。这包括父子关系以及当记录需要按特定顺序显示时。
class Category extends Model
{
use \October\Rain\Database\Traits\NestedTree;
}了解更多关于 NestedTree 特性在 数据库文档。
使用 SimpleTree trait 当需要基本的父子关系时。
class Category extends Model
{
use \October\Rain\Database\Traits\SimpleTree;
}了解更多关于 SimpleTree 特性,请参阅数据库文档。
当记录需要按特定顺序显示时,使用 Sortable 特性。
class User extends Model
{
use \October\Rain\Database\Traits\Sortable;
}了解更多关于 Sortable 特性在 数据库文档 中。
使用 SortableRelation 模型特性 当记录需要在中间表中排序时,例如 多对多 关系类型。此特性要求 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