2012-07-12 37 views
0

我在我的rails應用程序中有一個表格,我想用DataTables使它看起來不錯。我正在使用jquery-datatables-rails寶石。爲什麼DataTables不能在我的rails中工作?

我的部分:

<script type="text/javascript"> 
    $(document).ready(function() { 
    $('#businesses').dataTable(); 
    }); 
    </script> 

<table id="businesses"> 
      <thead> 
       <tr> 
          Bunch of headers here... 

       </tr> 
       </thead> 

       <tbody> 
       <tr> 
       Lots and lots of code here... 
       </tr> 
      </tbody> 
     </table> 

的application.js:

// This is a manifest file that'll be compiled into application.js, which will include all the files 
// listed below. 
// 
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, 
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. 
// 
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 
// the compiled file. 
// 
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD 
// GO AFTER THE REQUIRES BELOW. 
// 
//= require jquery 
//= require jquery_ujs 
//= require dataTables/jquery.dataTables 
//= require_tree . 

application.css:

* 
* This is a manifest file that'll be compiled into application.css, which will include all the files 
* listed below. 
* 
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, 
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path. 
* 
* You're free to add application-wide styles to this file and they'll appear at the top of the 
* compiled file, but it's generally better to create a new file per style scope. 
* 
*= require_self 
*= require dataTables/jquery.dataTables 
*= require_tree . 
*/ 

的數據表文件是在application.jsapplication.css正確引用爲好。

謝謝!

+0

確保你只用裏面的。並在javascript控制檯中查找錯誤(chrome中的f12) – Pheonix 2012-07-12 18:13:05

+0

我只按照您的說法使用。感謝您的調試提示,看起來像錯誤是$(「#企業」)。dataTable不是一個函數... ... – Slicekick 2012-07-12 18:16:37

+0

瀏覽器無法找到該頁面中的datatable.js文件。請在這裏添加application.js的相關代碼 – Pheonix 2012-07-12 18:18:29

回答

0

你應該把你的js代碼移到你的application.js文件中。

所以,你html.erb內部沒有腳本標記,做的JavaScript部分,你應該有這樣的一個application.js中:

// This is a manifest file that'll be compiled into application.js, which will include all the files 
// listed below. 
// 
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, 
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. 
// 
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 
// the compiled file. 
// 
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD 
// GO AFTER THE REQUIRES BELOW. 
// 
//= require jquery 
//= require jquery_ujs 
//= require dataTables/jquery.dataTables 
//= require_tree . 

$('#businesses').dataTable(); 
    }); 
相關問題