如何以树形形式显示类别和子类别?

发布于 2024-12-17 13:45:19 字数 1831 浏览 1 评论 0原文

我想以树格式显示我的 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 '&nbsp;&nbsp;&nbsp;-'; ?>
                                <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕梦 2024-12-24 13:45:19

您可以在前端使用 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.

城歌 2024-12-24 13:45:19

也许这可以帮助您

<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>

在上面的代码中,您将获得相同模式的类别。
添加子子类别以使其毫无疑问。
根据您的要求自定义它

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文