如何以树形形式显示类别和子类别?
我想以树格式显示我的 magento 网站的类别及其子类别,就像 jquery 树一样。 当我单击根类别时,会显示其子类别,然后当我单击其中一个子类别时,应显示其产品。
类别1
-子类别1
-subcategory2
类别2
-子类别1
-subcategory2
当我单击根类别时,将出现子类别。我该怎么做?有什么想法吗?
已编辑
这是我的代码
<?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; ?>
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
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; ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在前端使用 ExtJs 组件,而不是在无冲突的情况下使用 jquery:
http: //www.magentocommerce.com/boards/viewthread/4139/
这是 ExtJs 树示例:
http://dev.sencha.com/deploy/ext-4.0 .0/examples/tree/treegrid.html
显然,如果您想使用管理中使用的 Magento 工具包,那么您需要做一些书本工作。
You can use the ExtJs component on the frontend rather than get into using jquery in no-conflict:
http://www.magentocommerce.com/boards/viewthread/4139/
Here is the ExtJs tree example:
http://dev.sencha.com/deploy/ext-4.0.0/examples/tree/treegrid.html
Clearly there is some book work ahead of you if you want to go with the toolkit Magento used in admin.
也许这可以帮助您
在上面的代码中,您将获得相同模式的类别。
添加子子类别以使其毫无疑问。
根据您的要求自定义它
May be this can help you
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