2017-06-13 72 views
0

我使用數據表來顯示來自控制器(我使用Codeigniter)的數據,並且需要在左表列中顯示數字列。索引列上的數據表返回[對象對象]

我曾嘗試:

$(document).ready(function() { 
 
\t $('#booking_table').dataTable({ 
 
     processing: true, 
 
     serverSide: true, 
 
     language: dt_lang, 
 
     pagingType: "simple", 
 
     dom: 't<"col-sm-3 text-left"l><"col-sm-3"i><"col-sm-2"r><"col-sm-4 text-right"p>', 
 
     autoWidth : true, 
 
     ajax: { 
 
      "url" : base_url+"book/ajax_history", 
 
      "type" : "POST", 
 
      data : function (d){ 
 
      \t d.show_filter = $('#_show_filter').val(); 
 
      \t d.view_type = $('#_view_type').val(); 
 
      } 
 
     }, 
 
     columns: [ 
 
      { 
 
       data : "b.booking_id", 
 
       visible : false, 
 
      }, 
 
      { data : null}, //where i should put index number 
 
\t \t \t { data : 'b.booking_date', className : "hidden-xs"}, 
 
\t \t \t { data : 'b.from_name', className : "hidden-xs"}, 
 
\t \t \t { data : 'b.to_name'}, 
 
     ], 
 
     responsive: false 
 
    }); 
 

 
\t // reference the table in a variable 
 
\t var table = $('#booking_table').DataTable(); 
 

 
    table.on('order.dt search.dt', function() { 
 
     table.column(0, { 
 
      search:'applied', 
 
      order:'applied' 
 
     }).nodes().each(function (cell, i) { 
 
      cell.innerHTML = i+1; 
 
     }); 
 
    }).draw();

我的表:

<table class="table table-condensed" id="booking_table"> 
 
         <thead> 
 
          <tr> 
 
           <th class="hidden-xs">id</th> 
 
           <th>No</th> 
 
           <th class="hidden-xs"> 
 
            Tanggal 
 
           </th> 
 
           <th class="hidden-xs"> 
 
            Pengirim 
 
           </th> 
 
           <th> 
 
            Penerima 
 
           </th> 
 
          </tr> 
 
         </thead> 
 
         <tbody> 
 
         </tbody> 
 
        </table>

請參閱本https://www.datatables.net/examples/api/counter_columns.html

但是,它不工作。我究竟做錯了什麼 ?

回答

0

請嘗試下面提到的解決方案。希望這會幫助你。其實你已經初始化了兩次數據表。

$(document).ready(function() { 
 
    var table = $('#booking_table').dataTable({ 
 
     processing: true, 
 
     serverSide: true, 
 
     language: dt_lang, 
 
     pagingType: "simple", 
 
     dom: 't<"col-sm-3 text-left"l><"col-sm-3"i><"col-sm-2"r><"col-sm-4 text-right"p>', 
 
     autoWidth: true, 
 
     ajax: { 
 
      "url": base_url + "book/ajax_history", 
 
      "type": "POST", 
 
      data: function(d) { 
 
       d.show_filter = $('#_show_filter').val(); 
 
       d.view_type = $('#_view_type').val(); 
 
      } 
 
     }, 
 
     columns: [{ 
 
       data: "b.booking_id", 
 
       visible: false, 
 
      }, 
 
      { 
 
       data: null 
 
      }, //where i should put index number 
 
      { 
 
       data: 'b.booking_date', 
 
       className: "hidden-xs" 
 
      }, 
 
      { 
 
       data: 'b.from_name', 
 
       className: "hidden-xs" 
 
      }, 
 
      { 
 
       data: 'b.to_name' 
 
      }, 
 
     ], 
 
     responsive: false 
 
    }); 
 

 
    table.on('order.dt search.dt', function() { 
 
     table.column(0, { 
 
      search: 'applied', 
 
      order: 'applied' 
 
     }).nodes().each(function(cell, i) { 
 
      cell.innerHTML = i + 1; 
 
     }); 
 
    }).draw(); 
 
});

+0

不,他沒有。如果一個dataTable已經被實例化了,它只是返回一個jQuery「object」或一個API實例。 – davidkonrad