2017-03-02 48 views
0

我一直在使用Laravel 5.2項目中的Datatables插件。Datatables不能處理5列或更少

我正在用jQuery和Ajax發佈表格。

這是我的HTML表:

<table id="entidadeTable" class="table"> 
    <thead> 
     <tr> 
      <th>ID</th> 
      <th>Nome</th> 
      <th>Descrição</th> 
      <th>Ações</th> 
     </tr> 
    </thead> 
</table> 

此後,我由DataTable的實例表:

var entidadeTable = $('#entidadeTable').DataTable({ 
    "language": { 
     "url":"//cdn.datatables.net/plugins/9dcbecd42ad/i18n/Portuguese.json" 
    }, 
    "order": [[0, "desc"]], 
    "ajax": { 
     "method": "POST", 
     "url": "{{ route('getTabelaEntidades') }}", 
     "data": {_token: "{{ Session::token() }}"} 
    }, 
    "columns": [ 
     {"data": "id"}, 
     {"data": "nome"}, 
     {"data": "descricao"}, 
     {"data": "actions"} 
    ], 
}); 

這是我的控制器代碼,也就是負責將數據獲取到數據表:

$entidades = Entidade::all(); 

return response()->json(['draw' => 1, 'recordsTotal' => $entidades->count(), 
      'recordsFiltered' => $entidades->count(), 'data' => $entidades]); 

但我得到這個數據表錯誤:

https://www.datatables.net/manual/tech-notes/4

我只得到這個錯誤,如果我的表中有不到6列。 我試圖把超過5列在我的表之後,它的工作和錯誤消失

錯誤:

DataTables warning: table id=entidadeTable - Requested unknown parameter '4' for row 0, column 4. For more information about this error, please see http://datatables.net/tn/4 
+0

退房此[包(https://github.com/yajra/laravel-數據表)。它幫助你很多 –

回答

0

HTML

<table id="entidadeTable" class="table"> 
<thead> 
    <tr> 
     <th>ID</th> 
     <th>Nome</th> 
     <th>Descrição</th> 
     <th>Ações</th> 
    </tr> 
</thead> 

retu數據 Laravel會自動返回您的json類型。

return Entidade::all(); 

數據表

var entidadeTable = $('#entidadeTable').DataTable({ 
    "language": { 
     "url":"//cdn.datatables.net/plugins/9dcbecd42ad/i18n/Portuguese.json" 
    }, 
    "order": [[0, "desc"]], 
    "ajax": { 
     "method": "POST", 
     "url": "{{ route('getTabelaEntidades') }}", 
     "data": {_token: "{{ Session::token() }}"} 
    }, 
    "columns": [ 
     {"data": "id"}, 
     {"data": "nome"}, 
     {"data": "descricao"}, 
     {"data": null, orderable: false, 
       render: function(data , type, row){ 
        var html ='<div class="btn-group">'+ 
         '   <button type="button" onclick="edit('+row.id+');" class="btn btn-xs btn-info">' + 
         '    <i class="ace-icon fa fa-pencil bigger-130"></i>' + 
         '   </button>' + 
         '  </div>'; 
        return html; 
       },className: "center" 
      } 
    ], 
}); 
+0

沒有工作,因爲我不是說所需的參數: response() - > json(['draw'=> 1,'recordsTotal'=> $ entidades-> count(), 'recordsFiltered'=> $ entidades-> count(),'data'=> $ entidades]); –

3

添加此數據表屬性和差錯消失

columnDefs: [{ }],