2017-06-16 112 views
0

我正在使用laravel 5.0 我也使用數據表jquery插件來顯示網格。現在 我硬編碼 - >限制(1000)1000工作在數據表中網格顯示 ,但我有更多的則1000點的記錄顯示:laravel中datatable分頁

控制器mehtod

public function index() { 

    $jobs = \App\Job::orderBy('created_at', 'DESC')->limit(1000)->get(); 

    return View::make('jobs.index', ['jobs' => $jobs]); 
} 

問題。

我想要什麼? 我想用網格和500條記錄顯示500條記錄。 我不確定是否有任何回調數據表插件功能可用? 我需要一個動態的方式來加載下一個500

注:我不願意給我們 滾動 https://datatables.net/extensions/scroller/examples/initialisation/server-side_processing.html

回答

0

您可以將用戶ajax的數據源:

,請訪問:https://datatables.net/examples/ajax/objects.html

示例PHP腳本:

// function will process the ajax request 
public function getMembers(Request $request) { 

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


     $search = (isset($filter['value']))? $filter['value'] : false; 

     $total_members = 1000; // get your total no of data; 
     $members = $this->methodToGetMembers($start, $length); //supply start and length of the table data 

     $data = array(
      'draw' => $draw, 
      'recordsTotal' => $total_members, 
      'recordsFiltered' => $total_members, 
      'data' => $members, 
     ); 

     echo json_encode($data); 
    } 

例的JavaScript:

$('#all-member-table').DataTable({ 
     "processing": true, 
     "serverSide": true, 
     "ajax": { 
      url: base_url+"ajax/members" 
      }, 
      "columns": [ 
      { data: '1' }, 
      { data: '2' }, 
      { data: '3' }, 
      { data: '4' }, 
      { data: '5' }, 
     ] 
    }); 

示例HTML:

<table id="all-member-table"> 
      <thead> 
       <tr> 
        <th>Column1</th> 
        <th>Column2</th> 
        <th>Column3</th> 
        <th>Column4</th> 
        <th>Column5</th> 
       </tr> 
      </thead> 
    </table> 
+0

如何搜索和排序? – Peter

0

的這種解決方案可以使用standard pagination

$jobs = \App\Job::latest()->paginate(500); 

或者create it manually

+0

我需要一個動態的方式來加載下一個500條記錄 – Peter

+0

@Peter所以有什麼問題?只需使用ajax使用我所展示的相同代碼加載以前的500或500條記錄。 –

+0

我需要一個邏輯加載下一個前500條記錄。所以我期待一些解決方案。 – Peter