【问题标题】:Why isn't DataTables working in my rails partial?为什么 DataTables 不能在我的 rails 部分工作?
【发布时间】:2012-07-12 18:10:08
【问题描述】:

我的 rails 应用程序中有一个部分表格,我想使用 DataTables 使它看起来更漂亮。我正在使用jquery-datatables-rails gem。

我的部分:

 <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 中正确引用了 DataTables 文件。

谢谢!

【问题讨论】:

  • 确保您只在 的 中使用 。并在 javascript 控制台中查找错误(chrome 中的 f12)
  • 我只使用 如你所说。感谢调试提示,看起来错误是$("#businesses").dataTable is not a function...
  • 浏览器在此页面中找不到 datatable.js 文件。请在此处添加application.js的相关代码
  • 好的,相关代码已添加。
  • 在最终生成的html中,确定jquery.js、datatable.js .. ..的包含顺序。

标签: javascript ruby-on-rails datatables partial


【解决方案1】:

您应该将您的 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();
    } );

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签