【问题标题】:How can show text array according options checked?如何根据检查的选项显示文本数组?
【发布时间】:2015-01-28 16:53:38
【问题描述】:

我正在尝试根据选中的复选框显示文本。

我有 3 个状态,所以当我选中一个或两个复选框时,我会显示在没有 javascript 的情况下选择的选项。

例如:

if I check1 will show me the text active.
if I check2 will show me the text inactive.
if I check3 will show me the text inactive and active.
if I check1 and check 2 will show me the text "active","inactive".
if I check1 and check 2 will show me the text "active","removed".
if I check1 and check 2 will show me the text "removed","inactive".

只需根据选中的复选框保留文本即可。

这里是控制器:

@search_state = params[:search_state]
@people = Person.all

这里是视图:

<%= form_tag(people_path,:method => "get") do %>
 #### BEGIN CHECKBOX #####
 <% if @search_state.blank? %> 
   Check1 <%= check_box_tag "search_state[]", "0" %>
   Check2 <%= check_box_tag "search_state[]", "1" %>
   Check3 <%= check_box_tag "search_state[]", "2" %>
 <% else %>
   Check1 <%= check_box_tag "search_state[]", "0", @search_state.collect(&:to_i).include?(0) %>
   Check2 <%= check_box_tag "search_state[]", "1", @search_state.collect(&:to_i).include?(1) %>
   Check3 <%= check_box_tag "search_state[]", "2", @search_state.collect(&:to_i).include?(2) %>
 <% end %>
  #### END CHECKBOX ####

 ### WANT TO SHOW A TEXT ACCORDING CHECKBOXES SELECTED ###
   <% if @search_state.to_s=="0"%>
     Active
   <% elsif @search_state.to_s=="1"%>
     Inactive
   <% elsif @search_state.to_s=="2" %>
     Removed
   <% elsif @search_state.to_s==["0","1"] %>
     Active,Inactive
   <% elsif @search_state.to_s==["0","2"] %>
     Active,Removed
   <% elsif @search_state.to_s==["1","2"] %>
     Inactive,Removed
   <% elsif @search_state.to_s==["0","1","2"] %>
     Active,Inactive,Removed
   <% end %>
   ### END WANTTO SHOW A TEXT ACCORDING CHECKBOXES SELECTED ### 
 <% end %>

这里是文件和演示:

http://runnable.com/VMkReiHVr0FbfQKN/checkbox-project-test-for-ruby-on-rails
http://runnable.com/VMkRe1xPNqFWSMtd/output

我的代码太长,我认为这不是正确的做法。

例如在这种情况下我有 3 个状态,如果我有更多的状态会太长和混乱。

有人可以帮我在没有几行代码的情况下使用 ruby​​ on rails 制作复选框吗?

我知道使用 Javascript 是解决方案,但这是解决问题的唯一方法吗?请问如何解决?

我将非常感谢所有帮助。

【问题讨论】:

  • 你能解释一下你想要达到的目标吗?我不认为你正在以正确的方式做“它”,但我不确定“它”是什么......你能写出功能的预期行为吗?
  • Sorry MrYoshiji ,我的意思是没有根据复选框编写几行代码,如果我有更多的复选框,代码会太长,我认为不是正确的方法需要使它更动态

标签: javascript ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4


【解决方案1】:

我很难从您的示例中遵循您打算执行的操作。但我认为您的意思是按钮 1 是“活动”,按钮 2 是“非活动”,按钮 3 是“已删除”。此外,我认为您打算没有任何选项是相互排斥的……特别是,您可以同时检查“活动”和“不活动”。您还暗示未来可能会有更多类似行为的复选框。

如果所有这些都是正确的,那么我同意,您当前的方法根本无法很好地扩展。

我不会尝试根据复杂的标准选择完整的消息,而是重构它以从其组件中构建消息。这些方面的东西应该可以让你到达那里:

  flags = []
  flags << 'Active' if @search_state.collect(&:to_i).include?(0)
  flags << 'Inactive' if @search_state.collect(&:to_i).include?(1)
  flags << 'Removed' if @search_state.collect(&:to_i).include?(2)

  puts flags.join(', ')

FWIW,如果您没有理由为所有复选框赋予相同的名称和不同的值,我会给每个复选框一个不同的名称。这将使您的许多代码变得更加简单。大致如下:

   Check1 <%= check_box_tag "search_active", "1", @search_active == 1) %> Active
   Check2 <%= check_box_tag "search_inactive", "1", @search_inactive == 1) %> Inactive
   Check3 <%= check_box_tag "search_removed", "1", @search_removed == 1) %> Removed

如果您真的需要将它们放在一个数组中,您可以稍后将它们连接在一起,这很容易。

【讨论】:

  • 天哪,你是大师,我注意到你没有长谱来展示你知道的。谢谢大卫,它对我有用。
  • 感谢您的时间和经验 +1。
猜你喜欢
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2016-10-11
相关资源
最近更新 更多