runroom-packages/sortable-behavior-bundle

提供了一种对管理员列表进行排序的方法

安装次数: 546,922

依赖者: 1

建议者: 0

安全: 0

星级: 9

关注者: 8

分支: 7

类型:symfony-bundle

0.19.1 2024-09-06 13:59 UTC

README

Latest Stable Version Latest Unstable Version License

Total Downloads Monthly Downloads Daily Downloads

此包提供定义可排序实体并在 Sonata Backoffice 中对其进行排序的能力。它受到了 pixSortableBehaviorBundle 的启发。

安装

打开命令行控制台,进入您的项目目录,然后执行以下命令以下载此包的最新稳定版本

composer require runroom-packages/sortable-behavior-bundle

启用包

然后,通过将其添加到项目 config/bundles.php 文件中注册的包列表中,启用包

// config/bundles.php

return [
    // ...
    Runroom\SortableBehaviorBundle\RunroomSortableBehaviorBundle::class => ['all' => true],
];

使用

此包会检查您是否正在使用 Gedmo Sortable 来处理实体排序,如果不是,它将使用默认的 ORM 实现,其中您需要手动在包的配置中添加实体。如果您正在使用 Gedmo,并且不想更改默认字段名 position,则无需为此包进行任何配置。

我们提供了一个特质,这样您就可以轻松地将位置字段与 Gedmo 配置添加到您希望能够排序的每个实体中

# src/Entity/Example.php

namespace App\Entity;

use Runroom\SortableBehaviorBundle\Behaviors\Sortable;

class Example
{
    use Sortable;
    // ... rest of your class
}

然后,在您的管理类中,您可以添加 SortableAdminTrait 特质以在列表视图中对实体进行排序

# src/Admin/ExampleAdmin.php

namespace App\Admin;

use Runroom\SortableBehaviorBundle\Admin\SortableAdminTrait;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;

class ExampleAdmin extends AbstractAdmin
{
    use SortableAdminTrait;

    protected function configureListFields(ListMapper $list): void
    {
        $list
            // ... rest of your list fields
            ->add(ListMapper::NAME_ACTIONS, ListMapper::TYPE_ACTIONS, [
                'actions' => [
                    // ... rest of your actions
                    'move' => [
                        'template' => '@RunroomSortableBehavior/sort.html.twig',
                    ],
                ],
            ]);
    }
}

就这样,您现在应该可以在管理类的列表视图中看到排序按钮。

配置

# config/packages/runroom_sortable_behavior.yaml

runroom_sortable_behavior:
    # position_handler can be any service id that implements the PositionHandlerInterface
    position_handler: Runroom\SortableBehaviorBundle\Service\GedmoPositionHandler # or Runroom\SortableBehaviorBundle\Service\ORMPositionHandler if gedmo is not found
    position_field:
        default: position # Default field name for the position
        # Only needed when not using Gedmo
        entities:
            App\Entity\Foobar: order
            App\Entity\Baz: rang
    # Only needed when not using Gedmo
    sortable_groups:
        entities:
            App\Entity\Baz: [group]

使用可拖动的列表代替上下按钮

为了使用可拖动的列表而不是上下按钮,请将 move 动作中的模板更改为 @RunroomSortableBehavior/sort_drag_drop.html.twig

protected function configureListFields(ListMapper $list): void
{
    $list
        // ... rest of your list fields
        ->add(ListMapper::NAME_ACTIONS, ListMapper::TYPE_ACTIONS, [
            'actions' => [
                // ... rest of your actions
                'move'   => [
                    'template' => '@RunroomSortableBehavior/sort_drag_drop.html.twig',
                    'enable_top_bottom_buttons' => true, // optional
                ],
            ],
        ]);
}

贡献

此包的源代码包含在 Runroom monorepo 中。我们欢迎在 runroom/runroom-packages 上为此包做出贡献。

许可证

此包受 MIT 许可证 的约束。