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

Written on . Posted in .

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