【问题标题】:Laravel Datatable default search is not working with custom filtersLaravel Datatable 默认搜索不适用于自定义过滤器
【发布时间】:2020-12-01 13:11:26
【问题描述】:

我在 laravel 中使用 yajra 数据表。我已经实现了该表。除了搜索之外它工作正常。我也有一个自定义过滤器。 下面的代码显示了 Datatable 初始化和 ajax 请求。用户类型过滤器工作正常。

<script>
    $(document).ready(function() {
            var table = $('#dataList').DataTable({
                processing: true,
                serverSide: true,
                ajax: {
                    url: "{{ route('users.user.index') }}",
                    data: function (d) {
                        d.user_type = $('#user_type_filter').val();
                    }
                },
                columns: [
                    {data: 'DT_RowIndex', name: 'DT_RowIndex'},
                    {data: 'name', name: 'name'},
                    {data: 'email', name: 'email'},
                    {data: 'phone', name: 'phone'},
                    {data: 'action', name: 'action', orderable: false, searchable: false},
                ]
            });
        $('#search-form').on('submit', function(e) {
            table.draw();
            e.preventDefault();
        });            
    } );
</script>

后端索引页面过滤用户类型

public function index(Request $request)
{
    /**
     * Ajax call by datatable for listing of the users.
     */
    if ($request->ajax()) {
        $data = User::with('userType')->get();
        $datatable =  DataTables::of($data)
            ->filter(function ($instance) use ($request) {                    
                if ($request->has('user_type') && $request->get('user_type')) {
                    $instance->collection = $instance->collection->filter(function ($row) use ($request) {
                        //return Str::contains($row['phone'], $request->get('phone')) ? true : false;
                        return $row['user_type_id'] == $request->get('user_type');
                    });
                }  
               if ($request->input('search.value') != "") {
                     $instance->collection = $instance->collection->filter(function ($row) use ($request) {
                        return Str::contains($row['name'], $request->input('search.value')) ? true : false;
                    });
                }                                      
            })             
            ->addIndexColumn()
            ->addColumn('action', function ($user) {
                return view('users.datatable', compact('user'));
            })
            ->rawColumns(['action'])
            ->make(true);
        return $datatable;
    }

    $user_type = UserType::pluck('user_type','id')->all();
    $users = User::with('userType')->paginate(25);

    return view('users.index', compact('users','user_type'));
}

此代码仅在当前页面上生成搜索。

【问题讨论】:

    标签: laravel yajra-datatable


    【解决方案1】:

    你应该这样尝试

       filterColumn('name', function($query, $keyword) {
                        $sql = "name like ?";
                        $query->whereRaw($sql, ["%{$keyword}%"]);
                    })
    

    参考这个链接 https://datatables.yajrabox.com/fluent/custom-filter

    【讨论】:

    • 我指的是那个。创建自定义过滤器。创建过滤器后搜索不起作用
    • 你能详细说明一下@BLPraveen
    • 我使用链接datatables.yajrabox.com/fluent/custom-filter创建了客户过滤器
    • user_type 不是数据表中的列,您应该如何过滤它们
    • user_type 是一个过滤器,它可能不是一个列。它是带有 user_type 列表的选择框
    【解决方案2】:

    我已通过更改为以下代码解决了关系问题

    if ($request->input('search.value') != "") {
         $instance->collection = $instance->collection->filter(function ($row) use ($request) {
            return Str::contains($row['user']['name'], $request->input('search.value')) ? true : false;
        });
    } 
    

    我想通过更改$row['user']['name'] 来访问关系用户及其名称

    【讨论】:

      【解决方案3】:

      BL Praveen 给出的答案几乎是清晰的,但是如果您希望搜索完整的数据表而不是仅搜索一列怎么办。 以下是我的解决方案:

      将以下代码添加到您预先添加的自定义过滤器中:

      if ($request->input('search.value') != "") {
      if((!Str::contains(strtolower($row['name']), strtolower($request->input('search.value')))) && 
          (!Str::contains(strtolower($row['id']), strtolower($request->input('search.value')))) && 
          (!Str::contains(strtolower($row['created_at']), strtolower($request->input('search.value')))) && 
          (!Str::contains(strtolower($row['nuvei_transaction_id']), strtolower($request->input('search.value')))) && 
          (!Str::contains(strtolower($row['itemcount']), strtolower($request->input('search.value')))) && 
          (!Str::contains(strtolower($row['product_name']), strtolower($request->input('search.value')))) && 
          (!Str::contains(strtolower($row['status']), strtolower($request->input('search.value')))) && 
          (!Str::contains(strtolower($row['order_status']), strtolower($request->input('search.value')))) && 
          (!Str::contains(strtolower($row['order_status_fr']), strtolower($request->input('search.value')))) && 
          (!Str::contains(strtolower($row['product_name_fr']), strtolower($request->input('search.value')))) && 
          (!Str::contains(strtolower($row['sales']), strtolower($request->input('search.value')))) && 
          (!Str::contains(strtolower($row['taxes']), strtolower($request->input('search.value')))) && 
          (!Str::contains(strtolower($row['total']), strtolower($request->input('search.value')))) ){
          $present++;
      }}
      

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题,所以我用雄辩的“何时”定制了假过滤器,我不使用函数 filter()

        $data = $this->anggotaRepo->scope()
                      ->when($request->get('status') , function ($query) use ($request) {
                                    $query->where('status_id', $request->get('status'));
                                })
                      ->when($request->get('nik') , function ($query) use ($request) {
                                    $query->where('nik', $request->get('nik'));
                                })
                      ->when($request->get('anggota') , function ($query) use ($request) {
        
                                $query->where('id', $request->get('anggota'));
                            })->get();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-11-11
          • 2020-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-21
          • 2018-04-05
          • 2016-12-20
          相关资源
          最近更新 更多