Magento Enterprise Tabs - How to select specific tab in link?

北城以北 提交于 2019-12-25 03:53:50

问题


I am trying to link to a specific tab in Magento Enterprise. It seems that all of the answers I've found don't apply well to their method. I just need a link to the page to also pull up a specific tab. This is the code they use:

Enterprise.Tabs = Class.create();
Object.extend(Enterprise.Tabs.prototype, {
 initialize: function (container) {
    this.container = $(container);
    this.container.addClassName('tab-list');
    this.tabs = this.container.select('dt.tab');
    this.activeTab = this.tabs.first();
    this.tabs.first().addClassName('first');
    this.tabs.last().addClassName('last');
    this.onTabClick = this.handleTabClick.bindAsEventListener(this);
    for (var i = 0, l = this.tabs.length; i < l; i ++) {
        this.tabs[i].observe('click', this.onTabClick);
    }
    this.select();
},
handleTabClick: function (evt) {
    this.activeTab = Event.findElement(evt, 'dt');
    this.select();
},
select: function () {
    for (var i = 0, l = this.tabs.length; i < l; i ++) {
        if (this.tabs[i] == this.activeTab) {
            this.tabs[i].addClassName('active');
            this.tabs[i].style.zIndex = this.tabs.length + 2;
            /*this.tabs[i].next('dd').show();*/
            new Effect.Appear (this.tabs[i].next('dd'), { duration:0.5 });
            this.tabs[i].parentNode.style.height=this.tabs[i].next('dd').getHeight() + 15 + 'px';
        } else {
            this.tabs[i].removeClassName('active');
            this.tabs[i].style.zIndex = this.tabs.length + 1 - i;
            this.tabs[i].next('dd').hide();
        }
    }
}
});

Anyone have an idea?


回答1:


I would consider modifying how the class starts up.

initialize: function (container) {
    this.container = $(container);
    this.container.addClassName('tab-list');
    this.tabs = this.container.select('dt.tab');
// change starts here //
    var hashTab = $(window.location.hash.slice(1));
    this.activeTab = ( this.tabs.include(hashTab) ? hashTab : this.tabs.first());
// change ends here //
    this.tabs.first().addClassName('first');
    this.tabs.last().addClassName('last');
    this.onTabClick = this.handleTabClick.bindAsEventListener(this);
    for (var i = 0, l = this.tabs.length; i < l; i ++) {
        this.tabs[i].observe('click', this.onTabClick);
    }
    this.select();
}

Here, I have only changed how the initial tab is chosen. It checks for an URL fragment which is commonly known as a hash, if that identifies one of the tabs it is preselected. As a bonus the browser will also scroll to that element if possible.

Then you only need to append the tab's ID to the URL. For example you might generate the URL by;

$productUrl = Mage::getUrl('catalog/product/view', array(
    'id' => $productId,
    '_fragment' => 'tab_id',
));



回答2:


If you've recently migrated from an earlier Magento release, e.g. from Enterprise 1.11 to Enterprise 1.12, make sure the javascript in /template/catalog/product/view.phtml right after the foreach that generates the tabs gets updated to the 1.12 version:

<script type="text/javascript">
        var collateralTabs = new Enterprise.Tabs('collateral-tabs');
        Event.observe(window, 'load', function() {
            collateralTabs.select();
        });
</script>

surfimp's VERY helpful suggestions did not produce the desired opening of the closed tab otherwise. Once this updated javascript was added, clicking on a link to read Review or Add Your Review on the product page, jumped to the Reviews tab, even if the tab had been hidden.




回答3:


Similar to Zifius' answer, you can modify the initialize function to just take another argument which will be the active tab.

Event.observe(window, 'load', function() {
    new Enterprise.Tabs('collateral-tabs', $('tab_review'));
});

and then in the scripts.js (or wherever this class may exist for you)

initialize: function (container, el) {
    ...
    this.activeTab = el;
    ...
}

Use whatever logic in the template you like to set 'el' to the desired value.

The reason I did it this way is because when I used Zifius' method, the desired tab would be the active tab, but the default tab's content was still displayed.




回答4:


Had the same task yesterday and as I don't know about prototype much I solved it by adding another method:

selectTab: function (element) {
    this.activeTab = element;
    this.select();
},

Usage:

var Tabs = new Enterprise.Tabs('collateral-tabs');
Tabs.selectTab($('tabId'));

Would like to know if it's a correct approach



来源:https://stackoverflow.com/questions/6526527/magento-enterprise-tabs-how-to-select-specific-tab-in-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!