Skip to content

Latest commit

 

History

History
720 lines (578 loc) · 14.6 KB

File metadata and controls

720 lines (578 loc) · 14.6 KB

DOCUMENTATION

  1. Entity

Create an entity in the src/AppBundle/Entity directory of your project:

namespace AppBundle\Entity;

use DateTime;

/**
 * Employee entity.
 */
class Employee {

    /**
     * Age.
     *
     * @var integer
     */
    private $age;

    /**
     * Id.
     *
     * @var integer
     */
    private $id;

    /**
     * Name.
     *
     * @var string
     */
    private $name;

    /**
     * Office.
     *
     * @var string
     */
    private $office;

    /**
     * Position.
     *
     * @var string
     */
    private $position;

    /**
     * Salary.
     *
     * @var integer
     */
    private $salary;

    /**
     * Start date.
     *
     * @var DateTime
     */
    private $startDate;

    /**
     * Constructor.
     */
    public function __construct() {
        // NOTHING TO DO
    }

    /**
     * Get the age.
     *
     * @return integer Returns the age.
     */
    public function getAge() {
        return $this->age;
    }

    /**
     * Get the id.
     *
     * @return integer Returns the id.
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Get the name.
     *
     * @return string Returns the name.
     */
    public function getName() {
        return $this->name;
    }

    /**
     * Get the office.
     *
     * @return string Returns the office.
     */
    public function getOffice() {
        return $this->office;
    }

    /**
     * Get the position.
     *
     * @return string Returns the position.
     */
    public function getPosition() {
        return $this->position;
    }

    /**
     * Get the salary.
     *
     * @return integer Returns the salary.
     */
    public function getSalary() {
        return $this->salary;
    }

    /**
     * Get the start date.
     *
     * @return DateTime Returns the start date.
     */
    public function getStartDate() {
        return $this->startDate;
    }

    /**
     * Set the age.
     *
     * @param integer $age The age.
     * @return Employee Returns this employee.
     */
    public function setAge($age) {
        $this->age = $age;
        return $this;
    }

    /**
     * Set the name.
     *
     * @param string $name The name.
     * @return Employee Returns this employee.
     */
    public function setName($name) {
        $this->name = $name;
        return $this;
    }

    /**
     * Set the office.
     *
     * @param string $office The office.
     * @return Employee Returns this employee.
     */
    public function setOffice($office) {
        $this->office = $office;
        return $this;
    }

    /**
     * Set the position.
     *
     * @param string $position The position.
     * @return Employee Returns this employee.
     */
    public function setPosition($position) {
        $this->position = $position;
        return $this;
    }

    /**
     * Set the salary.
     *
     * @param integer $salary The salary.
     * @return Employee Returns this employee.
     */
    public function setSalary($salary) {
        $this->salary = $salary;
        return $this;
    }

    /**
     * Set the start date.
     *
     * @param DateTime $startDate The start date.
     * @return Employee Returns this employee.
     */
    public function setStartDate($startDate) {
        $this->startDate = $startDate;
        return $this;
    }
}
  1. Repository

Create a repository in the src/AppBundle/Repository directory of your project:

namespace AppBundle\Repository;

use WBW\Bundle\JQuery\DataTablesBundle\Repository\DefaultDataTablesRepository;

/**
 * Employee repository.
 */
class EmployeeRepository extends DefaultDataTablesRepository {

}
  1. Provider

Create a provider in the src/AppBundle/Provider directory of your project:

namespace AppBundle\Provider;

use AppBundle\Entity\Employee;
use DateTime;
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesColumnInterface;
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesResponseInterface;
use WBW\Bundle\JQuery\DataTablesBundle\Factory\DataTablesFactory;
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesCSVExporterInterface;
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesEditorInterface;
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface;

/**
 * Employee DataTables provider.
 */
class EmployeeDataTablesProvider implements DataTablesProviderInterface, DataTablesCSVExporterInterface, DataTablesEditorInterface {

    /**
     * {@inheritDoc}
     */
    public function editColumn(DataTablesColumnInterface $dtColumn, $entity, $value) {

        switch ($dtColumn->getData()) {

            case "age":
                $entity->setAge(intval($value));
                break;

            case "name":
                $entity->setName($value);
                break;

            case "office":
                $entity->setOffice($value);
                break;

            case "position":
                $entity->setPosition($value);
                break;

            case "salary":
                $entity->setSalary(intval($value));
                break;

            case "startDate":
                $entity->setStartDate(new DateTime($value));
                break;
        }
    }

