【问题标题】:Foundation Text Grid基础文本网格
【发布时间】:2015-03-25 11:42:42
【问题描述】:

我正在尝试创建一个包含文本而非图像的网格,类似于 Zurb 模板中的“内容部分”:http://foundation.zurb.com/templates/orbit.html

我想在每一行上创建三个文本块。

我能够创建这个,但出现的问题是,有时在我的数据中,一行只有一两个文本块。

例如,如果产品缺少它的位置,有没有办法移动成分来代替位置,或者如果缺少类别和位置来将成分移动到类别位置?

我正在使用 Ruby on Rails 和 Foundation 5。

谢谢!

  <div class="row">
    <div class="large-4 columns">
      <% unless @product.category.nil? %>
      <div class="product-heading">Product Category</div>
      <div class="product-value">
        <%= @product.category %>
      </div>
      <% end %>
    </div>

    <div class="large-4 columns">
      <% unless @product.location.nil? %>
      <div class="product-heading">Product Location</div>
      <div class="product-value">
        <%= @product.location.sort.join(", ") %>
      </div>
      <% end %>
    </div>

    <div class="large-4 columns">
      <% unless @product.ingredients.nil? %>
      <div class="product-heading">Product Ingredients</div>
      <div class="product-value">
        <%= @product.ingredients.sort.join(", ") %>
      </div>
      <% end %>
    </div>
  </div>

根据 Max Williams 的建议生成的 HTML。

  <div class="row">
    <div class="large-4 columns">
      <div class="product-heading">Product Category</div>
      <div class="product-value">Food </div>
    </div>
    <div class="large-4 columns">
      <div class="product-heading">Product Ingredients</div>
      <div class="product-value">Flour, Sugar </div>
    </div>
  </div>

Javascript 尝试删除空的 li 标签

<script>$('li, p')
  .filter(function() {
    return $.trim($(this).text()) === '' && $(this).children().length == 0
  })
  .remove();
</script>

【问题讨论】:

  • 我认为这很容易解决,但您没有提供任何有关您的实际 Rails 代码、模型等的详细信息。例如,您是否在此处迭代对象集合?您应该包括相关的 erb 代码以及呈现的 html。
  • @MaxWilliams 我已经更新了代码。我是新手,但我希望我已经为您提供了足够的信息。谢谢。
  • 你能改用block-grid吗? codeply.com/go/iouWc9rtUy
  • @Skelly 感谢代码示例。我试过使用块网格,但是当一个块丢失时,块不会移动。 HTML 显示有一个空的
  • 标记。如果我通过网络检查器删除它,那么一切正常。我尝试使用 javascript 删除空的
  • 标记,但它不起作用。有什么建议吗?

标签: css ruby-on-rails zurb-foundation


【解决方案1】:

关键是寻找重复的元素,并将它们抽象出来,例如使用循环。例如

<% product_cells = [["Category", @product.category], ["Location", @product.location_string], ["Ingredients", @product.ingredients_string]] %>
<div class="row">
  <% product_cells.each do |title, data| %>
    <% unless data.blank? %>    
      <div class="large-4 columns">
        <div class="product-heading">Product <%= title %></div>
        <div class="product-value"><%= data %> </div>
      </div>
    <% end %>
  <% end %>
</div>

请注意,我已将测试放在不同的位置,因此如果数据为空白,则会跳过整个 large-4 div。

@product.location.sort.join(", ") 这种东西,一般应该换成 Product 模型中的方法,例如

#in Product
def location_string
  self.location.reject(&:blank?).sort.join(", ")
end

def ingredients_string
  self.ingredients.reject(&:blank?).sort.join(", ")
end

那么你可以把上面的代码改成

<% product_cells = [["Category", @product.category], ["Location", @product.location_string], ["Ingredients", @product.ingredients_string]] %>

编辑:更改字符串方法以应对 nils

EDIT2:更改了我原来的答案以使用我添加的模型方法。

【讨论】:

  • 感谢您的帮助。我现在已经在 Product 模型中创建了方法。我在您的代码中遇到了两个问题。我收到连接和排序错误:undefined method `join' for nil:NilClass。此外,标题和数据不会移动。我已经更新了我的问题以向您展示 html。谢谢。
  • nil:NilClass` 的undefined method join/sort' 表示self.locationself.ingredients 正在返回一些nil 值。我会修改我的方法。
  • 你能解释一下“标题和数据不动”是什么意思吗?
  • 抱歉含糊不清。这是我要实现的目标的图像:imgur.com/6CKne8C。在第一种情况下,产品包含所有三个标题的信息;第二个产品缺少产品位置信息,所以我想移动产品成分来代替产品位置等等。我希望这是有道理的。我更新了我的字符串方法,但我现在收到以下错误:undefined method 'reject' for nil:NilClass。我设法通过在每种方法中添加unless self.category/location/ingredient.nil? 来解决这个问题。感谢您的时间和努力
  • 你可以试试我最新的编辑吗?我希望它只在第二行呈现两个“.large-4”div,其中位置为空白。是这样吗?
猜你喜欢
相关资源
最近更新 更多
热门标签