2012-11-05 119 views
2

我想爲我的數據表中的每一行添加一個行號。我正在使用來自http://datatables.net的插件告訴如何添加索引的頁面是http://datatables.net/release-datatables/examples/api/counter_column.html如何將索引列添加到Datatable?

...但是我不知道如何實際實現它才能使其工作。我對jquery/javascript瞭解得非常少,在這種情況下會有所幫助。我不知道在哪裏可以把這個代碼,使其正常工作(如果它可以幫助我也使用Ruby on Rails的)

的初始化代碼:

jQuery -> 
    $('#staffs').dataTable 
    sPaginationType: "full_numbers" 
    bJQueryUI: true 
    } 
+0

添加您的DataTable的初始化代碼工作。 –

+0

@DavidStetler添加到問題 – StanM

回答

3

這裏是datatables.net一個例子網站DataTables row numbers example

$(document).ready(function() { 
    $('#staffs').dataTable({ 
     sPaginationType: "full_numbers", 
     bJQueryUI: true, 
     "fnDrawCallback": function (oSettings) { 
      /* Need to redo the counters if filtered or sorted */ 
      if (oSettings.bSorted || oSettings.bFiltered) 
      { 
       for (var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++) 
       { 
        $('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[i] ].nTr).html(i+1); 
       } 
      } 
     }, 
     "aoColumnDefs": [ 
      { "bSortable": false, "aTargets": [ 0 ] } 
     ], 
     "aaSorting": [[ 1, 'asc' ]] 
    }); 
}); 

關於你SyntaxError: reserved word "function" on line 4 (in /home/ubuntu/ruby/scoreboard/app/assets/javascripts/staffs.js.coffee)'錯誤

看看這個rails, getting syntax error with coffee script

+0

正確的是,從我提供的鏈接複製/粘貼(我已看到此代碼)。我只是不知道在哪裏把這個代碼實際上使其工作 – StanM

+0

沒有點擊您的鏈接...編輯我的答案(添加您的兩行代碼...) – Daniel

+0

我試過只是複製和粘貼它像那樣之前,但我不斷收到第4行 (在/home/ubuntu/ruby/scoreboard/app/assets/javascripts/staffs.js.coffee)上的'SyntaxError:reserved word「函數」' – StanM

0

jquery是javascript。您需要添加丹尼爾粘貼標籤

之間
<script language="javascript"> 

</script> 

的代碼。

+0

謝謝,如果我把它保存在html文件中,這將工作,但對我來說,工作是js2coffee轉換器。 – StanM

0

我與最新的dataTable 1.10和寶石rails datatable and ajx

找到DataTable的行號(序列號)

def data outer = [] records.each_with_index do |record, index| outer << [ # comma separated list of the values for each cell of a table row # example: record.attribute, index + 1 + params[:start].to_i, record.company_name, record.id, record.patients.count, record.revenue_total ] end outer end