php Laravel Yajra数据表服务器端存在分页问题

bvuwiixz  于 2023-01-12  发布在  PHP
关注(0)|答案(4)|浏览(148)

我是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);
    }
jljoyd4f

jljoyd4f1#

你只需要替换掉控制器中的方法,并按照下面的描述设置内容,就可以解决你的
1.使用或不使用搜索管理查询
1.通过启用分页提高性能

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

anhgbhbe2#

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

eeq64g8w3#

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

xvw2m8pv

xvw2m8pv4#

附加注解,
有时数据可能无法正确显示(数据不显示)的页面与分页,所以你可能会添加“绘制”和“数据”,尝试一个接一个.(这个解决方案为旧版本的Yajra数据表)

$draw = $request->get('draw');

...

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

相关问题