狂欢 - 仅在选择类别时显示子类别

时间:2013-12-17 06:50:32

标签: categories taxonomy spree

我是Spree的新手,所以非常感谢任何帮助!

我找到了一种方法让Spree在左侧导航中显示子类别(设置config.max_level_in_taxons_menu = 4),但在此方法中,所有子类别始终显示。见下文:

在上面的示例中,我只想在选择“服装”时显示“衬衫和T恤”。

我想我必须编辑base_helper.rb文件中的taxons_tree函数,但我不知道从哪里开始。

def taxons_tree(root_taxon, current_taxon, max_level = 1)
  return '' if max_level < 1 || root_taxon.children.empty?
  content_tag :ul do
    root_taxon.children.map do |taxon|
      css_class = (current_taxon && current_taxon.self_and_ancestors.include?(taxon)) ? 'active' : nil
      content_tag :li, class: css_class do
       link_to(taxon.name, seo_url(taxon), "class" => css_class) +
       taxons_tree(taxon, current_taxon, max_level - 1)
      end
    end.join("\n").html_safe
  end
end

提前致谢!

1 个答案:

答案 0 :(得分:0)

好的 - 我想在写出这个问题时,它会让大脑中的齿轮工作,我想出来了:

def taxons_tree(root_taxon, current_taxon, max_level = 1, current_level = 1)
  return '' if max_level < 1 || root_taxon.children.empty?
  if current_level == 1
    content_tag :ul, class: 'widget-shadow', id: 'left-nav' do
      root_taxon.children.map do |taxon|
        css_class = (current_taxon && current_taxon.self_and_ancestors.include?(taxon)) ? 'active' : nil
        content_tag :li, class: css_class do
          link_to(taxon.name, seo_url(taxon), "class" => css_class) +
          if current_taxon && current_taxon.self_and_ancestors.include?(taxon)
            taxons_tree(taxon, current_taxon, max_level - 1, current_level + 1)
          end
        end
      end.join("\n").html_safe
    end
  else
    content_tag :ul do
      root_taxon.children.map do |taxon|
        css_class = (current_taxon && current_taxon.self_and_ancestors.include?(taxon)) ? 'active' : nil
        content_tag :li, class: css_class do
          link_to(taxon.name, seo_url(taxon), "class" => css_class) +
          if current_taxon && current_taxon.self_and_ancestors.include?(taxon)
            taxons_tree(taxon, current_taxon, max_level - 1, current_level + 1)
          end
        end
      end.join("\n").html_safe
    end
  end
end

我必须同时进行不同的修改(根级别的ul必须具有与之关联的id和类)。可能有一种更简单的方法,但它有效!

无论如何,我只是使用相同的if语句决定是否应该激活li以决定是否递归调用taxons_tree函数。

我还以为我会在这里留下这个问题。以防其他人像我这样的白痴。