如何以树格式显示类别和子类别? [英] How to display the categories and subcategories in a tree format?
问题描述
我想以类似于jquery树的树格式显示我的magento网站的类别及其子类别. 当我单击根类别时,将显示其子类别,然后当我单击其中一个子类别时,应显示其产品.
I want to display the categories of my magento website along with its sub categories in a tree format just like the jquery tree. When I click on the root category its sub categories appears and then when I click on one of the sub category, its products should be displayed.
Category1
-subcategory1
-subcategory2
Category1
-subcategory1
-subcategory2
Category2
-subcategory1
-subcategory2
Category2
-subcategory1
-subcategory2
当我单击根类别时,将出现子类别.我应该怎么做?有想法吗?
The subcategories will appear when I click on the root category. How should I do this? Any Idea?
已编辑
这是我的密码
EDITED
Here is my CODE
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<?php echo ' -'; ?>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
推荐答案
也许这可以帮助您
May be this can help you
<div>
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<div id="navigation_vert">
<?php if (count($categories) > 0): ?>
<ul>
<?php foreach($categories as $category): ?>
<li>
<a class="navlink" href="<?php echo $helper->getCategoryUrl($category) ?>"><?php echo $this->escapeHtml($category->getName()) ?></a>
<div id="dropdown" class="dropdown">
<div>
<?php $subcategories = $category->getChildren() ?>
<?php if (count($subcategories) > 0): ?>
<?php $i=0;?>
<?php foreach($subcategories as $subcategory): ?>
<?php $i++;
if($i<3)
{
if($i==1)
{
echo '<div style="float:left;">';
}
?>
<div class="firstlist">
<?php
}
else
{
if($i==3)
{
echo '<div style="float:right;">';
}
echo '<div class="secondlist">';
}
?>
<p>
<strong>
<a href="<?php echo $helper->getCategoryUrl($subcategory) ?>"><?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?></a>
</strong>
</p>
<?php $subsubcategories = $subcategory->getChildren() ?>
<?php if (count($subsubcategories) > 0): ?>
<ul>
<?php foreach($subsubcategories as $subsubcategory): ?>
<li>
<a href="<?php echo $helper->getCategoryUrl($subsubcategory) ?>"><?php echo $this->escapeHtml(trim($subsubcategory->getName(), '- ')) ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?><!-- subsubcategories if e -->
</div><!-- firstlist e -->
<?php
if($i==2 || $i==3)
{
echo "</div>";//div 2 with style e
}
?>
<?php endforeach; ?><!-- subcategories for e -->
<?php endif; ?><!-- subcategories if e -->
</div><!-- div 1 e -->
</div><!-- id dropdown_four e-->
</li>
<?php endforeach; ?><!-- categories e -->
</ul>
<?php endif; ?><!-- count main categories if e -->
</div><!-- id navigation_vert e -->
</div>
在上面的代码中,您将以相同的模式获得类别.
添加了子类别以使其毫无疑问.
根据您的要求进行自定义
In the above code u will get categories in the same pattern.
Added sub-sub-categories to make it doubt free.
Customize it with your requirements
这篇关于如何以树格式显示类别和子类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!