Trao đổi về dự án
PHP symfony Sonata

Symfony 4 / Sonata : Quản lý thứ tự bằng kéo và thả

Bạn cần cài đặt pixassociates/sortable-behavior-bundle và stof/doctrine-extensions-bundle composer require stof/doctrine-extensions-bundle composer require pixassociates/sortable-behavior-bundle Thêm cấu hình trong pix_sortable.yamlBằng cách thêm entity và...

Symfony 4 / Sonata : Quản lý thứ tự bằng kéo và thả

Bạn cần cài đặt pixassociates/sortable-behavior-bundlestof/doctrine-extensions-bundle

composer require stof/doctrine-extensions-bundle
composer require pixassociates/sortable-behavior-bundle

Thêm cấu hình trong pix_sortable.yaml
Bằng cách thêm entity và trường trong position_field (ở đây là wconf, và trường vị trí)

pix_sortable_behavior:
    db_driver: orm # mongodb default value : orm
    position_field:
        default: position #default value : position
        entities:
            App\Entity\Wconf: position
    sortable_groups:
        entities:
            #AppBundle\Entity\Baz: [ group ]
            
stof_doctrine_extensions:
    orm:
        default:
            sortable: true

Trong entity, bạn cần tạo một trường sẽ quản lý vị trí (gọi nó, ngẫu nhiên, là vị trí)

use Gedmo\Mapping\Annotation as Gedmo;

    /**
     * @Gedmo\SortablePosition
     * @ORM\Column(type="integer")
     */
    private $position;

    public function getPosition(): ?int
    {
        return $this->position;
    }

    public function setPosition(int $position): self
    {
        $this->position = $position;

        return $this;
    }


Trong controller, bạn cần thêm tham chiếu trong use, thêm route, và nút

use Sonata\AdminBundle\Route\RouteCollection;
use Pix\SortableBehaviorBundle\Services\PositionORMHandler as PositionHandler;


public $last_position = 0;
private $positionService;

protected $datagridValues = array(
	'_page' => 1,
	'_sort_by' => 'position',
	'_sort_order' => 'ASC',
);

public function setPositionService(PositionHandler $positionHandler)
{
        $this->positionService = $positionHandler;
}


protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('_action', null, array(
            'actions' => array(
                'move' => array(
                    'template' => '@PixSortableBehavior/Default/_sort_drag_drop.html.twig',
                    'enable_top_bottom_buttons' => false, 
                ),
            ),
        ))
        ;
}

protected function configureRoutes(RouteCollection $collection)
{
        $collection->add('move', $this->getRouterIdParameter().'/move/{position}');
}



Sau đó thêm tài sản JavaScript trong config/sonata_admin.yaml, thêm dịch vụ gedmo, và thêm cấu hình cho các đối số và cuộc gọi

                    
    assets:
        extra_javascripts:
            - bundles/pixsortablebehavior/js/jquery-ui.min.js // if you haven't got jQuery UI yet.
            - bundles/pixsortablebehavior/js/init.js
            

Trong cấu hình dịch vụ:

    gedmo.listener.sortable:
        class: Gedmo\Sortable\SortableListener
        calls:
            - [setAnnotationReader, ['@annotation_reader']]
        tags:
            - { name: doctrine.event_subscriber, connection: default }


    admin.wconf:
        class: App\Admin\WconfAdmin
        arguments: [~, App\Entity\Wconf, 'PixSortableBehaviorBundle:SortableAdmin']
        tags:
            - { name: sonata.admin, manager_type: orm, label: "Configuration des types" }
        public: true        
        calls:
              - [setPositionService, ["@pix_sortable_behavior.position"]]

Điều chỉnh ví dụ cho phù hợp với phiên bản đã cài đặt

Các ví dụ Symfony và Sonata vẫn gắn với các phiên bản được chỉ ra trong bài viết. Symfony 4.x và Symfony 6.2 không còn được duy trì: đối với một dự án hiện tại, hãy sử dụng một nhánh được duy trì và tài liệu tương ứng chính xác với các phụ thuộc của bạn.

php bin/console about
composer show symfony/framework-bundle sonata-project/admin-bundle

Trước khi tiếp tục với mã, hãy kiểm tra các chữ ký, dịch vụ, tuyến đường và mẫu liên quan. Sau đó thêm một bài kiểm tra chức năng bao phủ quyền, mã HTTP và kết quả hiển thị.

Tham chiếu : phiên bản Symfony được hỗ trợ.

Chia sẻ bài viết