omines / datatables-bundle

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

$value from $request->request->get can be passed before $datatableFactory #219

Closed souhaiebtar closed 1 year ago

souhaiebtar commented 3 years ago

my code is

$eventId = $request->request->get('eventId');
$table = $dataTableFactory->create()
    ->add('firstname', TextColumn::class, ['label' => 'Name'])
    ->add('lastname', TextColumn::class, ['label' => 'Capacity'])
    ->add('id', TextColumn::class, [
        'render' => function ($value, $context) {
            return sprintf('<a href="/admin/manageEvent/edit/%u" class="btn btn-success">Edit</a><a data-id="%u" data-toggle="modal" class="removeEvent btn btn-danger" data-target="#removeEvent">Delete</a>',
                $value, $value);
        },
        'label' => 'Options',
    ])
    ->createAdapter(ORMAdapter::class, [
        'entity' => User::class,
        'query' => function (QueryBuilder $builder) use ($eventId) {
            $builder
                ->select('u')
                ->from(User::class, 'u')
                ->where('u.id = :id')
                ->setParameter('id',$eventId);
        }

    ])
    ->handleRequest($request);

if ($table->isCallback()) {
    return $table->getResponse();
}

$event = $this->em->getRepository(Event::class)->find($eventId);

$eventId can be used in $event = $this->em->getRepository(Event::class)->find($eventId) and result is returned but inside anonymous function it's not passed, if i add $eventId = 2; before $table = $dataTableFactory->create(), it work just fine

but variable from $request->request->get( can't be used

i can't understand why? and is there an alternative?

curry684 commented 3 years ago

Did you ever fix this?

Doesn't seem like it has anything to do with the bundle, must be something at PHP level or a far fetched issue with http-foundation, but it's definitely interesting.

kapcus commented 3 years ago

i believe this is because of using GET request and query parameters, author probably did not mention the internal datatable POST request that is being invoked is not using these query parameters. I have same issue.

basic url: http://localhost:8081/user-management/ 1] there is form on a page for main filtering (to display users from group 1, 2, ..) - with every change of this form, page is refreshed by GET, e.g. when i select group 3, page is refreshed and url is http://localhost:8081/user-management/?x=3 2] below this form is DataTable which has a context of the form above - I need this table shows only users from selected group - this means I need the internal datatable POST request to be http://localhost:8081/user-management/?x=3 instead of http://localhost:8081/user-management/

is there any way to do this? This question has been raised multiple times here but never answered.

Thx

davidromani commented 2 years ago

is there any way to do this? This question has been raised multiple times here but never answered.

I have same issue, anybody found an answer?

github-actions[bot] commented 1 year ago

Stale issue message

aniskasmi commented 1 year ago

Hi anyone have information about this feature ? Or fix ?

aniskasmi commented 1 year ago

@souhaiebtar @kapcus ?

jarodxxx commented 1 year ago

I have same issue, anybody found an solution?

curry684 commented 1 year ago

I think the core issue here is a misunderstanding about how the bundle works, which is entirely up to how DataTables works.

The code to create the adapter is executed (at least) TWICE for every table pageview. Once during the page generation, where the adapters are not even invoked but the definition of the table is used to inject into the page. Then after the page is loaded DataTables performs a callback to the exact same URL (and any time you change page, filters, sorting etc.). The if ($table->isCallback()) line differentiates between these 2 types of requests.

Now this means that if you open the URL /users/list?name=foo on your site, the name parameter is present during the initial rendering only. DataTables nor our scripts magically include the same parameters in subsequent callbacks. You have to provide some custom code to inject data in the callback. This is done through the preXhr.dt event and is completely handled by DataTables itself. This bundle has no 'opinions' on such matters and just streamlines the integration, so there's nothing broken on this end. It's just not facilitated out of the box, nor was it ever intended to be, as there is no generic-fix-all solution that will fit every use case. For example if you have custom filters on a table page you'll want to serialize the form and inject it, a wholly different bit of code.

curry684 commented 1 year ago

I'm closing this per the explanation above.