Individual column search in table with Symfony and Omines bundle

There is a question about searching by columns in a table. I use Symfony 4 and Omines bundle.

I want to do a individual column search, but it doesn’t work. More precisely, it works on the full occurrence of the string. For example, there is a value Donald Trump and a search for "donald trump" will find it. But I would like the search to work on substring as well: ex. "nald".

In this case, the request in the url is formed, but on the server side it does not find matches by the substring. Global search (for the whole table, it add in DOM) works fine. Searches both by full match and by substring.

Do I need to do server side request processing? Or is there a ready-made column search solution in Omines?

Code:

public function list(Request $request, DataTableFactory $dataTableFactory)
    {
        $table = $dataTableFactory->create()
            ->add('firstName', TextColumn::class, ['label' => 'Имя', 'field' => 'c.firstName', 'searchable' => true])
            ->add('phone', TextColumn::class, ['label' => 'Телефон', 'field' => 'c.phone', 'searchable' => true])
            ->add('email', TextColumn::class, ['label' => 'E-mail', 'field' => 'c.email', 'searchable' => true])
            ->add('total', NumberColumn::class, ['label' => 'Сумма заказов', 'field' => 'total.ordersTotal', 'searchable' => false])
            ->add('accessCount', NumberColumn::class, ['label' => 'Кол-во визитов', 'field' => 'c.accessCount', 'searchable' => true])
            ->add('firstAccess', DateTimeColumn::class, ['label' => 'Первый визит', 'format' => 'd-m-Y h:i', 'field' => 'c.firstAccess', 'searchable' => true])
            ->add('lastAccess', DateTimeColumn::class, ['label' => 'Последний визит', 'format' => 'd-m-Y h:i', 'field' => 'c.lastAccess', 'searchable' => true])
            ->createAdapter(ORMAdapter::class, [
                'hydrate' => Query::HYDRATE_ARRAY,
                'entity' => Customer::class,
                'query' => function (QueryBuilder $builder) {
                    $builder
                        ->distinct()
                        ->select('c.firstName, c.phone, c.email, c.accessCount, c.firstAccess, c.lastAccess, SUM(o.total) AS ordersTotal')
                        ->from(Customer::class, 'c')
                        ->join('c.orders', 'o')
                        ->groupBy('c.id')
                        ->getQuery()
                        ->getResult()
                    ;
                },
            ])
            ->handleRequest($request);

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

        return $this->render('admin/customer/index.html.twig', [
            'customers' => $table,
        ]);
    }

JS:

<script>
        $(function () {
            $('#customersTable').initDataTables({{ datatable_settings(customers) }}, {
                searching: true,

                initComplete: function () {
                    this.api().columns().every(function () {
                        let that = this;

                        $('input', this.footer()).on('keyup change clear', function () {
                            if (that.search() !== this.value) {
                                that
                                    .search(this.value)
                                    .draw();
                            }
                        });

                        $('option.toggle-vis').on('click', function (e) {
                            e.preventDefault();
                            let column = that.column($(this).attr('data-column'));
                            column.visible(!column.visible());
                        });

                    });

                    let r = $('tfoot tr');
                    r.find('th').each(function () {
                        $(this).css('padding', 8);
                    });
                    $('thead').append(r);
                    $('#search_0').css('text-align', 'center');
                },

            }).then(function (dt) {

                $('tfoot th').each(function () {
                    let title = $(this).text();
                    $(this).html('<input type="search" class="form-control form-control-sm" placeholder="Поиск по полю ' + title + '" />');
                })

            });
        });
    </script>

Visual:

Global search

Individual column search

Individual column search with full value

I would be grateful for your help!

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *