如何使用 ddaccordian.init 保持当前类别选项卡保持打开状态?

发布于 2024-12-21 23:00:28 字数 4360 浏览 2 评论 0原文

我有类别列表,有些有子类别,有些有子子类别,其方式如下:

Main menu
   sub menu
      sub menu

我使用< a href="http://www.dynamicdrive.com/dynamicindex17/ddaccordion.htm" rel="nofollow">ddaccordian.init 来监视打开关闭事件。一切正常,但现在我想要的是当前选定的 URL 类别必须保持打开状态。

我尝试了以下代码:

<script type="text/javascript">
ddaccordion.init({
    headerclass: "question", //Shared CSS class name of headers group
    contentclass: "theanswer", //Shared CSS class name of contents group
    revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
    mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
    collapseprev: false, //Collapse previous content (so only one open at any time)? true/false
    defaultexpanded: [], //index of content(s) open by default [index1, index2, etc]. [] denotes no content.
    onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
    animatedefault: false, //Should contents open by default be animated into view?
    persiststate: false, //persist state of opened contents within browser session?
    toggleclass: ["closedanswer", "openanswer"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
    //togglehtml: ["prefix", "<img src='images/plus.gif' /> ", "<img src='images/minus.gif' />"], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
    animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
    oninit:function(expandedindices){ //custom code to run when headers have initalized
        //do nothing
    },
    onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
        //do nothing
    }
})
</script>

要激活当前类别,即必须保持打开状态,我这样做了:

<a <?php if($_category->hasChildren()): ?>class="question"<?php endif; ?><?php if ($currentUrl == $this->getCategoryUrl($_category)): ?> class="current"<?php endif; ?> href="<?php echo $this->getCategoryUrl($_category) ?>">
    <?php echo $_category['name']; ?>
</a>

<?php $potential1 = $_category->hasChildren(); ?>
<?php if($_category->hasChildren()): ?>
    <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
    <?php $_subcategories = $_category->getChildrenCategories() ?>
    <ul <?php if($_subcategories): ?>class="theanswer"<?php endif; ?>>
    <?php foreach($_subcategories as $subcat): ?>
        <li>
            <a <?php if($subcat->hasChildren()): ?>class="question"<?php endif; ?><?php if ($currentUrl == $this->getCategoryUrl($subcat)): ?> class="current"<?php endif; ?> href="<?php echo $this->getCategoryUrl($subcat); ?>">
                <?php echo $subcat->getName(); ?>
            </a>
            <?php if($subcat->hasChildren()): ?>
            <?php
                $_subcat = Mage::getModel('catalog/category')->load($subcat->getId());
                $childrens = $_subcat->getChildrenCategories();
            ?>
            <ul <?php if($childrens): ?>class="theanswer"<?php endif; ?>>
            <?php foreach($childrens as $_childrens): ?>
                <li>
                    <a <?php if ($currentUrl == $this->getCategoryUrl($_childrens)): ?> class="current"<?php endif; ?>href="<?php echo $this->getCategoryUrl($_childrens); ?>">
                        <?php echo $_childrens->getName(); ?>
                    </a>
                </li>
            <?php endforeach; ?>
            </ul>
            <?php endif ?>
        </li>
    <?php endforeach ?>
    </ul>
<?php endif ?>
</li>

但我在前端没有得到任何响应:(如果您认为我缺少 foreach 循环等,请不要介意。
我刚刚粘贴了部分代码,所以请告诉我哪里错了:(
虽然我正确地获取了当前的 URL。

I have list of categories, some have sub-category and some have sub-sub-category which are in such a way:

Main menu
   sub menu
      sub menu

I used ddaccordian.init to watch for open close events. All works fine, But now what I want is that current selected URL category must remain open.

I have tried the following code:

<script type="text/javascript">
ddaccordion.init({
    headerclass: "question", //Shared CSS class name of headers group
    contentclass: "theanswer", //Shared CSS class name of contents group
    revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
    mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
    collapseprev: false, //Collapse previous content (so only one open at any time)? true/false
    defaultexpanded: [], //index of content(s) open by default [index1, index2, etc]. [] denotes no content.
    onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
    animatedefault: false, //Should contents open by default be animated into view?
    persiststate: false, //persist state of opened contents within browser session?
    toggleclass: ["closedanswer", "openanswer"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
    //togglehtml: ["prefix", "<img src='images/plus.gif' /> ", "<img src='images/minus.gif' />"], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
    animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
    oninit:function(expandedindices){ //custom code to run when headers have initalized
        //do nothing
    },
    onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
        //do nothing
    }
})
</script>

and to get the current category activated i.e. must remain open I did this:

<a <?php if($_category->hasChildren()): ?>class="question"<?php endif; ?><?php if ($currentUrl == $this->getCategoryUrl($_category)): ?> class="current"<?php endif; ?> href="<?php echo $this->getCategoryUrl($_category) ?>">
    <?php echo $_category['name']; ?>
</a>

<?php $potential1 = $_category->hasChildren(); ?>
<?php if($_category->hasChildren()): ?>
    <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
    <?php $_subcategories = $_category->getChildrenCategories() ?>
    <ul <?php if($_subcategories): ?>class="theanswer"<?php endif; ?>>
    <?php foreach($_subcategories as $subcat): ?>
        <li>
            <a <?php if($subcat->hasChildren()): ?>class="question"<?php endif; ?><?php if ($currentUrl == $this->getCategoryUrl($subcat)): ?> class="current"<?php endif; ?> href="<?php echo $this->getCategoryUrl($subcat); ?>">
                <?php echo $subcat->getName(); ?>
            </a>
            <?php if($subcat->hasChildren()): ?>
            <?php
                $_subcat = Mage::getModel('catalog/category')->load($subcat->getId());
                $childrens = $_subcat->getChildrenCategories();
            ?>
            <ul <?php if($childrens): ?>class="theanswer"<?php endif; ?>>
            <?php foreach($childrens as $_childrens): ?>
                <li>
                    <a <?php if ($currentUrl == $this->getCategoryUrl($_childrens)): ?> class="current"<?php endif; ?>href="<?php echo $this->getCategoryUrl($_childrens); ?>">
                        <?php echo $_childrens->getName(); ?>
                    </a>
                </li>
            <?php endforeach; ?>
            </ul>
            <?php endif ?>
        </li>
    <?php endforeach ?>
    </ul>
<?php endif ?>
</li>

But I am getting no response on frontend :( Never mind if you think I am missing a foreach loop etc.
I have just pasted a portion of the code so tell me where I am wrong :(
Although i am getting the current URL correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

猫烠⑼条掵仅有一顆心 2024-12-28 23:00:28

您选择的手风琴插件似乎没有保持部分打开的选项,因此您必须自己拦截并停止它。它提供的唯一可能的事件是 onopenclose 但它被称为 after slideUp已经完成了,没用。

您应该选择另一个满足您需求的插件。


幸运的是,Magento 已经附带了 Prototype 和它自己的折叠类,您可以替换或 wrap 这是 sectionClicked 方法并在那里停止事件。

Accordion.prototype.sectionClicked = function(event) {
    var element = $(Event.element(event)).up('.section');
    if (!element.hasClassName('current')) {
        this.openSection(element);
    }
    Event.stop(event);
};

The accordion plugin you've chosen doesn't appear to have an option for keeping a section open so you would have to intercept that yourself and stop it. The only possible event it provides is onopenclose but that is called after slideUp has finished, which is useless.

You should pick another plugin which satisfies your needs.


Luckily Magento already comes with Prototype and it's own accordion class, you can replace or wrap it's sectionClicked method and stop the event there.

Accordion.prototype.sectionClicked = function(event) {
    var element = $(Event.element(event)).up('.section');
    if (!element.hasClassName('current')) {
        this.openSection(element);
    }
    Event.stop(event);
};
木森分化 2024-12-28 23:00:28

这解决了我的问题,我所做的是添加这个
isCategoryActive($_category)): ?>
而不是
if($currentUrl == $this->getCategoryUrl($_category)): ?> 并在底部添加了 Css 类。

<div class="content-links">
                                <ul>
                    <?php  $i=0; foreach ($catlist as $_category): ?>
                        <?php if($_category->getIsActive()): ?>

                            <li>

                                <a <?php if($_category->hasChildren()): ?>class="question"<?php endif; ?> href="<?php echo $this->getCategoryUrl($_category) ?>">
                                    <?php echo $_category['name']; ?>
                                </a>

                                            <?php $potential1 = $_category->hasChildren(); ?>

                                    <?php if($_category->hasChildren()): ?>

                                        <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                                        <?php $_subcategories = $_category->getChildrenCategories() ?>
                                        <ul class="<?php if($_subcategories): ?>theanswer<?php endif; ?><?php //if($currentUrl == $this->getCategoryUrl($_category)): ?><?php if ($this->isCategoryActive($_category)): ?> opened<?php endif; ?>">

                                            <?php foreach($_subcategories as $subcat): ?>


                                                <li>
                                                <a <?php if($subcat->hasChildren()): ?>class="question"<?php endif; ?> href="<?php echo $this->getCategoryUrl($subcat); ?>">
                                                    <?php echo $subcat->getName(); ?>
                                                </a>
                                                <?php if($subcat->hasChildren()): ?>

                                                    <?php
                                                        $_subcat = Mage::getModel('catalog/category')->load($subcat->getId());
                                                        $childrens = $_subcat->getChildrenCategories();
                                                    ?>
                                                        <ul class="<?php if($childrens): ?>theanswer<?php endif; ?><?php if ($this->isCategoryActive($_subcat)): ?> opened<?php endif; ?>">
                                                    <?php   foreach($childrens as $_childrens):
                                                    ?>
                                                        <li>
                                                            <a <?php if ($currentUrl == $this->getCategoryUrl($_childrens)): ?>class="current"<?php endif; ?>href="<?php echo $this->getCategoryUrl($_childrens); ?>">
                                                                <?php echo $_childrens->getName(); ?>
                                                            </a>
                                                        </li>
                                                    <?php
                                                        endforeach;
                                                        echo "</ul>";
                                                    ?>
                                                <?php endif ?>
                                                </li>

                                            <?php endforeach ?>
                                        </ul>

                                    <?php endif ?>
                            </li>
                        <?php endif ?>
                    <?php endforeach ?>
                                </ul>
                            </div>
                            </div>

                            <div class="clear" style="height:218px;"></div>
                        </div>
                    </div>
                    <div class="event-bot"></div>
                </div>

            </div> <!--inner right end here-->
<style type="text/css">
.opened { display:block !important;}
</style>

This solved my problem, what i did was added this
<?php if ($this->isCategoryActive($_category)): ?>
instead of
if($currentUrl == $this->getCategoryUrl($_category)): ?> and added Css class at the bottom.

<div class="content-links">
                                <ul>
                    <?php  $i=0; foreach ($catlist as $_category): ?>
                        <?php if($_category->getIsActive()): ?>

                            <li>

                                <a <?php if($_category->hasChildren()): ?>class="question"<?php endif; ?> href="<?php echo $this->getCategoryUrl($_category) ?>">
                                    <?php echo $_category['name']; ?>
                                </a>

                                            <?php $potential1 = $_category->hasChildren(); ?>

                                    <?php if($_category->hasChildren()): ?>

                                        <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                                        <?php $_subcategories = $_category->getChildrenCategories() ?>
                                        <ul class="<?php if($_subcategories): ?>theanswer<?php endif; ?><?php //if($currentUrl == $this->getCategoryUrl($_category)): ?><?php if ($this->isCategoryActive($_category)): ?> opened<?php endif; ?>">

                                            <?php foreach($_subcategories as $subcat): ?>


                                                <li>
                                                <a <?php if($subcat->hasChildren()): ?>class="question"<?php endif; ?> href="<?php echo $this->getCategoryUrl($subcat); ?>">
                                                    <?php echo $subcat->getName(); ?>
                                                </a>
                                                <?php if($subcat->hasChildren()): ?>

                                                    <?php
                                                        $_subcat = Mage::getModel('catalog/category')->load($subcat->getId());
                                                        $childrens = $_subcat->getChildrenCategories();
                                                    ?>
                                                        <ul class="<?php if($childrens): ?>theanswer<?php endif; ?><?php if ($this->isCategoryActive($_subcat)): ?> opened<?php endif; ?>">
                                                    <?php   foreach($childrens as $_childrens):
                                                    ?>
                                                        <li>
                                                            <a <?php if ($currentUrl == $this->getCategoryUrl($_childrens)): ?>class="current"<?php endif; ?>href="<?php echo $this->getCategoryUrl($_childrens); ?>">
                                                                <?php echo $_childrens->getName(); ?>
                                                            </a>
                                                        </li>
                                                    <?php
                                                        endforeach;
                                                        echo "</ul>";
                                                    ?>
                                                <?php endif ?>
                                                </li>

                                            <?php endforeach ?>
                                        </ul>

                                    <?php endif ?>
                            </li>
                        <?php endif ?>
                    <?php endforeach ?>
                                </ul>
                            </div>
                            </div>

                            <div class="clear" style="height:218px;"></div>
                        </div>
                    </div>
                    <div class="event-bot"></div>
                </div>

            </div> <!--inner right end here-->
<style type="text/css">
.opened { display:block !important;}
</style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文