wlaedimir wlaedimir - 26 days ago 16
PHP Question

CodeIgniter + Bootstrap tabs, reload won't return to last active tab

this is my first question in here, but I've been lurking for long. I hope I can make it clear.

It happens that I'm developing an CI application, with Bootstrap 3 + tabs, and after I make a "form submit" the reload takes me to the first tab, instead of the last active tag.

<ul class="nav nav-tabs" id="tabs">
<li class="active"><a href="#accion" data-toggle="tab">Acción</a></li>
<li><a href="#observaciones" data-toggle="tab">Observaciones</a></li>
<li><a href="#adjuntos" data-toggle="tab">Adjuntos</a></li>
</ul>


That's how my tabs are built, and the controller has a redirect() like this:

redirect(base_url().'/responderconsulta/index/'.$consulta_id.'#observaciones');


I haven't found a way to correct this. I hope I can get some help.

Thanks in advance.

Answer

Thanks to @Paul Zepernick, in his comment he mentioned this post where this code makes this easy breezy.

$(document).ready(function() {
if(location.hash) {
    $('a[href=' + location.hash + ']').tab('show');
}
  $(document.body).on("click", "a[data-toggle]", function(event) {
      location.hash = this.getAttribute("href");
  });
});
$(window).on('popstate', function() {
  var anchor = location.hash || $("a[data-toggle=tab]").first().attr("href");
  $('a[href=' + anchor + ']').tab('show');
});

Thanks to all for you help, I really appreciate it!

Comments