承接 mikeevstropov/sortable-tree-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mikeevstropov/sortable-tree-bundle

最新稳定版本:v1.1

Composer 安装命令:

composer require mikeevstropov/sortable-tree-bundle

包简介

Offers a sortable feature for your Symfony2/3 admin tree listing

README 文档

README

Packagist

mevSortableTreeBundle

Offers a sortable feature for your Symfony2/3 admin tree listing

screenshot

Install requirements

SonataAdminBundle
- the SonataAdminBundle provides a installation article here:
http://symfony.com/doc/current/cmf/tutorial/sonata-admin.html

Install and enable Tree Extension from gedmo/doctrine-extensions
- nested behavior will implement the standard Nested-Set behavior on your Entity
(check stof/doctrine-extensions-bundle for easier integration in your project) https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md

Installation

composer require mikeevstropov/sortable-tree-bundle

Configuration

Enable the mevSortableTreeBundle to your kernel:

// app/AppKernel.php

class AppKernel extends Kernel
{
	public function registerBundles()
	{
		$bundles = [
			// ...
			new Mev\SortableTreeBundle\MevSortableTreeBundle(),
		];
		// ...
	}
}

Include MevSortableTreeBundle:SortableTree to third service argument:

# app/config/services.yml

#   SonataAdminBundle Category
    admin.category:
        class: AppBundle\Admin\CategoryAdmin
        arguments:
            - ~
            - AppBundle\Entity\Category
            - 'MevSortableTreeBundle:SortableTree'
        tags:
            - { name: sonata.admin, manager_type: orm, label: Category }

Create new routes and the action field in Admin Class:

// src/AppBundle/Admin/CategoryAdmin.php

// ...

use Sonata\AdminBundle\Route\RouteCollection;

class CategoryAdmin extends AbstractAdmin
{
	// ...
	protected function configureRoutes(RouteCollection $collection)
	{
		$collection->add('up', $this->getRouterIdParameter().'/up');
        $collection->add('down', $this->getRouterIdParameter().'/down');
    }
    
    protected function configureFormFields(FormMapper $formMapper)
    {
        // create custom query to hide the current element by `id`

        $subjectId = $this->getRoot()->getSubject()->getId();
        $query = null;

        if ($subjectId)
        {
            $query = $this->modelManager
                ->getEntityManager('AppBundle\Entity\Category')
                ->createQueryBuilder('c')
                ->select('c')
                ->from('AppBundle:Category', 'c')
                ->where('c.id != '. $subjectId);
        }
        
        // ...
        $formMapper->add('parent', 'sonata_type_model', array(
            'query' => $query,
            'required' => false, // remove this row after the root element is created
            'btn_add' => false,
            'property' => 'name'
        ));
    }

	protected function configureListFields(ListMapper $listMapper)
	{
		// ...
		$listMapper->add('_action', null, array(
			'actions' => array(
				'up' => array(
                    'template' => 'MevSortableTreeBundle:Default:list__action_up.html.twig'
                ),
                'down' => array(
                    'template' => 'MevSortableTreeBundle:Default:list__action_down.html.twig'
                )
			)
		));
	}
}

Configure sort the list of models by root and lft fields:

// src/AppBundle/Admin/CategoryAdmin.php

// ...

class CategoryAdmin extends AbstractAdmin
{
	// ...
	public function createQuery($context = 'list')
	{
		$proxyQuery = parent::createQuery('list');
        // Default Alias is "o"
        // You can use `id` to hide root element
        // $proxyQuery->where('o.id != 1');
        $proxyQuery->addOrderBy('o.root', 'ASC');
        $proxyQuery->addOrderBy('o.lft', 'ASC');
    
		return $proxyQuery;
	}
	// ...
}

That's it!

ToDo

  • Sortable behaveor for root elements (but, you can hide root element)

统计信息

  • 总下载量: 51.13k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-15