omines / datatables-bundle

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

Can't search by datetime field #64

Closed rush4u closed 4 years ago

rush4u commented 5 years ago

This is the code of my action:

public function index(Request $request, CarRepository $carRepository, DataTableFactory $dataTableFactory)
    {
        $table = $dataTableFactory->create()
            ->add('id', NumberColumn::class, ["orderable" => true, "searchable" => true])
            ->add('carBrand', TextColumn::class, ["field" => "brand.name", "orderable" => true, "label" => "Marca del Carro", "searchable" => true])
            ->add('model', TextColumn::class, ["field" => "car.model", "orderable" => true, "label" => "Modelo del Carro", "searchable" => true])
            ->add("filledAt", DateTimeColumn::class, ["format" => "Y-m-d H:i:s", "searchable" => true, "orderable" => true])
            ->add("fuelFilled", NumberColumn::class, ["render" => "%s L", "searchable" => true])
            ->add("price", NumberColumn::class, ["render" => "%s cuc", "searchable" => true])
            ->add("formerRead", NumberColumn::class, ["searchable" => true])
            ->add("currentRead", NumberColumn::class, ["searchable" => true])
            ->add("mileage", NumberColumn::class, ["searchable" => true])
            ->add('actions', TwigColumn::class, [
                'className' => 'buttons',
                'template' => 'frontend/supervisor/carfill/table-buttons.html.twig',
            ])
            ->addOrderBy('filledAt', DataTable::SORT_DESCENDING)
            ->createAdapter(ORMAdapter::class, [
                "entity" => CarFuelFillRecord::class,
                "query" => function(QueryBuilder $builder){
                    $builder
                        ->from("App:CarFuelFillRecord", "cffr")
                        ->select("cffr, car, brand")
                        ->leftJoin("cffr.car", "car")
                        ->leftJoin("car.brand", "brand")
                    ;
                }
            ])
            ->handleRequest($request);

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

        return $this->render('frontend/supervisor/carfill/index.html.twig', [
            'datatable' => $table,
            "cars" => $carRepository->findAll()
        ]);
    }

This is the code of my template $(function() { $('#carFuelFillRecords').initDataTables({{ datatable_settings(datatable) }},{searching: true}); }); The datatable renders well, and it let me search by all the fields except by the only DateTimeColumn field.

What am i doing wrong? Thanks a lot in advance

curry684 commented 5 years ago

I suspect we may not have tested that scenario all too well and you might just be stumbling into a real bug there 😄

Alternatively we may just have consciously not implemented it as a "simple" search on DateTime does not make much sense, it would need to support ranges. To select those you'll likely need to integrate with third party date pickers. It would likely be easier to implement a customized app specific filter than try to solve this inside the bundle.

rush4u commented 5 years ago

Hey man, what do you recommend me to do in this situation? This bundle solves many problems to me, but the datetime search is very important for my use case. Any alternatives?

shades684 commented 4 years ago

We didn't implement this on purpose. What does is mean when you're searching for 19. Do you want all years that contain 19 or all years that start with 19 or both. That is why you need to adapt your own query, use the 2nd parameter of the query parameter like this:

                "query" => function(QueryBuilder $builder, DataTableState $state){
                    $search = $state->getGlobalSearch();
                    $builder
                        ->from("App:CarFuelFillRecord", "cffr")
                        ->select("cffr, car, brand")
                        ->leftJoin("cffr.car", "car")
                        ->leftJoin("car.brand", "brand")
                    ;
                },

and adapt your query