Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bootstrap/Tabs: remember active tabs #4890

Closed
RomeroMsk opened this issue Sep 1, 2014 · 14 comments
Closed

Bootstrap/Tabs: remember active tabs #4890

RomeroMsk opened this issue Sep 1, 2014 · 14 comments

Comments

@RomeroMsk
Copy link
Contributor

Just a feature request: config option to "remember" active tab in Tabs widget (to activate last opened tab after reloading the page).
It is not so hard to implement with application JS, but can be a feature of widget. We need to register just two event handlers:

  1. Set location.hash on tab click:
$(document).on('click.bs.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
    location.hash = e.target.hash;
});

We need this because of https://github.com/twbs/bootstrap/blob/master/js/tab.js#L126
2. On document ready activate the tab:

$(document).ready(function() {
    if (location.hash.length) {
        $('[data-toggle="tab"], [data-toggle="pill"]').filter('[href="' + location.hash + '"]').tab('show');
    }
}
@SDKiller
Copy link
Contributor

SDKiller commented Sep 1, 2014

Isn't it better to use cookie?

If their are other anchors on page - not related to tabs - and user navigates them - location.hash will be overwritten.

@RomeroMsk
Copy link
Contributor Author

Yes, we can use cookie for this - it is easy too.

@Ragazzo
Copy link
Contributor

Ragazzo commented Sep 2, 2014

usually user storage in browser is used

@RomeroMsk
Copy link
Contributor Author

@Ragazzo, if you mean localStorage then we need to check if it is supported by browser.

@Ragazzo
Copy link
Contributor

Ragazzo commented Sep 2, 2014

all modern browser supports it . Also i saw it already used in Yii2 in some widget or debug module

@RomeroMsk
Copy link
Contributor Author

The key word is modern. Debug module is for devs (who use modern browsers), but not application users. We can degrade to cookies if browser doesn't support localStorage or just make a note in docs about it :)

@Ragazzo
Copy link
Contributor

Ragazzo commented Sep 2, 2014

Can you show what browsers does not support it since it is HTML5 and we also use in Yii2 some HTML5 features and tags ? Also since browsers updates very quickly i dont think it is a problem

@philippfrenzel
Copy link
Contributor

I think you can implement the localStorage by default and if someone wants to be "bullet"browser"proof" you can make a comment about cookie storage

@RomeroMsk
Copy link
Contributor Author

http://caniuse.com/#feat=namevalue-storage
Yes, it is not a big problem.

@samdark samdark added this to the 2.1 milestone Sep 2, 2014
@Sammaye
Copy link

Sammaye commented Sep 2, 2014

Bootstrap 3.0 is only supported on browsers (and works) that support localstorage, you kinda in the wrong business if you are supporting old browsers (except for Opera Mini of couse :).

@Ragazzo
Copy link
Contributor

Ragazzo commented Sep 2, 2014

So as we see here it is not a problem and we should use local storage

@RomeroMsk
Copy link
Contributor Author

As I said earlier: yes, it is not a big problem. We can use localStorage for this, I'm convinced :)

@Forin
Copy link

Forin commented Jan 14, 2016

Workaround until this feature is added to Yii 2.1:
Add this to your view:

<?php
$script = <<< JS
    $(function() {
        //save the latest tab (http://stackoverflow.com/a/18845441)
        $('a[data-toggle="tab"]').on('click', function (e) {
            localStorage.setItem('lastTab', $(e.target).attr('href'));
        });

        //go to the latest tab, if it exists:
        var lastTab = localStorage.getItem('lastTab');

        if (lastTab) {
            $('a[href="'+lastTab+'"]').click();
        }
    });
JS;
$this->registerJs($script, yii\web\View::POS_END);
?>

@yii-bot
Copy link

yii-bot commented Jan 24, 2016

Issue moved to yiisoft/yii2-bootstrap#104

@yii-bot yii-bot closed this as completed Jan 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants