In jquery-ui 1.9, how do you create new tabs dynamically?

前端 未结 5 1273
情话喂你
情话喂你 2020-12-13 14:51

According to the upgrade guide for jquery-ui 1.9 tabs - http://jqueryui.com/upgrade-guide/1.9/#deprecated-add-and-remove-methods-and-events-use-refresh-method - when adding

相关标签:
5条回答
  • 2020-12-13 15:21

    After each tab addition you have to create a div for showing content like for creating tab number 2

    $("div#tabs").append("<div id='tab"+num_tabs+"'></div>");
    

    check the fiddle http://jsfiddle.net/AJDLt/1/

    0 讨论(0)
  • 2020-12-13 15:22

    It is annoying that the tabs "add" was removed, however jQuery is very easy to extend. Just add your own addTab method.

    e.g. Something like this:

    // jQuery extension method
    $.fn.addTab = function (name) {
        $('ul', this).append('<li><a href="#tab-' + name + '">' + name + '</a></li>');
        $(this).append("<div id='tab-" + name + "'></div>");
        $(this).tabs("refresh");
    };
    

    And simply use like this:

    $('#tabs').addTab("NewTab");
    $('#tabs').addTab("Another NewTab");
    $('#tabs').addTab("etc");
    

    JSFiddle: http://jsfiddle.net/TrueBlueAussie/AJDLt/315/

    As suggested by @Andy, you need to make it use .first() if you nest tabs in your interface:

    e.g. Something like this:

    // jQuery extension method
    $.fn.addTab = function (name) {
        $('ul', this).first().append('<li><a href="#tab-' + name + '">' + name + '</a></li>');
        $(this).append("<div id='tab-" + name + "'></div>");
        $(this).tabs("refresh");
    };
    
    0 讨论(0)
  • 2020-12-13 15:28

    The guide must be incomplete, you need to add a tab panel

    $(document).ready(function() {
        $("div#tabs").tabs();
    
        $("button#add-tab").click(function() {
    
            var num_tabs = $("div#tabs ul li").length + 1;
    
            $("div#tabs ul").append(
                "<li><a href='#tab" + num_tabs + "'>#" + num_tabs + "</a></li>"
            );
    $("div#tabs").append(
                "<div id='tab" + num_tabs + "'>#" + num_tabs + "</div>"
            );
            $("div#tabs").tabs("refresh");
        });                    
    });
    

    example

    0 讨论(0)
  • 2020-12-13 15:33

    So, the right way to do this like a top answer but without nub_tabs.

    $("div#tabs").tabs();
    var tab_id = 1;
    $("button#add-tab").click(function() {
        tab_id++; //It can be any id;
    
        $("div#tabs ul").append(
        '<li id="'+tab_id+'"><a href="#tab"'+tab_id+'">#"+tab_id+"</a><i class="close-tab" target="'+tab_id+'"></i></li>');
    $("div#tabs").append(
        '<div id="'+tab_id+'">#'+tab_id+'</div>');
        $("div#tabs").tabs("refresh");
    });
    
    //Now i want to show some functions from my code;
    //It will not work for you, just want show the idea;
    
    //No clone tabs
    
    function if_tab_exist(thisI) { //thisI - contains some id, title and text for tab content.
        var tab_id = $(thisI).attr('id');
        if(!$('.ui-tabs-nav li#'+tab_id).is('*')) {
            addTab(thisI); //Add tab function
        } else {
            var TAB_index = $('.ui-tabs-nav li#'+tab_id).index();
            $("#tabs").tabs({active: TAB_index}); //Will activate already exist tab
        }
    }
    
    //Close tab
    function close_tab(tab_id) {
        $('.ui-tabs-nav i[target='+tab_id+']').parent().remove();       
        $('#tabs div#'+tab_id+']').remove();
        $("#tabs").tabs("refresh");
    }
    //Events
    //CLose
    $("body").on('click','.ui-tabs-nav .ui-state-default i', function() {
        close_tab($(this).attr('target'));
    });
    //Add
    $("body").on('click','.addTab', function() {
        if_tab_exist(this);
    });
    
    0 讨论(0)
  • 2020-12-13 15:40

    An example is described in more details here moreover there is a runnable version is on jquery ui's official site with its source code.

    A simple declaration could be something like the following:

    var addTab = function (tabs, tabId, tabLabel, tabContentHtml) {
         var header = "<li><a href='#" + tabId + "'>" + tabLabel + "</a> </li>"
         tabs.find(".ui-tabs-nav").append(header);
         tabs.append("<div id='" + tabId + "'><p>" + tabContentHtml + "</p></div>");
         tabs.tabs("refresh");
    };
    

    Addition of a new tab is getting easier, just type the following:

    $("#tabs_area").tabs();
    addTab($("#tabs_area"), "tab_id1", "Tab 1", "this is just test");
    
    0 讨论(0)
提交回复
热议问题