为什么不是't在我的rails部分中工作的DataTables

Why isn't DataTables working in my rails partial?

本文关键字:工作 DataTables rails 我的 为什么不      更新时间:2023-09-26

我在rails应用程序的分部中有一个表,我想使用DataTables让它看起来很好看。我正在使用jquery-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中都有适当的引用。

谢谢!

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