简体   繁体   English

单击按钮时如何使输入字段可见或单击按钮以在滑轨上显示红宝石时如何创建相同的字段

[英]How to make a input field visible when a button is clicked or create the same field when a button is clicked for ruby on rails

rails newbie here. Rails新手在这里。

this is my current form 这是我目前的表格

 <%= f.input :campaign_name , :input_html => {:style=> 'width: 300px'} %>
 <%= f.input :date_range, :input_html => {:style=> 'width: 300px'}%>
 <%= f.label :first_event %>
 <%= f.collection_select :first_event, eventNames, :to_s, :to_s, include_blank: true %>
 <br><br><br>
 <%= f.label :second_event %>
 <%= f.collection_select :second_event, eventNames, :to_s, :to_s, include_blank: true%>

what i want is this, when user clicks "add filter" i want another field to pop up with the same eventNames array as a collection select.I tried to create another button and get it's tag and if its not the submit button and its add filter button render another form. 我想要的是,当用户单击“添加过滤器”时,我希望另一个字段以与集合select相同的eventNames数组弹出。我试图创建另一个按钮并获取其标签,如果不是提交按钮及其添加,过滤器按钮呈现另一种形式。

but this is terribly bad as a interface and as a user experience. 但这对于界面和用户体验来说是非常糟糕的。

i want my user to be able to remove the second event field at anytime, without having to submit the form. 我希望我的用户能够随时删除第二个事件字段,而不必提交表单。

So i need to add another button in it to remove the newly made visible form. 所以我需要在其中添加另一个按钮以删除新创建的可见表单。

How can i achieve this 我怎样才能做到这一点

As i said in my comment if you have a restriction that a user could add only a single or lets say a fixed number of filters then you can simply show and hide your collection by js. 正如我在我的评论中所说,如果您限制用户只能添加一个过滤器或说固定数量的过滤器,那么您可以通过js简单地显示和隐藏您的集合。 Your form would have a hidden collection list 您的表单将具有隐藏的收藏夹列表

<%= f.input :campaign_name , :input_html => {:style=> 'width: 300px'} %>
<%= f.input :date_range, :input_html => {:style=> 'width: 300px'}%>
<%= f.label :first_event %>
<%= f.collection_select :first_event, eventNames, :to_s, :to_s, include_blank: true %>
<br><br><br>
<%= f.label :second_event %>
<%= f.collection_select :second_event, eventNames, :to_s, :to_s, include_blank: true%>
<%= f.label :third_event, class: "hidden" %>
<%= f.collection_select :third_event, eventNames, :to_s, :to_s, include_blank: true, class: "hidden"%>

hide your collection by css 隐藏CSS的收藏

.hidden{display: none;} 

and show it by js when a user click on add filter, assuming it's a link you could do: 并在用户单击添加过滤器时通过js显示它,并假设它是您可以执行的链接:

= link_to "Add Filter", "#", id: "add-filter"

$(document).on("click","#add-filter", function(e){
  $(".hidden").show();
  e.preventDefault();
});

If user can add multiple filters then you'll need to add them by ajax. 如果用户可以添加多个过滤器,那么您需要通过ajax添加它们。 For ajax follow these steps: 对于ajax,请遵循以下步骤:

a. 一种。 Create a custom route for your action: 为您的操作创建自定义路线:

post "/filter" => "your_controller#add_filter", as: "filter"

b. create your link for adding filter: 创建用于添加过滤器的链接:

<%= link_to "Add Filter", filter_path, id: "add-filter", data: {event_id: "2", url: filter_path}%>

c. C。 Get event_id by js and send a ajax request: 通过js获取event_id并发送ajax请求:

$(document).on("click","#add-filter".function(e){
  var eventId = $(this).data("eventId");
  var url = $(this).data("url");
  $.ajax({
    type: "POST",
    url: url,
    data: {id: eventId}
  });
})

d. d。 Create add_filter action in your controller: 在控制器中创建add_filter动作:

def add_filter
  @event_id = params[:id]
  respond_to do |format|
    format.js
  end
end

e. Create your new collection and append it by js in app/views/your_controller/add_filter.js.erb file 创建您的新集合,并通过js将其附加到app / views / your_controller / add_filter.js.erb文件中

var label = "<%=j label_tag "#{@event_id.humanize}_event" %>" 
var collection = "<%=j collection_select(:resource, "#{@event_id.humanize}_event".to_sym , eventNames, :to_s, :to_s, include_blank: true) %>"

$("#form_id").find("select").last().after(label).after(collection);
$("#add-filter").data("eventId","<%=j @event_id + 1 %>");

You'll have to use humanize or number_and_words to convert your @event_id to words format and also you'll have to change :resource accordingly 您必须使用humanizenumber_and_words来将number_and_words转换为单词格式,并且还必须相应地更改:resource

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

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