javascript - 显示 Rails 项目中助手内行的隐藏

标签 javascript html ruby-on-rails

我正在我的辅助方法中运行一个循环。

这给了我一个结果,比如在 6 个项目之后,该行关闭,然后对于接下来的 6 个项目,创建另一行,然后继续。

现在我想要另一个条件,如果有超过 2 行,我不想显示第三个或第四个循环,而是我想放置一个链接/按钮,上面写着“显示更多”,然后单击“显示其他”项目。

这是代码

 part_child_option.  each_slice(6) do | six_o|
  html += "<div class = 'row'>"
  six_o.each do |o|
  html += "<div class='col-sm-2 uno_part_wrapper'>"
  html += "<label class = 'p_name' for='#{attr_name}'>"
    html += image_tag o.photo(:small), class: "tick_option_img"
      html += "</label>"
      html += "</div>"
   end 
      html += "</div>"
   end
 html.html_safe

对此有什么想法吗?我们可以设置一个类似 if count>2 的条件,隐藏剩余行并单击显示全部吗?

最佳答案

请详细说明您的问题并显示 View 中的一些代码。问题是当单击“更多+”按钮时您想要做什么(想要打开新的索引页或通过 ajax 仅加载剩余行)。

根据可用的描述,我建议您放置一个link_to more, items_path并在所有项目的索引页面上重定向。

更新: 每行包含 6 个元素,默认情况下有 2 行意味着 12 个元素。因此,请将 each_slice 循环替换为 each_slice_with_index 并添加 if 条件。

例如

part_child_option.  each_slice_with_index(6) do | six_o , i|
  if i <= 1      
      html += "<div class = 'row'>"
      six_o.each do |o|
      html += "<div class='col-sm-2 uno_part_wrapper'>"
      html += "<label class = 'p_name' for='#{attr_name}'>"
        html += image_tag o.photo(:small), class: "tick_option_img"
          html += "</label>"
          html += "</div>"
       end 
          html += "</div>"
   elsif i == 2
      show link here 
      html += same code as above with hidden class added 
   else
      html += same code as above with hidden class added
   end
 end

我建议将一些行生成代码放到更小的方法中,以使这个帮助器变得简单。

关于javascript - 显示 Rails 项目中助手内行的隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32264875/

相关文章:

自 1-1-1970 以来,javascript 以毫秒为单位将本地日期和时间数组转换为 UTC?

html - 将鼠标悬停在另一个 div 中的元素上时如何在一个 div 中显示元素

css - Div 垂直中间的文本

ruby-on-rails - 获取 has_many 关联时出错,TypeError : can't convert String into Integer

javascript - 预编译的 javascript 文件在 heroku 部署后不起作用,有没有好的 js 文件顺序?

javascript - 带有 svg 和(双击)单击事件的 <object> 标签

php - 单引号内的单引号 PHP

sql - 如何在 postgresql 和 rails 3 中按月分组

javascript - 阻止 UserScript 自动点击

css - Bootstrap 网站,嵌入 iframe (youtube) 溢出并阻止其他内容