Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Datatablesbundle Laravel Package

azraelir/datatablesbundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require azraelir/datatablesbundle
    

    Enable the bundle in config/bundles.php:

    return [
        // ...
        Azraelir\DatatablesBundle\AzraelirDatatablesBundle::class => ['all' => true],
    ];
    
  2. Basic Configuration: Add to config/packages/azraelir_datatables.yaml:

    azraelir_datatables:
        entities:
            App\Entity\YourEntity: ~
    
  3. First Use Case: Create a controller method to handle DataTables requests:

    use Azraelir\DatatablesBundle\Annotation\DataTables;
    use Azraelir\DatatablesBundle\Annotation\DataColumn;
    
    /**
     * @DataTables(
     *     entity="App\Entity\YourEntity",
     *     columns={
     *         @DataColumn(property="id", title="ID"),
     *         @DataColumn(property="name", title="Name")
     *     }
     * )
     */
    public function yourEntityAction(Request $request)
    {
        return $this->get('azraelir_datatables.datatable')->handleRequest($request);
    }
    
  4. Template Integration: Include DataTables CSS/JS in your Twig template:

    {{ azraelir_datatables_js() }}
    {{ azraelir_datatables_css() }}
    

    Render the table:

    {{ azraelir_datatables_table({
        'entity': 'App\Entity\YourEntity',
        'columns': [
            { 'property': 'id', 'title': 'ID' },
            { 'property': 'name', 'title': 'Name' }
        ]
    }) }}
    

Implementation Patterns

Common Workflows

  1. Entity-Based Tables: Use annotations or YAML config to define tables for Doctrine entities:

    # config/packages/azraelir_datatables.yaml
    azraelir_datatables:
        entities:
            App\Entity\User:
                columns:
                    - { property: 'username', title: 'Username' }
                    - { property: 'email', title: 'Email' }
                    - { property: 'roles', title: 'Roles' }
    
  2. Custom Query Handling: Override the default query builder for complex logic:

    use Azraelir\DatatablesBundle\Event\DataTablesEvent;
    
    public function onBuildQuery(DataTablesEvent $event)
    {
        $event->getQueryBuilder()
            ->andWhere('u.active = :active')
            ->setParameter('active', true);
    }
    

    Register the listener in a service:

    services:
        App\EventListener\CustomDataTablesListener:
            tags:
                - { name: kernel.event_listener, event: azraelir.datatables.build_query, method: onBuildQuery }
    
  3. Dynamic Columns: Use Twig to render dynamic columns:

    {% for column in columns %}
        {{ azraelir_datatables_column({
            'property': column.property,
            'title': column.title,
            'render': 'App\\Controller\\YourController::renderCustomColumn'
        }) }}
    {% endfor %}
    
  4. Server-Side Processing: Enable server-side processing in config:

    azraelir_datatables:
        server_side_processing: true
    
  5. Integration with Forms: Use DataTables with Symfony Forms for inline editing:

    {{ form_row(form.name, {
        'attr': {
            'data-dt-column': 'name',
            'class': 'editable'
        }
    }) }}
    

Integration Tips

  1. Routing: Use friendsofsymfony/jsrouting-bundle for AJAX routes:

    # config/routes.yaml
    azraelir_datatables:
        resource: "@AzraelirDatatablesBundle/Resources/config/routing.yml"
        prefix: "/datatables"
    
  2. Asset Management: Override default assets in config/packages/azraelir_datatables.yaml:

    azraelir_datatables:
        assets:
            css: ['/bundles/azraelirdatatables/css/custom.css']
            js: ['/bundles/azraelirdatatables/js/custom.js']
    
  3. Localization: Configure translations for column titles:

    # config/packages/azraelir_datatables.yaml
    azraelir_datatables:
        translations:
            App\Entity\User:
                username: 'utilisateur'
                email: 'e-mail'
    
  4. Pagination: Customize pagination settings:

    $datatable->setOptions([
        'paging' => true,
        'pageLength' => 25,
        'lengthMenu' => [[10, 25, 50, -1], [10, 25, 50, "All"]],
    ]);
    

Gotchas and Tips

Pitfalls

  1. PostgreSQL Limitation: The bundle is not tested or supported for PostgreSQL. Use MySQL or SQLite for production.

  2. Annotation Caching: Clear cache after adding new annotations:

    php bin/console cache:clear
    
  3. Case-Sensitive Properties: Ensure property names in annotations match Doctrine entity properties exactly (including case).

  4. Lazy-Loaded Associations: Eager-load associations to avoid NULL values in columns:

    $event->getQueryBuilder()
        ->leftJoin('u.roles', 'r')
        ->addSelect('r');
    
  5. Memory Issues: Large datasets may cause memory exhaustion. Use server-side processing (server_side_processing: true) and pagination.


Debugging

  1. Query Logging: Enable Doctrine query logging to debug SQL:

    # config/packages/dev/doctrine.yaml
    doctrine:
        dbal:
            logging: true
            profiling: true
    
  2. Request Dumping: Dump DataTables request data in development:

    public function yourEntityAction(Request $request)
    {
        dump($request->request->all());
        // ...
    }
    
  3. Event Listener Debugging: Log events to trace execution:

    public function onBuildQuery(DataTablesEvent $event)
    {
        \Log::debug('Query built:', [
            'query' => $event->getQueryBuilder()->getSQL(),
            'params' => $event->getQueryBuilder()->getParameters()
        ]);
    }
    

Tips

  1. Reusable Config: Extract entity configurations to a separate YAML file and import them:

    # config/packages/azraelir_datatables.yaml
    imports:
        - { resource: ../datatables/config.yaml }
    
  2. Custom Rendering: Use Twig functions for custom cell rendering:

    {{ azraelir_datatables_column({
        'property': 'status',
        'render': 'App\\Twig\\Extension\\DataTablesExtension::renderStatus'
    }) }}
    
  3. Performance: Add indexes to frequently filtered/sorted columns in your database.

  4. Testing: Mock the DataTables service in PHPUnit:

    $this->container->set('azraelir_datatables.datatable', $this->createMock(DataTables::class));
    
  5. Extensions: Extend the bundle by creating custom column types or event subscribers. Example:

    use Azraelir\DatatablesBundle\Column\ColumnInterface;
    
    class CustomColumn implements ColumnInterface
    {
        public function render($value, array $rowData)
        {
            return '<span class="label label-' . $value . '">' . $value . '</span>';
        }
    }
    

    Register it in services:

    services:
        App\Column\CustomColumn:
            tags:
                - { name: azraelir_datatables.column_type, type: 'custom' }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware