Search code examples
javascriptjquerymagento-1.5magento

How to keep current category tab remain open using ddaccordian.init?


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.


Solution

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