PHP code example of jul6art / datatable-bundle

1. Go to this page and download the library: Download jul6art/datatable-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jul6art / datatable-bundle example snippets


// config/bundles.php — Flex does this for you
Jul6Art\DatatableBundle\DatatableBundle::class => ['all' => true],

final class UserDataTableConfigProvider extends AbstractDataTableConfigProvider
{
    public function getColumns(): array
    {
        return [
            $this->column('id', 'datatable.col.id', responsivePriority: 10),
            $this->column('fullName', 'user.field.name', 'user', render: 'userNameWithAvatar', responsivePriority: 1),
            $this->column('email', 'user.field.email', 'user', responsivePriority: 2),
            $this->readOnlyColumn('isActive', 'user.field.status', 'user', render: 'statusBadge'),
        ];
    }

    public function getFilters(): array
    {
        return [
            $this->staticFilter('isActive', 'isActive', 'user.field.status', [
                ['value' => 'true',  'label' => $this->t('datatable.status.active')],
                ['value' => 'false', 'label' => $this->t('datatable.status.inactive')],
            ], 'user'),
            $this->dateRangeFilter('createdAt', 'createdAt', 'user.filter.created', 'user', granularity: 'datetime'),
            $this->apiFilter('team', 'team', 'user.filter.team', '/api/teams'),
        ];
    }

    public function getActions(): array
    {
        return [
            $this->linkAction('show', '/admin/users/{id}', 'eye', 'action.show'),
            $this->bulkDeleteAction('/admin/users/{id}/delete', '/admin/users/bulk-delete'),
        ];
    }
}

#[IsGranted(PermissionCodes::USER_DELETE)]
#[Route('/admin/users/{id}/delete', methods: ['POST'])]
public function delete(User $user, Request $request): Response
{
    if (!$this->isCsrfTokenValid('datatable_action', (string) $request->request->get('_token'))) {
        // …
    }
}

$columns = $admin->decorateColumns($provider->getColumns());
$filters = [...$provider->getFilters(), $admin->tenantFilter()];

use Jul6Art\DatatableBundle\Preference\DatatablePreferenceStoreInterface;
use Symfony\Component\Security\Core\User\UserInterface;

final readonly class DatatablePreferenceStore implements DatatablePreferenceStoreInterface
{
    public function read(UserInterface $user, string $key): ?string { /* SELECT … */ }
    public function write(UserInterface $user, string $key, string $json): void { /* UPSERT … */ }
    public function delete(UserInterface $user, string $key): void { /* DELETE … */ }
}

$this->readOnlyColumn('parent', 'erp.product.fields.parent', 'erp', render: 'iri',
    extra: ['resolveField' => 'name'], responsivePriority: 10, hidden: true),

#[Route('/admin/users/export', name: 'admin_user_export')]
public function export(Request $request, DatatableViewExporter $exporter): StreamedResponse
{
    return $exporter->stream(
        new UserDataTableConfigProvider($this->translator),
        'admin_user',
        $this->getUser(),
        $request,
        new CsvWriter(CsvDialect::excelFr()),
        'users',
    );
}

final class UserDataTableConfigProvider extends AbstractDataTableConfigProvider
{
    public function rootEntity(): ?string
    {
        return User::class;
    }

    // getColumns(), getFilters() — unchanged
}

$this->readOnlyColumn('tags', 'crm.deal.fields.tags', 'crm', render: 'chipList', extra: ['reportable' => false]),

public function rootEntity(): ?string
{
    return Project::class;
}

public function rowReorder(): ?RowReorder
{
    // the key in the URL, the integer position field, the permission that guards the write
    return new RowReorder('projects', 'position', 'project:reorder');
}

// the displayed key, by default
$this->apiFilter('site', 'site', 'asset.filter.site', '/api/sites', searchKey: 'search');
// another key — the label is computed, the sort runs on a real column
$this->apiFilter('asset', 'asset', 'defect.filter.asset', '/api/assets', textKey: 'label', orderKey: 'serialNumber');
// no order: the server's order IS the meaning (a rank, a priority)
$this->apiFilter('level', 'level', 'x.filter.level', '/api/levels', orderKey: 'none');

$this->staticFilter('status', 'status', 'work_order.field.status', $options, 'work_order', tabs: true),

$this->previewAction('show', '/admin/work-orders/{id}', 'action.show'),   // icon: 'eye' by default
yaml
# config/routes/datatable.yaml
datatable_preferences:
    resource: '@DatatableBundle/Controller/DatatablePreferenceController.php'
    type: attribute
    prefix: /datatable/preferences
yaml
# config/routes/datatable.yaml
datatable_reorder:
    resource: '@DatatableBundle/Controller/DatatableReorderController.php'
    type: attribute
    prefix: /admin/datatable
yaml
# translations/javascript.<locale>.yaml
datatable:
    dt:
        processing: 'Traitement…'
        search: 'Rechercher :'
        length_menu: 'Afficher _MENU_ éléments'
        info: 'Affichage de _START_ à _END_ sur _TOTAL_ éléments'
        info_empty: 'Affichage de 0 à 0 sur 0 élément'
        info_filtered: '(filtré de _MAX_ éléments au total)'
        loading: 'Chargement…'
        zero_records: 'Aucun élément trouvé'
        empty_table: 'Aucune donnée disponible'
        aria:
            sort_ascending: ': activer pour trier la colonne par ordre croissant'
            sort_descending: ': activer pour trier la colonne par ordre décroissant'