omines / datatables-bundle

DataTables bundle for Symfony
https://omines.github.io/datatables-bundle/
MIT License
258 stars 115 forks source link

Can't use two datatables in same route #96

Closed laurentk6 closed 5 years ago

laurentk6 commented 5 years ago

Hello,

I'm trying to create two datatables with different data on the same route, but both tables are exactly the same (with the same data). Is there a function or trick to display two datatables with two different entities ?

Exemple :

` /**

and datatabletypeInterface :

`class ContactTableType implements DataTableTypeInterface {

public function configure(DataTable $dataTable, array $options){
    $dataTable
        ->add('firstname', TextColumn::class, ['label' => 'firstname'])
        ->add('lastname', TextColumn::class, ['label' => 'lastname'])
        ->add('id', TextColumn::class, ['label' => 'Action', 'render' => function($value, $context){
            return sprintf('<a href="/contact/%s/edit" class="btn btn-success">Modifier le contact</a>', $value, $value);
        }])
        ->createAdapter(ORMAdapter::class, [
            'entity' => Contact::class,
        ]);
}

}`

`class AddressTableType implements DataTableTypeInterface {

public function configure(DataTable $dataTable, array $options){
    $dataTable
        ->add('name', TextColumn::class, ['label' => 'Nom'])
        ->add('address', TextColumn::class, ['label' => 'Adresse'])
        ->add('id', TextColumn::class, ['label' => 'Action', 'render' => function($value, $context){
            return sprintf('<a href="/societe/%s/adresse/%s/edit" class="btn btn-success">Modifier l\'adresse</a>', $value, $value);
        }])
        ->createAdapter(ORMAdapter::class, [
            'entity' => Address::class,
        ]);
}

}`

Thanks for help

MaximePinot commented 5 years ago

Hi,

You have to set a unique name per DataTable.

Check this file for a complete example.

The important line is: https://github.com/omines/datatables-bundle/blob/47261b3170804af8506eafc8ba40920141f121a9/tests/Fixtures/AppBundle/Controller/HomeController.php#L40

laurentk6 commented 5 years ago

Thank you very much ! Works great !