【问题标题】:How to keep current category tab remain open using ddaccordian.init?如何使用 ddaccordian.init 保持当前类别选项卡保持打开状态?
【发布时间】:2011-12-20 08:36:44
【问题描述】:

我有类别的列表,有些有子类别,有些有子子类别,它们是这样的:

Main menu
   sub menu
      sub menu

我使用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。

【问题讨论】:

    标签: javascript jquery magento-1.5 magento


    【解决方案1】:

    您选择的手风琴插件似乎没有保持部分打开的选项,因此您必须自己拦截并停止它。它提供的唯一可能的事件是onopenclose,但在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);
    };
    

    【讨论】:

      【解决方案2】:

      这解决了我的问题,我所做的是添加了这个 &lt;?php if ($this-&gt;isCategoryActive($_category)): ?&gt; 代替 if($currentUrl == $this-&gt;getCategoryUrl($_category)): ?&gt; 并在底部添加了 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>
      

      【讨论】:

        猜你喜欢
        • 2011-09-07
        • 1970-01-01
        • 1970-01-01
        • 2019-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多