    /**
     * {@inheritDoc}
     */
    public function exportColumns() {

        return [
            "#",
            "Name",
            "Position",
            "Office",
            "Age",
            "Start date",
            "Salary",
        ];
    }

    /**
     * {@inheritDoc}
     */
    public function exportRow($entity) {

        $output = [];

        $output[] = $entity->getId();
        $output[] = $entity->getName();
        $output[] = $entity->getPosition();
        $output[] = $entity->getOffice();
        $output[] = $entity->getAge();
        if (null !== $entity->getStartDate()) {
            $output[] = $entity->getStartDate()->format("Y-m-d");
        } else {
            $output[] = "";
        }
        $output[] = $entity->getSalary();

        return $output;
    }

    /**
     * {@inheritDoc}
     */
    public function getColumns() {

        $dtColumns = [];

        $dtColumns[] = DataTablesFactory::newColumn("name", "Name");
        $dtColumns[] = DataTablesFactory::newColumn("position", "Position");
        $dtColumns[] = DataTablesFactory::newColumn("office", "Office");
        $dtColumns[] = DataTablesFactory::newColumn("age", "Age");
        $dtColumns[] = DataTablesFactory::newColumn("startDate", "Start date");
        $dtColumns[] = DataTablesFactory::newColumn("salary", "Salary");
        $dtColumns[] = DataTablesFactory::newColumn("actions", "Actions")
            ->setOrderable(false)
            ->setSearchable(false);

        return $dtColumns;
    }

