2013-12-17 5 views
1

Я новичок в Spree, поэтому любая помощь очень ценится!Spree - показ подкатегорий только при выборе категории

Я нашел способ заставить Spree показать подкатегории в левом навигаторе (настройка config.max_level_in_taxons_menu = 4), но в этом методе все подкатегории отображаются все время. Смотрите ниже:

В приведенном выше примере, я только хочу, чтобы отобразить «Рубашки и футболки» выбран, когда «Одежда».

Я думаю, мне пришлось бы редактировать функцию taxons_tree в файле base_helper.rb, но я понятия не имею, с чего начать.

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 

Заранее благодарен!

ответ

0

OK - Я думаю, в то время как выписывая вопрос, он получил винтики, работающие в головном мозге, и я понял это:

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 должен был иметь идентификатор и класс, связанные с ним). Вероятно, был более простой способ сделать это, но он работает!

Во всяком случае, я использовал ту же самую инструкцию if, которая решила, должен ли li быть активным, чтобы решить, следует ли рекурсивно вызывать функцию taxons_tree.

Я думал, что оставил бы здесь вопрос в любом случае. На всякий случай кто-то другой идиот, как я.

Смежные вопросы