Update Tab content

Hi guys, I have a Yii tab widget that has 3 tabs, each tab will have a submit form. by default after submission the tab resets, and you are always end up in the 1st tab. I managed to use GET to set the correct tab active on page reload. however the tab content doesn’t and stays always at the first tab content (even tab2 is active). Has anyone had that problem before?

Thanks in advance. :(

If I’m not mistaken, you can force, which tab should be active, when you’re building tab widget definition.

I solved similar problem, by writing ID of currently selected tab into cookie upon submiting of a forum and then reading (and setting) it in the code responsible for drawing tabs. It worked fine.

you can also use _GET or _POST instead of cookie

Thanks Trejder, I almost though this is going to be a dead post. I will give this a try today, I am using the Yii->Boostrap widget and I felt I was limited by how the widget works. I am able to get the tab to focus via $_GET & $_POST, however the content remain the what the problem which remained on first tab, but with your suggestion I will certainly try that again! Cheer pal!!

Thansk amiramir, I am already using $_GET to do that, but i think my problem is how I set the Boostrap tab widget,(only the right tab is focused, but the content of the tab is not, always shows the first tab after submission) so I will give that another go. :wink:

Hi, I’m facing same problem actually, I would like to stay on the same tab after page refresh… however it goes on the default tab at left every time…

One of the solution would be to write the actual tab in a cookie, en select it at page loading but don’t really know how to do it… if you have any solution I’ll get it with pleasure =) Or maybe there is an option in cjuitabs to do it like “persist” option in ctreeview widget.

thanks

[edit]

add “‘cookie’=>true,” in options of cjuitabs seems to give the result I waited for… I found it in going to read CJuiTabs.php in framework, however it doesn’t seem to me that I had watched it in documentation

Hi Mate, personally due to requirement change I scrap the tab idea at the end, althoguh I did got it to work at one point. I was using the GET param to store the tab pos, but Cookie will work just fine. I can switch between tabs, but my main issues was the content in the new active tabs won’t load and can only be load via Ajax.

Be warn it’s not very clean :wink:





<script> 


  $(function () {

  	

    $('body').on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {

      e.preventDefault();

      

        $('.nav-tabs').children('li').removeClass('active');

        $('li').removeClass('active');


      	var tabText = e.currentTarget.text;

    

      	if (tabText == 'Add New Company') {

        	$.ajax({

                method:"GET",

                url:  <?php echo '"'.Yii::app()->createAbsoluteUrl('company/createPartial').'"'; ?>,

                dataType: "html",

                success: function(data) {

                    $("#tab-addcompany").html(data);

                }

        	});      

        }      


      //$(this).tab('show')

      

    });

    

  })


<?php $focustab = Yii::app()->getRequest()->getParam('tab'); ?>


$(document).ready(function(e) {

        console.log();

		//For detecting Tabs. 


        $('.nav-tabs').children('li').removeClass('active');

        $('li').removeClass('active');


        var t = <? echo "'".$focustab."';"; ?>

        var t_focus = $('a[href$='+t+']').attr('href');

       // alert($('a[href$='+t+']').attr('href'));


        if (t =='') //No tab selection load default first tab.

        {

            $('.nav-tabs :first-child').addClass('active');

        }else

        {

          // $('.nav-tabs').children('li').removeClass('active');

        //$('.nav-tabs > li').removeClass('active');

            $('li').removeClass('active');

            $('.nav-tabs').find('li').removeClass('active');

           // alert($('.nav-tabs').html());

            $('a[href$='+t_focus+']').parent().addClass('active');

          //  alert(t_focus);

            //alert($('.tab-pane').html());

            //Set the content

            $('.tab-pane').removeClass('active');

            $(t_focus).addClass('active');

           // var tabText = e.currentTarget.text;

        }

   

});



Hi, thanks for answering ;) however I already found solution adding just “‘cookie’=>array(‘expires’=>‘1’,‘path’=>”/")," in CJuiTabs widget “Options”, I got that in JQuery documentation, I’ll try to apply it directly in yii widget’s options and it worked like a charm =D

Thanks for reply =)

I didn’t know, that jQuery supports this! :]

I’m not sure, if you have to use path part. I think expire only should be enough.

How expire is expressed accoring to jQuery documentation? I think it is in days. Therefore using 1 might not be too good idea (at least I think so). For every feature or data that should be stored in my apps in cookies I always use 30 (thirty days). I assume that a user can go for a vacation or sth, so keeping his settings for a few days doesn’t make sense to me.

“‘path’=>’/’” was necessary, without it, it will save tabs’ state for each different page… very bothering when you come back on page visited before.

‘expires’=>‘1’ has no sens for me anyway… but it’s in day I think even if doc doesn’t precise it. But yes, regarding what you want 30 days sticks better with what we can see on website using session and cookie :)

You can set ‘activeTab’ property in the CTabView.

Here is a simple extension that saves the current tab in the cookie and set it as default:




class TabView extends CTabView

{


	public function run()

	{

		$this->activeTab= (isset($_COOKIE[$this->id])? substr($_COOKIE[$this->id], 1) : '');

		Yii::app()->clientScript->registerScript('tabView'.$this->id, "$('#{$this->id} > ul.tabs  a').click( function(){ $.cookie('{$this->id}', $(this).attr('href'), { path: '/admin' });});");

		return parent::run();

	}

	

}



Are you sure, that using only tab id as cookie name is wise and safe?

I always prepend my cookies’ names with anything similar to app name etc. to make sure, that names won’t collide with other things.

I know, that cookies are stored per domain, but even so, most softwares (like phpMyAdmin) are always prepending cookie name with something.

You can prepend whatever you want to the cookie, the code is meant to be an example