微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用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 举报,一经查实,本站将立刻删除。