Skip to content

Commit a02e393

Browse files
petersendiditscottgonzalez
authored andcommitted
Tabs: update manipulation demo to use the refresh method
1 parent 9bd731d commit a02e393

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

demos/tabs/manipulation.html

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@
2222
</style>
2323
<script>
2424
$(function() {
25-
var $tab_title_input = $( "#tab_title"),
26-
$tab_content_input = $( "#tab_content" );
27-
var tab_counter = 2;
25+
var tab_title_input = $( "#tab_title"),
26+
tab_content_input = $( "#tab_content" ),
27+
tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>Remove Tab</span></li>",
28+
tab_counter = 2;
2829

2930
// tabs init with a custom tab template and an "add" callback filling in the content
30-
var $tabs = $( "#tabs").tabs({
31-
tabTemplate: "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>Remove Tab</span></li>",
31+
var tabs = $( "#tabs").tabs({
3232
add: function( event, ui ) {
33-
var tab_content = $tab_content_input.val() || "Tab " + tab_counter + " content.";
33+
var tab_content = tab_content_input.val() || "Tab " + tab_counter + " content.";
3434
$( ui.panel ).append( "<p>" + tab_content + "</p>" );
3535
}
3636
});
3737

3838
// modal dialog init: custom buttons and a "close" callback reseting the form inside
39-
var $dialog = $( "#dialog" ).dialog({
39+
var dialog = $( "#dialog" ).dialog({
4040
autoOpen: false,
4141
modal: true,
4242
buttons: {
@@ -49,39 +49,46 @@
4949
}
5050
},
5151
open: function() {
52-
$tab_title_input.focus();
52+
tab_title_input.focus();
5353
},
5454
close: function() {
55-
$form[ 0 ].reset();
55+
form[ 0 ].reset();
5656
}
5757
});
5858

5959
// addTab form: calls addTab function on submit and closes the dialog
60-
var $form = $( "form", $dialog ).submit(function() {
60+
var form = $( "form", dialog ).submit(function() {
6161
addTab();
62-
$dialog.dialog( "close" );
62+
dialog.dialog( "close" );
6363
return false;
6464
});
6565

6666
// actual addTab function: adds new tab using the title input from the form above
6767
function addTab() {
68-
var tab_title = $tab_title_input.val() || "Tab " + tab_counter;
69-
$tabs.tabs( "add", "#tabs-" + tab_counter, tab_title );
68+
var label = tab_title_input.val() || "Tab " + tab_counter,
69+
id = "tabs-" + tab_counter,
70+
li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ) ),
71+
tab_content = tab_content_input.val() || "Tab " + tab_counter + " content.";
72+
73+
tabs.find('ul').append( li );
74+
tabs.append( "<div id='" + id + "'><p>" + tab_content + "</p></div>" );
75+
tabs.tabs( "refresh" );
7076
tab_counter++;
7177
}
7278

7379
// addTab button: just opens the dialog
7480
$( "#add_tab" )
7581
.button()
7682
.click(function() {
77-
$dialog.dialog( "open" );
83+
dialog.dialog( "open" );
7884
});
7985

8086
// close icon: removing the tab on click
8187
// note: closable tabs gonna be an option in the future - see http://dev.jqueryui.com/ticket/3924
8288
$( "#tabs span.ui-icon-close" ).live( "click", function() {
83-
var index = $( "li", $tabs ).index( $( this ).parent() );
84-
$tabs.tabs( "remove", index );
89+
$( this ).closest( "li" ).remove();
90+
$( "#" + $( this ).prev().attr( "aria-controls" ) ).remove();
91+
tabs.tabs( "refresh" );
8592
});
8693
});
8794
</script>

0 commit comments

Comments
 (0)