首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Rails 5:如何在部分html视图中加载Datatable javascript

Rails 5:如何在部分html视图中加载Datatable javascript
EN

Stack Overflow用户
提问于 2018-09-25 01:02:58
回答 1查看 471关注 0票数 1

我正在尝试将一个datatable加载到我的一个分部视图中,它只是一个表。数据表没有显示,只显示了"results“部分中的html。

Scenario是创建的父scaffold。Results是我在场景索引中呈现的表的一部分:

我的问题最终是:_results.html.erb有权访问sceanrios.coffee文件吗?

project\app\views\scenarios\_results.html.erb:

<table id="example" class="display" style="width:100%">
  <thead>
  <tr>
    <th>Submitter</th>
    <th>Scenario Name</th>
    <th>Options</th>
    <th colspan="8"></th>
  </tr>
  </thead>
  <tbody>
  <% @scenarios.each do |scenario| %>
    <tr>
      <td class="text-left"><%= scenario.submitter %></td>
      <td class="text-left"><%= scenario.scenario_name %></td>
      <td><%= render 'options', scenario: scenario %></td>
    </tr>
  <% end %>
  </tbody>
</table>

project\app\assets\javascripts\scenarios.coffee:

jQuery ->
  $(document).ready ->
    console.log("Working!")
    $('#example').dataTable()

Application.js:

//= require rails-ujs
//= require jquery
//= require jquery_ujs
//= require dataTables/bootstrap/3/jquery.dataTables.bootstrap
//= require dataTables/jquery.dataTables
//= require turbolinks
//= require_tree .

浏览器错误:

Uncaught TypeError: Cannot read property 'defaults' of undefined
    at jquery.dataTables.bootstrap.self-131f13015a9da63d8ed71f09b1c89ebd2fc8c81f1b2032430e06b8547d03f943.js?body=1:48
    at jquery.dataTables.bootstrap.self-131f13015a9da63d8ed71f09b1c89ebd2fc8c81f1b2032430e06b8547d03f943.js?body=1:40
    at jquery.dataTables.bootstrap.self-131f13015a9da63d8ed71f09b1c89ebd2fc8c81f1b2032430e06b8547d03f943.js?body=1:42

Uncaught TypeError: $(...).dataTable is not a function
    at HTMLDocument.<anonymous> (scenarios.self-8ab21e2e580c359da8a7fb9d7d533ce24740a882e2519d4675b5c305690a9b97.js?body=1:3)
    at fire (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:3233)
    at Object.fireWith [as resolveWith] (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:3363)
    at Function.ready (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:3583)
    at HTMLDocument.completed (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:3618)

Uncaught TypeError: Cannot read property 'LL_onclickHanlderExist' of null
    at window.LL_CustomUI.LL_CustomUI.animation.LL_CustomUI.animation.addClickHandler (hostui_animation.js?rnd=0.7801729638396486:1)
EN

回答 1

Stack Overflow用户

发布于 2018-09-25 04:04:26

我让它工作了,但我的代码可能有多个问题:

  1. 在scenarios.coffee中进行了以下更改:

$.noConflict() jQuery(document).ready ($) -> $('#example').DataTable() return

  • 确保您只需要一个版本的jquery。检查您的application.js和应用程序布局标题。

  • 确保在加载数据之前加载核心jquery。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52484163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档