2

问题现在已解决。

我通过删除与页面滚动的粘性标题的 javascript 部分来修复它。当代码在 application.html.erb 中时,我真的不知道为什么我的 javascript 很糟糕


当我的 application.html.erb 如下所示时,我的数据表将起作用:

<!DOCTYPE html>
<html>

    <head>
        <title>Dummyapp1</title>
          <%= stylesheet_link_tag "application" %>
          <%= javascript_include_tag "application", "contractens" %>
          <%= csrf_meta_tags %>

    <div>

    </head>

<body>
==rest of script==

当我的 application.html.erb 看起来像这样时它不起作用:

<!DOCTYPE html>
<html>

    <head>
        <title>Dummyapp1</title>
          <%= stylesheet_link_tag "application" %>
          <%= javascript_include_tag "application", "contractens" %>
          <%= csrf_meta_tags %>

    <div>

</div>

    <!-- If you have jQuery directly, then skip next line -->
    <script src="http://www.google.com/jsapi"></script>

    <script type="text/javascript">
    // If you have jQuery directly, then skip next line
    google.load("jquery", "1");

    function sticky_relocate() {
      var window_top = $(window).scrollTop();
      var div_top = $('#sticky-anchor').offset().top;
      if (window_top > div_top)
        $('#sticky').addClass('stick')
      else
        $('#sticky').removeClass('stick');
      }
    // If you have jQuery directly, use the following line, instead
    // $(function() {
    // If you have jQuery via Google AJAX Libraries
    google.setOnLoadCallback(function() {
      $(window).scroll(sticky_relocate);
      sticky_relocate();
      });
    </script>

    </head>

<body>

我按照这个Railscasts教程来获取 rails 中的数据表。我在我的另一个应用程序中有这个。在那里它工作正常。我做了与其他应用程序相同的事情,但它显示的是文本而不是实际的表格。

这是我对表格的看法:

    <h1>Listing contracten</h1>

    <table id="contractens" class="display">
      <thead>
          <tr>
            <th>Naam</th>
            <th>Omschrijving</th>
            <th>Datumingang</th>
            <th>Contractduur</th>
            <th>Opzegtermijn</th>
            <th>Betalingstermijn</th>
          </tr>
      </thead>
    <tbody>
      <%= @contractens.each do |contracten| %>
      <tr>
        <td><%= contracten.naam %></td>
        <td><%= contracten.omschrijving %></td>
        <td><%= contracten.datumingang %></td>
        <td><%= contracten.contractduur %></td>
        <td><%= contracten.opzegtermijn %></td>
        <td><%= contracten.betalingstermijn %></td>
      </tr>
    <% end %>
    </tbody>
    </table>

<%= link_to "Nieuw", new_contracten_path %>

我的javascript文件填写正确。

这是问题的屏幕截图:

问题

希望你能帮我 :)

编辑:它应该看起来像这样,没有 css:

在此处输入图像描述

使用 css 应该是这样的:

在此处输入图像描述

这是我的 contractens.js.coffee :

jQuery ->
        $('#contractens').dataTable
          sPaginationType: "full_numbers"
          bJQueryUI: true

这是我的 application.js:

//= require jquery
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require_tree .

这是我的application.css:

/*
*= require_self
*= require dataTables/jquery.dataTables
*= require_tree .
*/

编辑2:

当我有一个javascript函数可以在单击复选框时隐藏一个div。当我有

jQuery ->
        $('#contractens').dataTable

在我的contractens.js.coffee 中它确实有效,但是当我有:

jQuery ->
        $('#contractens').dataTable
        sPaginationType: "full_numbers"
        bJQueryUI: true

它也不起作用。所以我猜有些东西阻止了jquery。

4

2 回答 2

2

改变

<%= @contractens.each do |contracten| %>

为了

<% @contractens.each do |contracten| %>

<%= %>需要输出时使用标记集。

于 2012-06-27T14:13:38.253 回答
1

我也有同样的问题,我改变了这个问题:在我的 application.js 中:

//= require jquery
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require_tree .

//= require jquery
//= require_tree .
//= require jquery_ujs
//= require dataTables/jquery.dataTables

它现在有效。

于 2013-07-02T07:05:01.950 回答