簡體   English   中英

Laravel Yajra 數據表服務器端存在分頁問題

[英]Laravel Yajra Datatable Server Side with pagination problems

我是 Laravel 的新手,我正在嘗試使用具有服務器端功能的Yajra 數據表插件。 該插件適用於少量記錄,但我有大量約 100000 條記錄。

為了加快我的 controller 中的進程,我使用 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);
    }
 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);
    }

將實現用作服務,查看此處的文檔https://datatables.yajrabox.com/services/basic

您只需要替換控制器內部的方法並按如下所述設置內容。 它將解決您的問題

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

附加條款,

有分頁的頁面有時數據顯示不正確(data not showing),所以你可以加上“draw”和“data”,一個一個試試。 (此解決方案適用於舊版本的 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM