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

时间:2011-11-22 13:46:16

标签: magento

我想以树形格式显示我的magento网站的类别及其子类别,就像jquery树一样。 当我点击根类别时会显示其子类别,然后当我点击其中一个子类别时,应显示其产品。

组别
-subcategory1
-subcategory2

类别2 -subcategory1
-subcategory2

单击根类别时将显示子类别。我该怎么做?有什么想法吗?

EDITED
这是我的代码

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

2 个答案:

答案 0 :(得分:0)

您可以在前端使用ExtJs组件,而不是在无冲突中使用jquery:

http://www.magentocommerce.com/boards/viewthread/4139/

这是ExtJs树的例子:

http://dev.sencha.com/deploy/ext-4.0.0/examples/tree/treegrid.html

如果您想使用管理员中使用的工具包Magento,显然有一些书籍工作。

答案 1 :(得分:0)

可能这可以帮到你

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

在上面的代码中,您将获得相同模式的类别。
添加了子类别以使其免于怀疑。
根据您的要求进行自定义