Weave 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

Data Tables Bundle Laravel Package

brown298/data-tables-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require brown298/data-tables-bundle
    

    Add to config/bundles.php:

    Brown298\DataTablesBundle\Brown298DataTablesBundle::class => ['all' => true],
    
  2. Basic Configuration Update config/packages/brown298_data_tables.yaml (if needed) or use defaults.

  3. First Use Case: Displaying a Table In a controller:

    use Brown298\DataTablesBundle\DataTables;
    use Brown298\DataTablesBundle\DataTablesFactory;
    
    public function indexAction(DataTables $datatables, DataTablesFactory $factory)
    {
        $builder = $factory->createBuilder($this->getDoctrine()->getManager());
        $builder->add('entity', 'App\Entity\YourEntity');
    
        $data = $datatables->serverSide($builder);
        return $this->render('your_template.html.twig', [
            'data' => $data,
        ]);
    }
    
  4. Twig Template

    {{ include('Brown298DataTablesBundle::table.html.twig', {
        data: data,
        columns: [
            { data: 'id', name: 'ID' },
            { data: 'name', name: 'Name' },
        ]
    }) }}
    

Implementation Patterns

Common Workflows

1. Server-Side Processing

$builder = $factory->createBuilder($em);
$builder
    ->add('entity', 'App\Entity\Product')
    ->addColumn('name', 'Name')
    ->addColumn('price', 'Price', 'currency')
    ->addFilter('name', 'Name')
    ->addOrder('createdAt', 'Created At', 'desc');

$data = $datatables->serverSide($builder);

2. Custom Query Logic

Extend the builder for complex queries:

$builder->add('entity', 'App\Entity\Order')
    ->addColumn('total', 'Total', function($order) {
        return $order->getTotal();
    })
    ->addCallback('customFilter', function($query, $input) {
        $query->andWhere('o.status = :status')
            ->setParameter('status', $input);
    });

3. Integration with Forms

Use DataTables with Symfony Forms for dynamic filtering:

{{ form_start(form) }}
    {{ form_widget(form.search) }}
    {{ include('Brown298DataTablesBundle::table.html.twig', {
        data: data,
        form: form,
    }) }}
{{ form_end(form) }}

4. AJAX Rendering

Return JSON for SPA-like behavior:

return $datatables->ajax($builder);

Integration Tips

Doctrine ORM

  • Use add() with entity names for automatic column detection.
  • For DQL, use addQuery():
    $builder->addQuery('SELECT p, c FROM App\Entity\Product p JOIN p.category c');
    

Custom Repositories

Inject a custom repository into the builder:

$builder->setRepository('App\Entity\Product', $customRepository);

Twig Extensions

Use {{ datatables() }} helper for quick table generation:

{{ datatables({
    'entity': 'App\Entity\User',
    'columns': [{ data: 'email', name: 'Email' }]
}) }}

Gotchas and Tips

Pitfalls

  1. Column Mismatch

    • Ensure data attributes in Twig match the actual entity properties or custom column names.
    • Debug with dd($builder->getColumns()) to verify column definitions.
  2. Case Sensitivity

    • Filters are case-sensitive by default. Use addFilter('name', 'Name', 'like') for case-insensitive searches.
  3. Lazy-Loading Associations

    • Avoid N+1 queries by using addColumn() with join:
      $builder->addColumn('category.name', 'Category', 'join');
      
  4. Pagination Conflicts

    • Disable default pagination if using KnpPaginator:
      brown298_data_tables:
          server_side: true
          pagination: false
      

Debugging

  • Query Logging Enable Doctrine logging to inspect generated queries:

    $em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
    
  • Builder Inspection Dump the builder state before server-side processing:

    dd($builder->getQuery(), $builder->getColumns());
    

Extension Points

  1. Custom Column Types Extend Brown298\DataTablesBundle\Column\ColumnInterface for custom rendering:

    class CustomColumn extends AbstractColumn
    {
        public function render($value, $row)
        {
            return '<span class="badge">' . $value . '</span>';
        }
    }
    
  2. Event Listeners Subscribe to datatables.build.query or datatables.render.cell events:

    services:
        App\EventListener\DataTablesListener:
            tags:
                - { name: kernel.event_listener, event: datatables.build.query, method: onBuildQuery }
    
  3. Override Templates Copy templates/table.html.twig to templates/Brown298DataTablesBundle/table.html.twig for custom styling.

Performance Tips

  • Selective Loading Use addColumn() with select to limit loaded fields:

    $builder->addColumn('name', 'Name', null, ['select' => 'p.name']);
    
  • Caching Cache server-side responses for static data:

    $data = $datatables->serverSide($builder);
    $this->get('cache')->save('datatables_data', $data, 3600);
    
  • Batch Processing For large datasets, implement cursor-based pagination:

    $builder->addCallback('customPagination', function($query, $limit, $offset) {
        $query->setFirstResult($offset)->setMaxResults($limit);
    });
    
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.
dakiquang/laravel-oauth-refresh-lock
woda/laravel-worktrees
dalefranks/ramasite
faghy/laracashbook
roundly-consulting/google-places-for-laravel
roundly-consulting/kubernetes-api-for-laravel
roundly-consulting/plausible-for-laravel
gvp/gvp-laravel-template
metasyncsite/laravel-metasync-client
roundly-consulting/onboarding-for-laravel
andydefer/laravel-notification
adachsoft/cli-process-tester
jhaskell13/laraflow
sebastienheyd/boilerplate-media-manager
ccast/tagixo-primix
pretendtrue/laravel-builder
sawmainek/laravel-scout-tntsearch-driver
tomshaw/laravel-calendar-table
dibakar/laravel-ownership
adachsoft/changelog-linter