Juitabs

Hi, i am using CJuiTab for listing some static data. Now tabs contains more items and after the width of the tab division, tab items are listed in rows. i want add a more button to the right of tab menu for showing other tabs.

Now tabs looks like this


StaticTab1 | StaticTab2 | StaticTab3 | StaticTab4

StaticTab5 | StaticTab6 | StaticTab7 | StaticTab8



i want to show like this


StaticTab1 | StaticTab2 | StaticTab3 | StaticTab4 | more

this is the code




 

<?php 

$this->widget('zii.widgets.jui.CJuiTabs',array(

    'tabs'=>array(

        'StaticTab 1'=>'Content for tab 1',

        'StaticTab 2'=>'Content for tab 2',

        'StaticTab 3'=>'Content for tab 3',

        'StaticTab 4'=>'Content for tab 4',

        'StaticTab 5'=>'Content for tab 5',

        'StaticTab 6'=>'Content for tab 6',

        'StaticTab 7'=>'Content for tab 7',                

    ),    

    'options'=>array(

        'collapsible'=>true,

    ),

));

?>

I was intrigued by your suggestion, so I’ve put together a slightly hacky solution:




<script type="text/javascript">

    $(document).ready(function(){

        $('body').on('click', '.ui-tabs .ui-tabs-nav .view-more-link', function(){

            var moreTab = $(this).closest('.view-more');

            var tabContainer = moreTab.closest('.ui-tabs-nav');

            moreTab.remove();

            tabContainer.find('li').show();

            return false;

        });


        $(this).find('.ui-tabs .ui-tabs-nav').each(function(){

            var tabContainer = $(this);

            var tabContainerWidth = tabContainer.width() - 80;

            var cumulativeTabWidth = 0;


            tabContainer.find('li').each(function(){

                cumulativeTabWidth += $(this).width();


                if (cumulativeTabWidth > tabContainerWidth)

                    $(this).hide();

                });


                if (cumulativeTabWidth > tabContainerWidth)

                {

                    var moreContainer = $('<li>').addClass('view-more')

                            .css({float:'right'});

                    moreContainer.append($('<a>').addClass('view-more-link')

                            .attr('href', '#').text('More'));

                    tabContainer.append(moreContainer);

                }

            });

        }

    });

</script>



I’m not certain that the tabs will have been created at the point that this is called. If not, you could call the second part after a short timeout.

See the screen shots below.

5382

more-before.png

5383

more-after.png

Thanks for your rply. This script i added in view but there is no change in the view. Where is the divisions for class view more? any other divisions required?

Thanks

I suspect it’s running before the tabs have loaded. You could probably get it working properly by registering the javascript using Yii::app()->clientScript->registerScript() after the widget has been created.

For a quick fix, check whether it is an ordering issue by wrapping the code in a timeout, something like this:




<script type="text/javascript">

    $(document).ready(function(){

        $('body').on('click', '.ui-tabs .ui-tabs-nav .view-more-link', function(){

            var moreTab = $(this).closest('.view-more');

            var tabContainer = moreTab.closest('.ui-tabs-nav');

            moreTab.remove();

            tabContainer.find('li').show();

            return false;

        });


        window.setTimeout(function(){

            $(this).find('.ui-tabs .ui-tabs-nav').each(function(){

                var tabContainer = $(this);

                var tabContainerWidth = tabContainer.width() - 80;

                var cumulativeTabWidth = 0;


                tabContainer.find('li').each(function(){

                    cumulativeTabWidth += $(this).width();


                    if (cumulativeTabWidth > tabContainerWidth)

                        $(this).hide();

                    });


                    if (cumulativeTabWidth > tabContainerWidth)

                    {

                        var moreContainer = $('<li>').addClass('view-more')

                                .css({float:'right'});

                        moreContainer.append($('<a>').addClass('view-more-link')

                                .attr('href', '#').text('More'));

                        tabContainer.append(moreContainer);

                    }

                });

            }

        }, 100);

    });

</script>



I’m not sure what else you were asking, but there’s no additional code required to create the functionality I showed in the screenshots.

Hey, the more button not a tab in my tab menu. its displayed when tabs with exceeds the division width and i would like to show all other tabs by clicking more button.- (in a scroll wise or any other format)

Thanks.

See my screen shots in the first post…

i added the script in my view file but there in no change.

Ah, the context is wrong in the timeout. Try this:




