dutchbridge / datatablebundle
Symfony2 数据表格插件,用于 Doctrine 实体
dev-master
2018-02-28 17:42 UTC
Requires
- php: >=5.3.2
- symfony/framework-bundle: ~3.0
Suggests
- twitter/bootstrap: Add Bootstrap Css support, suggest v2.3.2
This package is not auto-updated.
Last update: 2024-09-28 16:20:32 UTC
README
Symfony2 数据表格插件,用于 Doctrine 实体。
状态:尚未准备就绪,正在开发中
我将尽快更新文档
在安装 DatatableBundle 之前,您需要拥有一个可正常工作的 Symfony2 安装。
截图
安装
-
使用 composer 下载 DatatableBundle
在 composer.json 中添加 DatatableBundle
{ "require": { "dutchbridge/datatablebundle": "dev-master" } }
运行以下命令让 composer 下载该插件
$ php composer.phar update
-
启用插件
在 kernel 中启用插件
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new DutchBridge\DatatableBundle\DutchBridgeDatatableBundle() ); }
-
Bootstrap 2.3 安装
注意:DatatableBundle 不包含 Twitter Bootstrap 的资产文件。例如,您可以下载 Bootstrap 仓库中文件的 ZIP 存档。
- 将 bootstrap css 文件复制到 DatatableBundle\Resources\public\css 目录。
- 将 bootstrap icons 文件复制到 DatatableBundle\Resources\public\img 目录。
- 将 bootstrap js 文件复制到 DatatableBundle\Resources\public\js 目录。
示例
-
创建 layout.html.twig
{% extends '::base.html.twig' %} {% block title %}UserBundle{% endblock %} {% block stylesheets %} <link href="{{ asset('bundles/dutchbridgedatatable/css/bootstrap.min.css') }}" rel="stylesheet" type="text/css" /> <link href="{{ asset('bundles/dutchbridgedatatable/css/dataTables_bootstrap.css') }}" rel="stylesheet" type="text/css" /> {% endblock %} {% block body%} {% block scripts %} <script src="{{ asset('bundles/dutchbridgedatatable/js/jquery-2.0.2.min.js') }}" type="text/javascript"></script> <script src="{{ asset('bundles/dutchbridgedatatable/js/bootstrap.min.js') }}" type="text/javascript"></script> <script src="{{ asset('bundles/dutchbridgedatatable/js/jquery.dataTables.min.js') }}" type="text/javascript"></script> <script src="{{ asset('bundles/dutchbridgedatatable/js/dataTables_bootstrap.js') }}" type="text/javascript"></script> {% endblock %} <div class="container"> {% block content %} {% endblock %} </div> {% endblock %}
-
创建 Datatable 类
<?php namespace DutchBridge\UserBundle\Datatable; use DutchBridge\DatatableBundle\Datatable\AbstractDatatableView; use DutchBridge\DatatableBundle\Datatable\Field; use DutchBridge\DatatableBundle\Datatable\Action; /** * Class UserDatatable * * @package DutchBridge\UserBundle\Datatable */ class UserDatatable extends AbstractDatatableView { /** * {@inheritdoc} */ public function build() { $this->setTableId('user_datatable'); $this->setSAjaxSource('user_results'); $this->setTableHeaders(array( 'Username', 'Email', 'Enabled', '' )); $nameField = new Field('username'); $emailField = new Field('email'); $enabledField = new Field('enabled'); $enabledField->setBSearchable('false'); $enabledField->setSWidth('90'); $enabledField->setMRender("render_boolean_icons(data, type, full)"); $idField = new Field('id'); $idField->setBSearchable('false'); $idField->setBSortable('false'); $idField->setMRender("render_action_icons(data, type, full)"); $idField->setSWidth('92'); $this->addField($nameField); $this->addField($emailField); $this->addField($enabledField); $this->addField($idField); $editAction = new Action(); $editAction->setType('route'); $editAction->setClass('btn btn-sm btn-primary'); $editAction->setRoute('user_edit'); $editAction->setIcon('icon-edit'); $editAction->addRouteParameter('userId', array('field' => 'id')); $removeAction = new Action(); $removeAction->setType('route'); $removeAction->setClass('btn btn-sm btn-danger'); $removeAction->setRoute('user_remove'); $removeAction->setIcon('icon-remove'); $removeAction->addRouteParameter('userId', array('field' => 'id')); $removeAction->addRouteParameter('lorem', array('value' => 'ipsum')); $this->addAction($editAction); $this->addAction($removeAction); } }
-
创建 index.html.twig
{% extends 'SgUserBundle::layout.html.twig' %} {% block title %}{{ title }}{% endblock %} {% block content %} <h2>{{ title }}</h2> {{ datatable_render(datatable) }} {% endblock %}
-
添加控制器操作
/** * Lists all User entities. * * @Route("/", name="user") * @Method("GET") * @Template() * * @return array */ public function indexAction() { /** * @var \DutchBridge\DatatableBundle\Factory\DatatableFactory $factory */ $factory = $this->get('sg_datatables.factory'); /** * @var \DutchBridge\DatatableBundle\Datatable\AbstractDatatableView $datatableView */ $datatableView = $factory->getDatatableView('DutchBridge\UserBundle\Datatable\UserDatatable'); return array( 'title' => 'Enabled Users', 'datatable' => $datatableView, ); } /** * Get all User entities. * * @Route("/results", name="user_results") * * @return \Symfony\Component\HttpFoundation\Response */ public function indexResultsAction() { /** * @var \DutchBridge\DatatableBundle\Datatable\DatatableData $datatable */ $datatable = $this->get('sg_datatables')->getDatatable('SgUserBundle:User'); /** * @var \Doctrine\ORM\QueryBuilder $qb */ $callbackFunction = function($qb) { $andExpr = $qb->expr()->andX(); $andExpr->add($qb->expr()->eq('fos_user.enabled', '1')); $qb->andWhere($andExpr); }; $datatable->addWhereBuilderCallback($callbackFunction); return $datatable->getSearchResults(); }
-
处理关联
我们扩展用户实体到多个 OneToMany 关联(帖子(Post)和评论(Comment))。
<?php namespace DutchBridge\UserBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; use FOS\UserBundle\Model\User as BaseUser; use Doctrine\ORM\Mapping as ORM; use DutchBridge\AppBundle\Entity\Comment; use DutchBridge\AppBundle\Entity\Post; /** * Class User * * @ORM\Entity * @ORM\Table(name="fos_user") * * @package DutchBridge\UserBundle\Entity */ class User extends BaseUser { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\OneToMany( * targetEntity="DutchBridge\AppBundle\Entity\Post", * mappedBy="createdBy" * ) */ private $posts; /** * @ORM\OneToMany( * targetEntity="DutchBridge\AppBundle\Entity\Comment", * mappedBy="createdBy" * ) */ private $comments; /** * Ctor. */ public function __construct() { parent::__construct(); // your own logic $this->posts = new ArrayCollection(); $this->comments = new ArrayCollection(); } // ... }
为了显示所有博客标题的逗号分隔视图,我们在 UserDatatable 类中添加以下内容。
$this->setTableHeaders(array( 'Username', 'Email', 'Enabled', 'Posts', '' )); $postsField = new Field('posts'); $postsField->setRenderArray(true); $postsField->setRenderArrayFieldName('title'); $postsField->setSName('posts.title'); $this->addField($nameField); $this->addField($emailField); $this->addField($enabledField); $this->addField($postsField); $this->addField($idField);
结果应如下所示