2017 © Pedro Peláez
 

symfony-bundle datatablebundle

Symfony2 Datatable For Doctrine Entities

image

dutchbridge/datatablebundle

Symfony2 Datatable For Doctrine Entities

  • Wednesday, February 28, 2018
  • by ajanssen
  • Repository
  • 2 Watchers
  • 1 Stars
  • 353 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 1 Versions
  • 1 % Grown

The README.md

DatatableBundle

Symfony2 Datatable For Doctrine Entities., (*1)

Status: not yet ready, hard-development, (*2)

I will update the documentation ASAP

Before installing DatatableBundle you need to have a working installation of Symfony2., (*3)

Screenshot

Screenshot

Installation

  1. Download DatatableBundle using composer, (*4)

    Add DatatableBundle in your composer.json:, (*5)

    {
        "require": {
            "dutchbridge/datatablebundle": "dev-master"
        }
    }
    

    Tell composer to download the bundle by running the command:, (*6)

    bash $ php composer.phar update, (*7)

  2. Enable the bundle, (*8)

    Enable the bundle in the kernel:, (*9)

    ``` php <?php // app/AppKernel.php, (*10)

    public function registerBundles() { $bundles = array( // ... new DutchBridge\DatatableBundle\DutchBridgeDatatableBundle() ); } ```, (*11)

  3. Bootstrap 2.3 installation, (*12)

    The DatatableBundle no contains the assets files from Twitter Bootstrap. You can e.g. download a ZIP archive with the files from the Bootstrap repository on Github., (*13)

    • Copy into the DatatableBundle\Resources\public\css directory the bootstrap css file.
    • Copy into the DatatableBundle\Resources\public\img directory the bootstrap icons files.
    • Copy into the DatatableBundle\Resources\public\js directory the bootstrap js file.

Example

  1. Create your layout.html.twig, (*14)

    ``` html {% extends '::base.html.twig' %}, (*15)

    {% block title %}UserBundle{% endblock %}, (*16)

    {% block stylesheets %}, (*17)

    <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 %}, (*18)

    {% block body%}, (*19)

    {% 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 %} ```, (*20)

  2. Create your Datatable class, (*21)

    ``` php <?php, (*22)

    namespace DutchBridge\UserBundle\Datatable;, (*23)

    use DutchBridge\DatatableBundle\Datatable\AbstractDatatableView; use DutchBridge\DatatableBundle\Datatable\Field; use DutchBridge\DatatableBundle\Datatable\Action;, (*24)

    /**, (*25)

    • Class UserDatatable
    • @package DutchBridge\UserBundle\Datatable */ class UserDatatable extends AbstractDatatableView { /**, (*26)

      • {@inheritdoc} */ public function build() { $this->setTableId('user_datatable'); $this->setSAjaxSource('user_results');, (*27)

        $this->setTableHeaders(array( 'Username', 'Email', 'Enabled', '' ));, (*28)

        $nameField = new Field('username');, (*29)

        $emailField = new Field('email');, (*30)

        $enabledField = new Field('enabled'); $enabledField->setBSearchable('false'); $enabledField->setSWidth('90'); $enabledField->setMRender("render_boolean_icons(data, type, full)");, (*31)

        $idField = new Field('id'); $idField->setBSearchable('false'); $idField->setBSortable('false'); $idField->setMRender("render_action_icons(data, type, full)"); $idField->setSWidth('92');, (*32)

        $this->addField($nameField); $this->addField($emailField); $this->addField($enabledField); $this->addField($idField);, (*33)

        $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'));, (*34)

        $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'));, (*35)

        $this->addAction($editAction); $this->addAction($removeAction); } } ```, (*36)

  3. Create your index.html.twig, (*37)

    ``` html {% extends 'SgUserBundle::layout.html.twig' %}, (*38)

    {% block title %}{{ title }}{% endblock %}, (*39)

    {% block content %}, (*40)

    <h2>{{ title }}</h2>
    
    {{ datatable_render(datatable) }}

    {% endblock %} ```, (*41)

  4. Add controller actions, (*42)

    ``` php /**, (*43)

    • Lists all User entities.
    • @Route("/", name="user")
    • @Method("GET")
    • @Template()
    • @return array */ public function indexAction() { /**, (*44)

      • @var \DutchBridge\DatatableBundle\Factory\DatatableFactory $factory */ $factory = $this->get('sg_datatables.factory');

      /**, (*45)

      • @var \DutchBridge\DatatableBundle\Datatable\AbstractDatatableView $datatableView */ $datatableView = $factory->getDatatableView('DutchBridge\UserBundle\Datatable\UserDatatable');

      return array( 'title' => 'Enabled Users', 'datatable' => $datatableView, ); }, (*46)

    /**, (*47)

    • Get all User entities.
    • @Route("/results", name="user_results")
    • @return \Symfony\Component\HttpFoundation\Response */ public function indexResultsAction() { /**, (*48)

      • @var \DutchBridge\DatatableBundle\Datatable\DatatableData $datatable */ $datatable = $this->get('sg_datatables')->getDatatable('SgUserBundle:User');

      /**, (*49)

      • @var \Doctrine\ORM\QueryBuilder $qb */ $callbackFunction =, (*50)

        function($qb) { $andExpr = $qb->expr()->andX(); $andExpr->add($qb->expr()->eq('fos_user.enabled', '1')); $qb->andWhere($andExpr); };, (*51)

      $datatable->addWhereBuilderCallback($callbackFunction);, (*52)

      return $datatable->getSearchResults(); } ```, (*53)

  5. Working with Associations, (*54)

    We extend the user entity to multiple OneToMany associations (Post and Comment)., (*55)

    ``` php <?php, (*56)

    namespace DutchBridge\UserBundle\Entity;, (*57)

    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;, (*58)

    /**, (*59)

    • Class User
    • @ORM\Entity
    • @ORM\Table(name="fos_user")
    • @package DutchBridge\UserBundle\Entity */ class User extends BaseUser { /**, (*60)

      • @ORM\Id
      • @ORM\Column(type="integer")
      • @ORM\GeneratedValue(strategy="AUTO") */ protected $id;

      /**, (*61)

      • @ORM\OneToMany(
      • targetEntity="DutchBridge\AppBundle\Entity\Post",
      • mappedBy="createdBy"
      • ) */ private $posts;

      /**, (*62)

      • @ORM\OneToMany(
      • targetEntity="DutchBridge\AppBundle\Entity\Comment",
      • mappedBy="createdBy"
      • ) */ private $comments;

      /**, (*63)

      • Ctor. */ public function __construct() { parent::__construct();, (*64)

        // your own logic, (*65)

        $this->posts = new ArrayCollection(); $this->comments = new ArrayCollection(); }, (*66)

      // ..., (*67)

    }, (*68)


    For a comma-separated view of all blog titles we add the following to the UserDatatable class. ``` php $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);

    The result should looks like that:, (*69)

    Screenshot

The Versions

28/02 2018

dev-master

9999999-dev https://github.com/dutchbridge/DatatableBundle

Symfony2 Datatable For Doctrine Entities

  Sources   Download

MIT

The Requires

 

by Avatar dutchbridge

symfony2 table grid datatable