<script type="text/javascript">

    $(document).ready(function(){

        $('body').on('click', '.ui-tabs .ui-tabs-nav .view-more-link', function(){

            var moreTab = $(this).closest('.view-more');

            var tabContainer = moreTab.closest('.ui-tabs-nav');

            moreTab.remove();

            tabContainer.find('li').show();

            return false;

        });


        window.setTimeout(function(){

            $('.ui-tabs .ui-tabs-nav').each(function(){

                var tabContainer = $(this);

                var tabContainerWidth = tabContainer.width() - 80;

                var cumulativeTabWidth = 0;


                tabContainer.find('li').each(function(){

                    cumulativeTabWidth += $(this).width();


                    if (cumulativeTabWidth > tabContainerWidth)

                        $(this).hide();

                    });


                    if (cumulativeTabWidth > tabContainerWidth)

                    {

                        var moreContainer = $('<li>').addClass('view-more')

                                .css({float:'right'});

                        moreContainer.append($('<a>').addClass('view-more-link')

                                .attr('href', '#').text('More'));

                        tabContainer.append(moreContainer);

                    }

                });

            }

        }, 100);

    });

</script>



Given that you’ll need to be able to understand this code when a bug is inevitably found, you should try to understand it and add the extra functionality yourself.

The first part handles the click on the More link, the second part handles the initial hiding of the tabs. You should be able to use the code I’ve given you to add the extra link that you need.

If you need more help after attempting a solution, post your code here and I (or someone else) will try to help you.

Here i’m using nested JuiTabs and when i insert the more tab script, it shows some problems. On page load it works fine, but when i click 2nd tab in index.php the content shows more button only. The screen shots are below. How can avoid it?

Its my code

index.php




<?php 

$this->widget('zii.widgets.jui.CJuiTabs',array(

    'tabs'=>array(

        'Render 1'=>array('id'=>'test-ids','content'=>$this->renderPartial(

                                        'success',

                                        array('grp'=>'1'),TRUE)),

        'Render 2'=>array('id'=>'test-id','content'=>$this->renderPartial(

                                        'success',

                                        array('grp'=>'2'),TRUE)),

    ),    

    'options'=>array(

        'collapsible'=>true,

    ),

));

?>



success.php




<?php echo $grp; ?>


<script type="text/javascript">

    $(document).ready(function(){

        $('body').on('click', '.ui-tabs .ui-tabs-nav .view-more-link', function(){

            var moreTab = $(this).closest('.view-more');

            var tabContainer = moreTab.closest('.ui-tabs-nav');

            moreTab.remove();

            tabContainer.find('li').show();

            return false;

        });


        window.setTimeout(function(){

            $('.ui-tabs .ui-tabs-nav').each(function(){

                var tabContainer = $(this);

                var tabContainerWidth = tabContainer.width() - 80;

                var cumulativeTabWidth = 0;


                tabContainer.find('li').each(function(){

                    cumulativeTabWidth += $(this).width();


                    if (cumulativeTabWidth > tabContainerWidth)

                        $(this).hide();

                    });


                    if (cumulativeTabWidth > tabContainerWidth)

                    {

                        var moreContainer = $('<li>').addClass('view-more')

                                .css({'float':'right'});

                        moreContainer.append($('<a>').addClass('view-more-link')

                                .attr('href', '#').text('More'));

                        tabContainer.append(moreContainer);

                    }

                });

            }

        , 100);

    });

</script>

<div style="width: 400px">

<?php 

$this->widget('zii.widgets.jui.CJuiTabs',array(

    'tabs'=>array(

        'StaticTab 1'=>'Content for tab 1',

        'StaticTab 2'=>'Content for tab 2',

        'StaticTab 3'=>'Content for tab 3',

        'StaticTab 4'=>'Content for tab 4',

    ),    

    'options'=>array(

        'collapsible'=>true,

    ),

));

?>

</div>

Cleaner version:




<script type="text/javascript">

	$(document).ready(function(){

		$('body').on('click', '.ui-tabs .ui-tabs-nav .view-more-link', function(){

			var moreTab = $(this).closest('.view-more');

			var tabContainer = moreTab.closest('.ui-tabs-nav');

			moreTab.remove();

			tabContainer.find('li').show();

			return false;

		});


		$("body").on("tabscreate", function(event, ui){

			hideTabs(event.target);

		});


		$("body").on("tabsactivate", function(event, ui){

			$(ui.newPanel).find('.ui-tabs').each(function(){hideTabs(this);});

		});

	});


	function hideTabs(tabWidgetElem)

	{

		var tabWidget = $(tabWidgetElem);


		// Only remove tabs once

		if (tabWidget.hasClass('tabs-removed'))

			return;


		var tabContainer = tabWidget.find(".ui-tabs-nav");

		var tabHeight = tabContainer.find("li:first").height();


		// Only add 'more' link if tabs have already wrapped

		if (tabContainer.height() > tabHeight * 2)

		{

			var moreContainer = $('<li>').addClass('view-more')

					.css({float:'right'});

			moreContainer.append($('<a>').addClass('view-more-link')

					.attr('href', '#').text('More'));

			tabContainer.append(moreContainer);

			tabWidget.addClass('tabs-removed');

		}


		// While tabs are taking more than one line, hide the last visible tab

		while (tabContainer.height() > tabHeight * 2)

			tabContainer.find('li:not(.view-more):visible:last').hide();

	}

</script>



Demonstrated here (resize the result pane and hit Run).