简体   繁体   English

Rails 5:如何在部分HTML视图中加载Datatable JavaScript

[英]Rails 5: How to load Datatable javascript in partial html view

I am trying to get a datatable to load in one of my partial views, which is simply a table. 我正在尝试获取一个数据表以加载到我的部分视图之一中,该视图只是一个表。 The datatable is not showing up though, only the html in the "results" partial. 虽然没有显示数据表,但仅显示“结果”部分中的html。

Scenarios is the parent scaffold that was created. 方案是已创建的父支架。 Results is the table partial I am rendering in the index of Scenarios: 结果是我在场景索引中呈现的部分表格:

My question ultimately is this: does _results.html.erb have access to the sceanrios.coffee file? 我的问题最终是这样的:_results.html.erb可以访问sceanrios.coffee文件吗?

project\\app\\views\\scenarios\\_results.html.erb: 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: project \\ app \\ assets \\ javascripts \\ scenarios.coffee:

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

Application.js: 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 .

Browser Errors: 浏览器错误:

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)

Does _results.html.erb have access to the scenarios.coffee file? _results.html.erb可以访问visions.coffee文件吗?

Yes, it should be. 是的,应该如此。 Because scenarios.coffee is a controller-specific-asset meaning it will available to all scenarios_controller#actions which eventually available to the respected views which are called upon by their respected actions. 因为scenarios.coffee是一个特定控制器的资产,这意味着它将对所有scenarios_controller#actions可用,最终可用于受其尊重的操作所调用的受尊重的视图。

I got it to work but there may have been multiple issues with my code: 我可以使用它,但是我的代码可能存在多个问题:

  1. made this change in scenarios.coffee: 在visions.coffee中进行了此更改:

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

  2. Ensure that you have only one version of jquery required. 确保仅需要一个版本的jquery。 Check your application.js, and your application layout head. 检查您的application.js和应用程序布局头。

  3. Ensure that core jquery is loaded before the datatables jquery. 确保在数据表jquery之前加载核心jquery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM