2013-10-04 40 views
0

我似乎無法使用tabletools在rails中工作。我遵循使用jquery-datatables gem在http://railscasts.com/episodes/340-datatables設置數據表的說明。我是新手,所以答案可能很簡單,但我根本無法弄清楚。如何初始化Rails中的TableTools 4

這裏是我的applications.js:

//= require jquery 
//= require jquery_ujs 
//= require turbolinks 
//= require dataTables/jquery.dataTables 
//= require dataTables/extras/TableTools 
//= require dataTables/extras/ZeroClipboard 
//= require_tree . 

我application.css:

*= require_self 
*= require dataTables/jquery.dataTables 
*= require dataTables/extras/TableTools 
*= require_tree . 

這是我index.html.erb:

<h1>Products</h1> 

<script> 
    $(document).ready(function() { 
     $('#products').dataTable({ 
     "sDom": "<'row-fluid'<'span6'T><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>", 
     "oTableTools": { 
      "sSwfPath": "media/swf/copy_csv_xls_pdf.swf", 
      "aButtons": [ 
      "copy", 
      "print", 
      { 
       "sExtends": "collection", 
       "sButtonText": 'Save <span class="caret" />', 
       "aButtons": [ "csv", "xls", "pdf" ] 
      } 
      ] 
     } 
     }); 
    }); 
</script> 

<table id='products'> 
    <thead> 
    <tr> 
     <th>Created</th> 
     <th>Updated</th> 
     <th>Product name</th> 
     <th>Category</th> 
     <th>Release date</th> 
     <th>Price</th> 
     <th>Buy</th> 
     <th>Notes</th> 
     <th></th> 
     <th></th> 
     <th></th> 
    </tr> 
    </thead> 

    <tbody> 
    <% @products.each do |product| %> 
     <tr> 
     <td><%= product.created_at.strftime("%m/%d/%Y") %></td> 
     <td><%= product.updated_at.strftime("%m/%d/%Y") %></td> 
     <td><%= product.Product_Name %></td> 
     <td><%= product.Category %></td> 
     <td><%= product.Release_Date %></td> 
     <td><%= product.Price %></td> 
     <td><%= translate(product.Buy.class)%></td> 
     <td><%= product.Notes %></td> 
     <td><%= link_to 'Show', product %></td> 
     <td><%= link_to 'Edit', edit_product_path(product) %></td> 
     <td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

<br> 


<%= link_to 'New Product', new_product_path %> 

現在,我得到這個錯誤:

DataTables警告(表id ='products'):無法重新初始化DataTable。

要檢索的DataTable對象此表,未傳遞參數或查看bRetrieve和bDestroy

的文檔 - 我意識到這可能是從初始化腳本和表ID翻倍。但是,如果我刪除了表格ID,它將刪除所有排序和搜索功能,並且我只得到一個純文本表格。初始化代碼本身似乎並沒有使我的表成爲數據表...但它正在做某件事,否則我不會得到錯誤。

任何想法如何讓這個工作?

感謝, 迪娜

回答

0

嘗試檢查,看看數據表第一次初始化:

$(document).ready(function() { 

    if (!$.fn.dataTable.isDataTable("#products")) { 

    $('#products').dataTable({ 
    "sDom": "<'row-fluid'<'span6'T><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>", 
    "oTableTools": { 
     "sSwfPath": "media/swf/copy_csv_xls_pdf.swf", 
     "aButtons": [ 
     "copy", 
     "print", 
     { 
      "sExtends": "collection", 
      "sButtonText": 'Save <span class="caret" />', 
      "aButtons": [ "csv", "xls", "pdf" ] 
     } 
     ] 
    } 
    }); 
} 
});