7

我是 Laravel 的新手,我正在尝试使用带有服务器端功能的Yajra Datatable插件。该插件适用于少量记录,但我有大量大约 100000 条记录。

为了加快我的控制器中的过程,我使用 take(10) 限制查询的结果,并使用另一个查询来计算总结果。到目前为止一切都很好。

问题是如何管理研究。除了主要研究领域外,我还使用了单独的列搜索,但我不知道如何返回正确的记录数来使用单独的搜索过滤器管理分页。

我认为个人搜索键在$columns = $request->get('columns');但我不知道如何管理计数的查询。

感谢您的宝贵建议。

HTML 查看代码:

<table id="oTable">
   <thead>
      <tr>
         <th>Action</th>
         <th>Brand</th>
         <th>Code</th>
         <th>Description</th>
      </tr> 
      <tr>
         <th class="no_search"></th>
         <th></th>
         <th></th>
         <th></th>
      </tr>
   </thead>
</table>

jQuery代码:

$('#oTable').DataTable({
    dom: 'lfrtip',
    "processing": true,
    "serverSide": true,
    "ajax": '{!! url('getRecords') !!}',
    "columns": [
      {data: 'items.id', name: 'items_id'},
      {data: 'brands.description', name: 'brands_description'},
      {data: 'items.code', name: 'items_code'},
      {data: 'items.description', name: 'items_description'}
    ],
    columnDefs: [
      {targets: 'no_sort', orderable: false}
    ],
    initComplete: function () {

      this.api().columns().every(function () {
        var column = this;
        var columnClass = column.header().className;
        if (columnClass.indexOf('no_search') != false) {
          var input = document.createElement("input");
          $(input).addClass('form-control');
          $(input).appendTo($(column.header()).empty())
          .on('change', function () {
            column.search($(this).val(), false, false, true).draw();
          });
        }
      });
    }
  });

控制器方法:

public function getRecords(Request $request) {

      $search = $request->input('search.value');
      $columns = $request->get('columns');

      $count_total = \DB::table('items')
                        ->join('brands', 'item.brand', '=', 'brands.code')
                        ->count();

      $count_filter = \DB::table('items')
                        ->join('brands', 'items.brand', '=', 'brands.code')
                        ->where(   'brands.description' , 'LIKE' , '%'.$search.'%')
                        ->orWhere( 'items.description' , 'LIKE' , '%'.$search.'%')
                        ->orWhere( 'items.code' , 'LIKE' , '%'.$search.'%')
                        ->count();

      $items= \DB::table('items')
        ->join('brands', 'items.brand', '=', 'brands.code')
        ->select(
            'items.id as items_id',
            'items.code as items_code',
            'items.description as items_description',
            'brands.description as brands_description'
        ) -> take(10);

        return Datatables::of($items)          
          ->with([
            "recordsTotal" => $count_total,
            "recordsFiltered" => $count_filter,
          ])
          ->rawColumns(['items_id','brands_description'])
          ->make(true);
    }
4

3 回答 3

1
 public function getRecords(Request $request) {
        //Use this way of your code
        $search  = $request->input('search.value');
        $columns = $request->get('columns');
        $order   = isset($_GET[ 'order' ]) ? $_GET[ 'order' ] : [];

        $count_total = \DB::table('items')
                          ->join('brands', 'item.brand', '=', 'brands.code')
                          ->count();

        $count_filter = \DB::table('items')
                           ->join('brands', 'items.brand', '=', 'brands.code')
                           ->where('brands.description', 'LIKE', '%' . $search . '%')
                           ->orWhere('items.description', 'LIKE', '%' . $search . '%')
                           ->orWhere('items.code', 'LIKE', '%' . $search . '%')
                           ->count();

        $items = \DB::table('items')
                    ->join('brands', 'items.brand', '=', 'brands.code')
                    ->select(
                        'items.id as items_id',
                        'items.code as items_code',
                        'items.description as items_description',
                        'brands.description as brands_description'
                    );
        foreach ($order as $o) {
            if(isset($columns[ $o[ 'column' ] ])) {
                $items = $items->orderBy($columns[ $o[ 'column' ] ][ 'name' ], $o[ 'dir' ]);
            }
        }
        $items = $items->take(10);

        return Datatables::of($items)
                         ->with([
                             "recordsTotal"    => $count_total,
                             "recordsFiltered" => $count_filter,
                         ])
                         ->rawColumns(['items_id', 'brands_description'])
                         ->make(TRUE);
    }
于 2019-06-18T08:30:12.547 回答
1

您只需要替换控制器内部的方法并按如下所述设置内容。它将解决您的问题

  1. 管理带有或不带有搜索的查询
  2. 通过启用分页来提高性能

    public function getRecords(Request $request) {
    
        $search = $request->input('search.value');
        $columns = $request->get('columns');
    
        $pageSize = ($request->length) ? $request->length : 10;
    
        $itemQuery = \DB::table('items')
        ->join('brands', 'items.brand', '=', 'brands.code');
    
        // $itemQuery->orderBy('items_id', 'asc');
        $itemCounter = $itemQuery->get();
        $count_total = $itemCounter->count();
    
        $count_filter = 0;
        if($search != ''){
            $itemQuery->where( 'brands.description' , 'LIKE' , '%'.$search.'%')
                    ->orWhere( 'items.description' , 'LIKE' , '%'.$search.'%')
                    ->orWhere( 'items.code' , 'LIKE' , '%'.$search.'%')
            $count_filter = $itemQuery->count();
        }
    
        $itemQuery->select(
            'items.id as items_id',
            'items.code as items_code',
            'items.description as items_description',
            'brands.description as brands_description'
        );
    
        $start = ($request->start) ? $request->start : 0;
        $itemQuery->skip($start)->take($pageSize);
        $items = $itemQuery->get();
    
        if($count_filter == 0){
            $count_filter = $count_total;
        }
    
        return Datatables::of($items)          
            ->with([
            "recordsTotal" => $count_total,
            "recordsFiltered" => $count_filter,
            ])
            ->rawColumns(['items_id','brands_description'])
            ->make(true);
    }
    
于 2020-04-27T18:19:14.763 回答
0

将实现用作服务,请在此处查看文档https://datatables.yajrabox.com/services/basic

于 2018-06-20T21:09:06.980 回答