    /**
     * {@inheritDoc}
     */
    public function getCSVExporter() {
        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getEditor() {
        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getEntity() {
        return Employee::class;
    }

    /**
     * {@inheritDoc}
     */
    public function getMethod() {
        return "POST";
    }

    /**
     * {@inheritDoc}
     */
    public function getName() {
        return "employee";
    }

    /**
     * {@inheritDoc}
     */
    public function getOptions() {
        return null;

        // Custom options with the following code :
        // $dtOptions = DataTablesFactory::newOptions();
        // $dtOptions->addOption("language", ["url" => DataTablesWrapperHelper::getLanguageUrl("French")]);
        // $dtOptions->addOption("responsive", true);
        // $dtOptions->addOption("searchDelay", 1000);
        // return $dtOptions;
    }

    /**
     * {@inheritDoc}
     */
    public function getPrefix() {
        return "e";
    }

    /**
     * {@inheritDoc}
     */
    public function getView() {
        return null;

        // Custom template can be use with the following code :
        // return "@App/Employee/index.html.twig";
    }

    /**
     * {@inheritDoc}
     */
    public function renderColumn(DataTablesColumnInterface $dtColumn, $entity) {

        $output = null;

        switch ($dtColumn->getData()) {

            case "actions":
                $output = "";
                break;

            case "age":
                $output = $entity->getAge();
                break;

            case "name":
                $output = $entity->getName();
                break;

            case "office":
                $output = $entity->getOffice();
                break;

            case "position":
                $output = $entity->getPosition();
                break;

            case "salary":
                $output = $entity->getSalary();
                break;

            case "startDate":
                if (null !== $entity->getStartDate()) {
                    $output = $entity->getStartDate()->format("Y-m-d");
                }
                break;
        }

        return $output;
    }

    /**
     * {@inheritDoc}
     */
    public function renderRow($dtRow, $entity, $rowNumber) {

        $output = null;

        switch ($dtRow) {

            case DataTablesResponseInterface::DATATABLES_ROW_ATTR:
                break;

            case DataTablesResponseInterface::DATATABLES_ROW_CLASS:
                $output = (0 === $rowNumber % 2 ? "even" : "odd");
                break;

            case DataTablesResponseInterface::DATATABLES_ROW_DATA:
                $output = ["pkey" => $entity->getId()];
                break;

            case DataTablesResponseInterface::DATATABLES_ROW_ID:
                $output = "employee_" . $entity->getId();
                break;
        }

        return $output;
    }
}

Add this provider in the app/config/services.yml file of your project:

services:
    # ...
    app.datatables.provider.employee:
        class: AppBundle\Provider\EmployeeDataTablesProvider
        tags:
            - { name: "wbw.jquery.datatables.provider" }

You can add as many providers as you want as long as the name of the providers are different.

  1. Template (for custom rendering)

Create a template in the src/AppBundle/Resources/views/Employeedirectory of your project:

{# src/AppBundle/Resources/views/Employee/index.html.twig #}

{% block stylesheets %}
    {{ parent() }}
    {% include "@WBWCore/assets/_stylesheets.html.twig" %}
    {% include "@WBWBootstrap/assets/_stylesheets.html.twig" %}
    {% include "@WBWJQueryDataTables/assets/_stylesheets.html.twig" %}
{% endblock %}

{% block content %}
    {{ renderDataTables(dtWrapper) }}
{% endblock %}

{% block javascripts %}
    {{ parent() }}
    {% include "@WBWCore/assets/_javascripts.html.twig" %}
    {% include "@WBWBootstrap/assets/_javascripts.html.twig" %}
    {% include "@WBWJQueryDataTables/assets/_javascripts.html.twig" %}
    {{ jQueryDataTables(dtWrapper) }}
{% endblock %}
  1. Enjoy

Open you browser at http://localhost:8000/app_dev.php/datatables/employee/index.

  1. Join column

Create a new entity in the src/AppBundle/Entitydirectory of your project:

namespace AppBundle\Entity;

use DateTime;

/**
 * Office entity.
 */
class Office {

    /**
     * Id.
     *
     * @var integer
     */
    private $id;

    /**
     * Name.
     *
     * @var string
     */
    private $name;

    /**
     * Constructor.
     */
    public function __construct() {
        // NOTHING TO DO
    }

    /**
     * Get the id.
     *
     * @return integer Returns the id.
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Get the name.
     *
     * @return string Returns the name.
     */
    public function getName() {
        return $this->name;
    }

    /**
     * Set the name.
     *
     * @param string $name The name.
     * @return Office Returns this office.
     */
    public function setName($name) {
        $this->name = $name;
        return $this;
    }
}

Modify the entity in the src/AppBundle/Entity/Employee.php file of your project like this:

class Employee {

    // ...

    /**
     * Office.
     *
     * @var Office
     */
    private $office;

    // ...

    /**
     * Get the office.
     *
     * @return Office Returns the office.
     */
    public function getOffice() {
        return $this->office;
    }

    // ...

    /**
     * Set the office.
     *
     * @param Office $office The office.
     * @return Employee Returns this employee.
     */
    public function setOffice(Office $office) {
        $this->office = $office;
        return $this;
    }

    // ...

}

Modify the repository in the src/AppBundle/Repository/EmployeeRepository.php file of your project like this:

class EmployeeRepository extends DefaultDataTablesRepository {

    /**
     * {@inheritDoc}
     */
    public function dataTablesCountFiltered(DataTablesWrapperInterface $dtWrapper) {

        // Create a query builder.
        $qb = $this->buildDataTablesCountFiltered($dtWrapper);
        $qb->leftJoin("e.office", "o");

        // Return the result.
        return intval($qb->getQuery()->getSingleScalarResult());
    }

    /**
     * {@inheritDoc}
     */
    public function dataTablesCountTotal(DataTablesWrapperInterface $dtWrapper) {

        // Create a query builder.
        $qb = $this->buildDataTablesCountTotal($dtWrapper);
        $qb->leftJoin("e.office", "o");

        // Return the result.
        return intval($qb->getQuery()->getSingleScalarResult());
    }

    /**
     * {@inheritDoc}
     */
    public function dataTablesFindAll(DataTablesWrapperInterface $dtWrapper) {

        // Create a query builder.
        $qb = $this->buildDataTablesFindAll($dtWrapper);
        $qb->leftJoin("e.office", "o")
            ->addSelect("o");

        // Return the result.
        return $qb->getQuery()->getResult();
    }
}

Modify the provider in the src/AppBundle/Provider/EmployeeDataTablesProvider.php file of your project like this:

    /**
     * {@inheritDoc}
     */
    public function getColumns() {

        // Initialize the columns.
        $dtColumns = [];

        // ...

        $dtColumns[2]->getMapping()
            ->setColumn("name")
            ->setPrefix("o");

        // Returns the columns.
        return $dtColumns;
    }

    // ...

    /**
     * {@inheritDoc}
     */
    public function renderColumn(DataTablesColumnInterface $dtColumn, $entity) {

        // Initialize the output.
        $output = null;

        // Switch into column data.
        switch ($dtColumn->getData()) {

            // ...

            case "office":
                $output = $entity->getOffice()->getName();
                break;

            // ...

        }

        // Return the output.
        return $output;
    }