Chat with us, powered by LiveChat
Home  /  Blog  /  News  /  How to remember tab with a session at Joomla! backend?

How to remember tab with a session at Joomla! backend?

How to remember tab with a session at Joomla! backend?

After I’ve seen different examples on the web (here or here), I’ve found a simple solution by seeing the core components of Joomla!, specifically the “com_content”. After comparing my custom component with “com_content”, I’ve seen that the second loads the system javascript file “/media/system/js/tabs-state.js”, instead of mine that didn’t.

So, you just have to insert the below line of code in the main file of your component. Let’s say that our component’s name is “test”. The main file will be “administrator/components/com_test/test.php”.

Enter the below code after the line of code “defined(‘_JEXEC’) or die;”.

JHtml::_('behavior.tabstate');

This code loads the “/media/system/js/tabs-state.js” which is a javascript behavior to allows the selected tab to be remained after save or page reload keeping the state in our local storage.

You can find a clean example (without bootstrap) to load tabs in your custom Joomla component in the docs: https://docs.joomla.org/J3.x:Using_the_JHtmlTabs_class_in_a_component

or simple you can try our example with bootstrap tabs:

<?php echo JHtml::_('bootstrap.startTabSet', 'myTab', array('active' => 'main1')); ?>
<?php echo JHtml::_('bootstrap.addTab', 'myTab', 'tab1', JText::_('Tab 1')); ?>
<div>Text for Tab 1 goes here...</div>
<?php echo JHtml::_('bootstrap.endTab'); ?>
<?php echo JHtml::_('bootstrap.addTab', 'myTab', 'tab2', JText::_('Tab 2')); ?>
<div>Text for Tab 2 goes here...</div>
<?php echo JHtml::_('bootstrap.endTab'); ?>
<?php echo JHtml::_('bootstrap.addTab', 'myTab', 'tab3', JText::_('Tab 3')); ?>
<div>Text for Tab 3 goes here...</div>
<?php echo JHtml::_('bootstrap.endTab'); ?>
<?php echo JHtml::_('bootstrap.endTabSet'); ?> 

It’s not something new or a clever tip, but hopes it will help many dev colleagues that have same issues with tab sessions in Joomla 3.x series on their custom Joomla! components.

Feel free to contact us if you need help with the above implementation into your custom Joomla! extension.

Best Regards,
Yiannis Christodoulou
Founder & Lead Developer, Web357

Leave a Reply

Your email address will not be published. Required fields are marked *

We are a young team of professionals and internet lovers who specialise in the development of professional websites and premium plugins for WordPress and Joomla! CMS.
Copyright © 2019 Web357. All Rights Reserved.
Joomla! ® name is used under a limited license from Open Source Matters in the United States and other countries.
Web357 is not affiliated with or endorsed by Open Source Matters or the Joomla! Project.