devhelp/datatables-bundle

Datatables support for Symfony 2

Installs: 33

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 4

Forks: 0

Open Issues: 2

Language:JavaScript

Type:symfony-bundle

dev-master 2015-08-25 21:23 UTC

This package is not auto-updated.

Last update: 2024-11-09 14:55:33 UTC


README

#DatatablesBundle Build Status

SensioLabsInsight

This bundle provides a simple integration of Datatables.

##Installation using composer.json

    "require": {
        "devhelp/datatables-bundle": "dev-master"
    },
    php app/console assetic:dump

##Configuration

###Create configuration

#####config.yml

    assetic:
        ...
        bundles:        [ DevhelpDatatablesBundle ]
        ...

#####Minimal configuration #####config.yml

    devhelp_datatables:
        default_per_page: 10
        grids:
            product_grid:
                model: Devhelp\DemoBundle\Entity\Product
                routing: get_grid
                colum   ns:
                    - { title: 'ID',          data: 'id',            alias : 'p.id' }
                    - { title: 'Name',        data: 'name',          alias : 'p.name' }
                    - { title: 'Description', data: 'description',   alias : 'p.description' }
                    - { title: 'Price',       data: 'price',         alias : 'p.price' }
                    - { title: 'Category',    data: 'category.name', alias : 'c.name'}

#####Full configuration #####config.yml

    devhelp_datatables:
        default_per_page: 10
        grids:
            product_grid:
                model: Devhelp\DemoBundle\Entity\Product
                routing: get_grid
                use_filters: true
                default_per_page: 10
                columns:
                    - { title: 'ID',          data: 'id',            alias : 'p.id',          searchable: 1, visible: 1, width: "10%" }
                    - { title: 'Name',        data: 'name',          alias : 'p.name',        searchable: 1, visible: 1, width: "30%" }
                    - { title: 'Description', data: 'description',   alias : 'p.description', searchable: 0, visible: 1, width: "10%" }
                    - { title: 'Price',       data: 'price',         alias : 'p.price',       searchable: 1, visible: 1, width: "10%" }
                    - { title: 'Category',    data: 'category.name', alias : 'c.name',        searchable: 1, visible: 1, width: "60%" }

#####AppKernel.php

    $bundles = array(
        ...
        new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
        new JMS\SerializerBundle\JMSSerializerBundle(),
        new Devhelp\DatatablesBundle\DevhelpDatatablesBundle(),
        ...
    )

###Create entities and repository class

#####Product.php

    namespace Devhelp\DemoBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;

    /**
     * @ORM\Entity(repositoryClass="Devhelp\DemoBundle\Entity\ProductRepository")
     * @ORM\Table(name="product")
     *
     */
    class Product
    {
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;

        /**
         * @ORM\Column(type="string", length=100)
         */
        protected $name;

        /**
         * @ORM\Column(type="decimal", scale=2)
         */
        protected $price;

        /**
         * @ORM\Column(type="text")
         */
        protected $description;

        /**
         * @ORM\ManyToOne(targetEntity="Category")
         * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
         */
        protected $category;
    }

#####Category.php

    namespace Devhelp\DemoBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;

    /**
     * @ORM\Entity
     * @ORM\Table(name="category")
     */
    class Category
    {
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;

        /**
         * @ORM\Column(type="string", length=100)
         */
        protected $name;

    }

#####ProductRepository.php

    namespace Devhelp\DemoBundle\Entity;

    use Devhelp\DatatablesBundle\Lib\AbstractDatatablesRepository;

    class ProductRepository extends AbstractDatatablesRepository
    {

        public function getBaseQuery()
        {
            return $this->createQueryBuilder('p')->leftJoin('p.category','c');
        }

    }

##Usage #####Controller

    /**
    *
    * @Route("/grid", name="product_grid")
    */
    public function indexAction()
    {
        $grid = $this->get('devhelp.datatables');
        $grid->load('product_grid');

        $jsonResult =  $grid->getResult();
        return new Response($jsonResult);
    }

#####View

    {{ render_datatables_grid('product_grid') }}