使用Symfony和Omines捆绑包在表中进行单个列搜索

问题描述

一个关于按表中的列进行搜索的问题。我使用Symfony 4和Omines捆绑包。

我想进行单个列搜索,但是它不起作用。更准确地说,它适用于整个字符串。例如,唐纳德·特朗普(Donald Trump)有一个值,搜索“唐纳德·特朗普(donald trump)”将找到它。但我也希望该搜索也可用于子字符串:例如。 “ nald”。

在这种情况下,URL中的请求已形成,但是在服务器端,子字符串找不到匹配项。全局搜索(对于整个表,它添加到DOM中)效果很好。通过完全匹配和子字符串进行搜索

我需要进行服务器端请求处理吗?还是Omines中有现成的列搜索解决方案?

代码

public function list(Request $request,DataTableFactory $dataTableFactory)
    {
        $table = $dataTableFactory->create()
            ->add('firstName',TextColumn::class,['label' => 'Имя','field' => 'c.firstName','searchable' => true])
            ->add('phone',['label' => 'Телефон','field' => 'c.phone','searchable' => true])
            ->add('email',['label' => 'E-mail','field' => 'c.email','searchable' => true])
            ->add('total',NumberColumn::class,['label' => 'Сумма заказов','field' => 'total.ordersTotal','searchable' => false])
            ->add('accessCount',['label' => 'Кол-во визитов','field' => 'c.accessCount','searchable' => true])
            ->add('firstAccess',DateTimeColumn::class,['label' => 'Первый визит','format' => 'd-m-Y h:i','field' => 'c.firstAccess','searchable' => true])
            ->add('lastAccess',['label' => 'Последний визит','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>

视觉:

Global search

Individual column search

Individual column search with full value

感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)