Strict Standards: Only variables should be passed by reference in /home/kilbot/public_html/beta/index.php on line 140
Add/remove tabs using jquery | kilbot labs

Add/remove tabs using jquery

This is a user interface I created to organise input into a content management system. The user is able to select any number of tabs to view (in this example 0 to 5), the tabs use AJAX to draw data, forms etc. At it’s core is the fantastic jQuery library  with UI Tabs, the tweak I made was to use a hidden dummy list so that the tabs can be added and removed in the correct index order. It may not be the most elegant solution, but it’s a relatively easy hack to get the desired UI effect.

15/3/2010: Please note, this is no longer compatible with the latest jQuery. I’m sure there is a much cleaner solution available but I will leave this here ... for posterity.

Demo

Click for working Demo

The CSS

<style>
#add-tabs {
	padding: 5px;
	border: 1px solid #519e2d;
	margin-bottom: 20px;
}

/* Caution! Ensure accessibility in print and other media types… */
@media projection, screen { /* Use class for showing/hiding tab content, so that visibility can be better controlled in different media types… */
    .ui-tabs-hide {
        display: none !important;
    }
}

/* Hide useless elements in print layouts… */
@media print {
    .ui-tabs-nav {
        display: none;
    }
}

/* Skin */
.ui-tabs-nav {
    list-style: none;
    margin: 0;
    padding: 0 0 0 3px;
}
.ui-tabs-nav:after { /* clearing without presentational markup, IE gets extra treatment */
    display: block;
    clear: both;
    content: " ";
}
.ui-tabs-nav li {
    float: left;
    margin: 0 0 0 2px;
    font-weight: bold;
}
.ui-tabs-nav a, .ui-tabs-nav a span {
    float: left; /* fixes dir=ltr problem and other quirks IE */
    padding: 0 12px;
    background: #519e2d;
}
.ui-tabs-nav a {
    margin: 5px 0 0; /* position: relative makes opacity fail for disabled tab in IE */
    padding-left: 0;
    background-position: 100% 0;
    text-decoration: none;
    white-space: nowrap; /* @ IE 6 */
    outline: 0; /* @ Firefox, prevent dotted border after click */    
}
.ui-tabs-nav a:link, .ui-tabs-nav a:visited {
    color: #fff;
}
.ui-tabs-nav .ui-tabs-selected a {
    position: relative;
    top: 1px;
    z-index: 2;
    margin-top: 0;
    background-position: 100% -23px;
}
.ui-tabs-nav a span {
    padding-top: 1px;
    padding-right: 0;
    height: 20px;
    background-position: 0 0;
    line-height: 20px;
}
.ui-tabs-nav .ui-tabs-selected a span {
    padding-top: 0;
    height: 27px;
    background-position: 0 -23px;
    line-height: 27px;
}
.ui-tabs-nav .ui-tabs-selected a:link, .ui-tabs-nav .ui-tabs-selected a:visited,
.ui-tabs-nav .ui-tabs-disabled a:link, .ui-tabs-nav .ui-tabs-disabled a:visited { /* @ Opera, use pseudo classes otherwise it confuses cursor… */
    cursor: text;
}
.ui-tabs-nav a:hover, .ui-tabs-nav a:focus, .ui-tabs-nav a:active,
.ui-tabs-nav .ui-tabs-unselect a:hover, .ui-tabs-nav .ui-tabs-unselect a:focus, .ui-tabs-nav .ui-tabs-unselect a:active { /* @ Opera, we need to be explicit again here now… */
    cursor: pointer;
}
.ui-tabs-disabled {
    opacity: .4;
    filter: alpha(opacity=40);
}
.ui-tabs-nav .ui-tabs-disabled a:link, .ui-tabs-nav .ui-tabs-disabled a:visited {
    color: #000;
}
.ui-tabs-panel {
    border: 1px solid #519e2d;
    padding: 10px;
    background: #fff; /* declare background color for container to avoid distorted fonts in IE while fading */
}
/*.ui-tabs-loading em {
    padding: 0 0 0 20px;
    background: url(loading.gif) no-repeat 0 50%;
}*/

/* Additional IE specific bug fixes… */
* html .ui-tabs-nav { /* auto clear @ IE 6 & IE 7 Quirks Mode */
    display: inline-block;
}
*:first-child+html .ui-tabs-nav  { /* auto clear @ IE 7 Standards Mode - do not group selectors, otherwise IE 6 will ignore complete rule (because of the unknown + combinator)... */
    display: inline-block;
}

.hide,
#dummy-0,
#dummy-1,
#dummy-2,
#dummy-3,
#dummy-4 {
	display: none !important;
}
</style>

↑ Top

The JavaScript

<script type="text/javascript" src="js/jquery.js?ver=1.2.6"></script>
<script type="text/javascript" src="js/ui.core.js?ver=1.5.3"></script>
<script type="text/javascript" src="js/ui.tabs.js"></script>

<script>
$(document).ready(function(){
	$("#example > ul").tabs();
	$(".toggle-modules input:checkbox").click(function() {
		$(this).blur();
		// get the checkbox index
		var module = $(".toggle-modules input:checkbox").index($(this));
		if($(this).is(':checked')) {
			$("#example > ul").tabs("remove", module); // remove dummy tab
			$("#example > ul").tabs("add", 'index.php?return=Content for '+this.value, this.value, module); // add tab and content
			$("#example > ul").tabs("select", module); // select the new tab
		}
		else {
			$("#example > ul").tabs("remove", module); // remove the tab
			$("#example > ul").tabs("add", '#dummy-'+module, '', module); // add the dummy tab
			$($("#example > ul").children("li")[module]).addClass("hide"); // hide the dummy tab
			
			//any checked checkboxes?
			var val = [];
			$(".toggle-modules input:checkbox:checked").each(function(i){
				val[ i ] = $(".toggle-modules input:checkbox").index($(this));
			});
			if(val[0]==undefined) {
				$("#example > ul").tabs("select", 0); // select the first dummy tab?
			} else {
				$("#example > ul").tabs("select", val[0]); // select the first checked tab
			}
		}
	});	
});
</script>

↑ Top


blog comments powered by Disqus