webinarium / DataTablesBundle

Symfony bundle for DataTables plugin.
MIT License
16 stars 9 forks source link

Search by individual column value #4

Closed inrumi closed 6 years ago

inrumi commented 6 years ago

There is no documentation in the wiki about how to solve searching by column using select inputs, explained in the following example:

https://datatables.net/examples/api/multi_filter_select.html

But I already have solved it using the columns array that comes in DataTableQuery and using each column value in this way:

public function handle(DataTableQuery $request): DataTableResults
{
...
foreach ($request->columns as $column) {
           if ($column->data == 0 && $column->search->value) {
                $query
                    ->andWhere($query->expr()->like('u.id', '?0'))
                    ->setParameter(0, strtolower("%{$column->search->value}%"));
            } elseif ($column->data == 1 && $column->search->value) {
                $query
                    ->andWhere($query->expr()->like('LOWER(u.login)', '?1'))
                    ->setParameter(1, strtolower("%{$column->search->value}%"));
            } elseif ($column->data == 2 && $column->search->value) {
                $query
                    ->andWhere($query->expr()->like('LOWER(u.fullname)', '?2'))
                    ->setParameter(2, strtolower("%{$column->search->value}%"));
            }
 }
...
}

I don't know if this should be in your wiki? also if there's another more DRY way to do it, I will be really glad to use it.

webinarium commented 6 years ago

Thank you, I have updated the wiki.

The code you have provided is nearly the same I use in such cases. I don't know any more DRY way, but this is just to nature of DataTables request JSON-schema, there is nothing to do in this bundle. The bundle itself is just to map DT's requests and responses, and to validate them seamlessly.