Yajra数据表结合使用搜索和自定义过滤器 [英] Yajra data table using search and custom filter together

查看:59
本文介绍了Yajra数据表结合使用搜索和自定义过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel: 5.6.39

PHP: 7.2.10

Yajra Table: 8.0

示例代码

$(document).ready( function() {
        var url = "{{ url('/admin/videos') }}";
        $(function() {
            var oTable = $('#admin-videos').DataTable({
                dom: "<'row'<'col-xs-12'<'col-xs-6'l><'col-xs-6'p>>r>"+
            "<'row'<'col-xs-12't>>"+
            "<'row'<'col-xs-12'<'col-xs-6'i><'col-xs-6'p>>>",
                processing: true,
                serverSide: true,
                ajax: {
                    url: url,
                    data: function (d) {
                        d.category = $("#category option:selected").val();
                        d.language = $("#language option:selected").val();
                    }
                },            
                columns: [
                    { data: 'video_checkbox', name: 'video_checkbox' },
                    { data: 'created_at', name: 'created_at' },
                    { data: 'video_label', name: 'video_label' },
                    { data: 'video_link', name: 'video_link' },
                    { data: 'view', name: 'view' },
                    { data: 'video_category', name: 'video_category' },
                ],
                stateSave: true,
                bDestroy: true,
          });

          $('#search-form').on('submit', function(e) {
               oTable.draw();
               e.preventDefault();
            });
        });
   });

现在,如果我将删除dom搜索和过滤器,都可以看到,但搜索仍然无法进行,如果我将删除过滤器,则仅搜索有效,我认为应该对dom或某些内容进行一些自定义将同时允许搜索和过滤器.

Now if I will remove dom search and filter both can be seen but search will still not work, if I will remove the filters then only search is working, I believe there should be some customization to dom or something, that will allow both search and filters.

文档中,也没有搜索.

有一个选项可以像下面的代码那样进行搜索,但是似乎可行,也因为我在上面的代码中删除了dom属性.

There is one option to enable search like below code, but it does not seems to be working, also for that I have removed dom attribute in above code.

search: {
        "regex": true
    }

推荐答案

我不知道搜索是如何自动进行的,但是我已经为我的要求进行了编码,并且可以正常工作,并给出了一些想法,您可以怎么做.

I do not know how the search works automatically, but I have coded for my requirement and that is working fine and give some idea, how you can do it.

您可以使用$request->get('search')['value']获取搜索词,现在编写代码以检查您的条件是否得到满足,并在控制器或模型中过滤结果.下面是在Controller中执行此操作的代码.

You can get search term using $request->get('search')['value'] and now write code to check if your condition is getting satisfied and filter the results in your controller or Model. Below is the code to do it in Controller.

return DataTables::eloquent($videos)
        ->filter(function ($query) use ($request) {
            if ($request->has('category') && ! is_null($request->get('category'))) {
                $query->where('video_category', $request->get('category'));
            }

            if ($request->has('language') && ! is_null($request->get('language')) ) {
                $query->where('video_language', $request->get('language'));
            }

            if ($request->has('search') && ! is_null($request->get('search')['value']) ) {
                $regex = $request->get('search')['value'];
                return $query->where('your_field', 'like', '%' . $regex . '%');
                });
            }
        })->toJson();

这篇关于Yajra数据表结合使用搜索和自定义过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