Laravel Yajra Datatable Server Side有分页问题

时间:2018-01-10 11:01:26

标签: php jquery laravel-5 datatables yajra-datatable

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

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

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

我认为个人搜索键位于$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);
    }

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

 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);
    }

答案 2 :(得分:0)

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

问题
  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);
    }