簡體   English   中英

Rails 4 / JS / HTML無法添加下拉菜單,因此它為html表創建了過濾器

[英]Rails 4 / JS / HTML Unable to add a drop down menu so it creates a filter for a html table

試圖弄清楚如何添加一個下拉菜單,以便為我的html表創建一個包含產品列表的過濾器。 使用我不存在的Javascript技能和Google,我遇到了這段有趣的代碼

http://www.bclittleleague.bc.ca/Baseball/Javascript/TableFilter_EN/examples_EN.htm#tbl2 (表格2)

<script language="javascript" type="text/javascript">
//<![CDATA[ 
    var table2_Props =  {                   
                    col_0: "select",
                    col_5: "none",
                    display_all_text: " [ Show all ] ",
                    sort_select: true
                };
    setFilterGrid( "table2",table2_Props );
//]]>
</script>

我已將此添加到我的listing.js.coffee文件中

//<![CDATA[ 
    var table2_Props =  {                   
                    col_0: "select",
                    col_5: "none",
                    display_all_text: " [ Show all ] ",
                    sort_select: true
                };
    setFilterGrid( "table2",table2_Props );
//]]>

listings.html.erb

<h1>Listing listings</h1>

<table class="table">
  <thead>
    <tr>
      <th>Name</th>
      <th>Description</th>
      <th>Price</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>

  <tbody>
    <% @listings.each do |listing| %>
      <tr>
        <td><%= listing.name %></td>
        <td><%= listing.description %></td>
        <td><%= listing.price %></td>
        <td><%= link_to 'Show', listing %></td>
        <td><%= link_to 'Edit', edit_listing_path(listing) %></td>
        <td><%= link_to 'Destroy', listing, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Listing', new_listing_path %>

但是當我編譯時,我ExecJS::RuntimeError in Listings#index. SyntaxError: [stdin]:2:2: reserved word "var" in Desktop/Rails/fooddemo/app/assets/javascripts/listings.js.coffee)得到ExecJS::RuntimeError in Listings#index. SyntaxError: [stdin]:2:2: reserved word "var" in Desktop/Rails/fooddemo/app/assets/javascripts/listings.js.coffee) ExecJS::RuntimeError in Listings#index. SyntaxError: [stdin]:2:2: reserved word "var" in Desktop/Rails/fooddemo/app/assets/javascripts/listings.js.coffee)

Fooddemo <%= stylesheet_link_tag“ application”,媒體:“所有”,“ data-turbolinks-track” =>正確%> <%= javascript_include_tag“ application”,“ data-turbolinks-track” =>正確%> <-錯誤 <%= csrf_meta_tags%>

mu是正確的,問題出在listing.js.coffee文件中。 var在coffeescript之后導致此錯誤。

有兩種解決方法:

1)將您的代碼轉換為coffeescript。 http://js2coffee.org/上有一個有用的轉換器,它提供以下信息:

 table2_Props = col_0: "select" col_5: "none" display_all_text: " [ Show all ] " sort_select: true setFilterGrid "table2", table2_Props 

如果將其粘貼到其中,則需要注意所有間距都正確維護。

2)將文件名更改為listing.js,它將把代碼視為純JavaScript。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM