简体   繁体   English

Bootstrap-动态添加的嵌套选项卡

[英]Bootstrap - Dynamically added nested tabs

I just started learning front-end development & really need help with a practice project. 我刚刚开始学习前端开发,并且确实需要实践项目的帮助。

I am trying to create 2-layers nested tabs where the user can add tabs on the outer layer. 我正在尝试创建2层嵌套标签,用户可以在其中添加外层标签。 Each outer tab will have inner tabs where the user can also add more inner tabs. 每个外部标签都将具有内部标签,用户还可以在其中添加更多内部标签。 Each tab will have the same content for now, so I'm using an iframe to accomplish this - if you have better suggestion, then I would very much like to hear it. 每个标签目前都具有相同的内容,因此我正在使用iframe来完成此操作-如果您有更好的建议,那么我非常想听听它。

The issue I'm having right now is that I don't know how to bind the inner tabs to the outer tabs. 我现在遇到的问题是我不知道如何将内部选项卡绑定到外部选项卡。 Each of the outer tabs should have different number of tabs depending on how many inner tabs the user creates. 每个外部选项卡应具有不同数量的选项卡,具体取决于用户创建的内部选项卡的数量。 I cannot seem to accomplish this & all of my outer tabs currently have the same inner tabs. 我似乎无法完成此操作,而且我的所有外部选项卡当前都具有相同的内部选项卡。

Any help/input would be very much appreciated! 任何帮助/输入将不胜感激! Thanks. 谢谢。

Here's my code snippet: 这是我的代码段:

<div class="container">
    <!-- top level tabs --> 
        <ul class="nav nav-tabs" id="topNav">
            <li class="active"><a href="#top1" data-toggle="tab">Home</a><span>x</span></li>
            <li><a href="#" class="addTop" data-toggle="tab">+ Add Tab</a></li>
        </ul>
        <div class="tabbable">
            <!--bottomNav -->
            <ul class="nav nav-tabs" id="bottomNav">
                <li class="active"><a href="#bottom1" data-toggle="tab">Test</a><span>x</span></li>
                <li><a href="#" class="addBottom" data-toggle="tab">+ Add Tab</a></li>
            </ul>

                <div class="tab-content">
                    <div class="tab-pane active" id="bottom1"><br><iframe src="form.html" width="100%" height="60%" allowtransparency="true" frameBorder="0"></iframe></div>
                </div>
            </div>
        </div>   

My script: 我的剧本:

$(document).ready(function() { 

$(".nav-tabs").on("click", "a", function(e){
      e.preventDefault();
       $(this).tab('show');
      })
    .on("click", "span", function () {
        var anchor = $(this).siblings('a');
        $(anchor.attr('href')).remove();
        $(this).parent().remove();
        $(".nav-tabs li").children('a').first().click();
    });
    /* Adding New Tabs */
    $('.addTop').click(function(e) {
        e.preventDefault();
        var id = $("#topNav").children().length; 
        $(this).closest('li').before('<li><a href="#top'+id+'">#'+ id + '</a><span>x</span></li>'); 
        //  $('.tab-content').append('<div class="tab-pane" id="top'+id+'">'+ '<br><iframe src="form.html" width="100%" height="60%" allowtransparency="true" frameBorder="0">' + '</iframe>' + '</div>');

  });
    $('.addBottom').click(function(e) {
        e.preventDefault();
        var id = $("#bottomNav").children().length; 
        $(this).closest('li').before('<li><a href="#bottom'+id+'">#'+ id + '</a><span>x</span></li>');         
        $('.tab-content').append('<div class="tab-pane" id="bottom'+id+'">'+ '<br><iframe src="form.html" width="100%" height="60%" allowtransparency="true" frameBorder="0">' + '</iframe>' + '</div>');

  });
});

There are ready made sollutions out there, but let us leave it for others to suggest. 有现成的解决方案,但让我们将其留给他人提出。

In reply to your issue: Your addTop.onclick event only adds a new tab, but does not create new content.tabbable for the said tab. 回答您的问题:您的addTop.onclick事件仅添加一个新标签,而不会为该标签创建新的content.tabbable。

When you ad a new tab you have to do 3 things: 投放新标签时,您需要做以下三件事:

  1. create a new tab. 创建一个新标签。
  2. create a new content for the new tab. 为新标签创建新内容。
  3. associate event. 关联事件。

Obviously you missed number 2 & 3. 显然,您错过了数字2和3。

Example: 例:

$('.addTop').click(function(e){
    e.preventDefault();

    // create new tab
    var newTab = $('<li>New Tab</li>'); 
    $('#topNav').append(newTab);

    // create new content for the new tab
    var newTabContent = $('<div class="tabbable"></div>').hide();
    $('.container').append(newTabContent);

    // associate tab to content
    newTab.click(function(){
        newTabContent.show();
        // hide others
        $('.container > .tabbable').hide();
    });
});

Hope this could help. 希望这会有所帮助。 This is the solution without using iframe 这是不使用iframe的解决方案

You can see the effect in jsfiddle link below: http://jsfiddle.net/Lzfsgeq9/ 您可以在下面的jsfiddle链接中看到效果: http : //jsfiddle.net/Lzfsgeq9/

Basically some points you need to be aware of are: 基本上,您需要注意以下几点:

1.Use ahref to direct you subtab from parent tab 1,使用ahref从父选项卡引导您的子选项卡

2.Use "on" event listener to deal with newly created element about event delegation issue 2.使用“ on”事件侦听器处理有关事件委托问题的新创建元素

<div class="tabbable boxed parentTabs">
<ul id="topNav" class="nav nav-tabs">
    <li class="active"><a href="#top1">Tab 1</a>
    </li>
    <li><a href="#top2">Tab 2</a>
    </li>
    <li><a href="#" class="addTop" data-toggle="tab">+ Add Tab</a></li>
</ul>
<div class="tab-content">
    <div class="tab-pane fade active in" id="top1">
        <div class="tabbable">
            <ul id="bottomNav1" class="nav nav-tabs" data-parent="1">
                <li class="active"><a href="#bottom11">Tab 11</a>
                </li>
                <li><a href="#bottom12">Tab 12</a>
                </li>
                <li><a href="#" class="addBottom" data-toggle="tab">+ Add Tab</a></li>
            </ul>
        </div>
    </div>
    <div class="tab-pane fade" id="top2">
        <div class="tabbable">
            <ul id="bottomNav2" class="nav nav-tabs" data-parent="2">
                <li class="active"><a href="#bottom21">Tab 21</a>
                </li>
                <li><a href="#bottom22">Tab 22</a>
                </li>
                <li><a href="#" class="addBottom" data-toggle="tab">+ Add Tab</a></li>
            </ul>
        </div>
    </div>
</div>

    $("ul.nav-tabs").on("click","a",function (e) {
            e.preventDefault();  
            $(this).tab('show');
    });

    $('.addTop').click(function(e) {
            e.preventDefault();
            var id = $("#topNav").children().length; 
            $(this).closest('li').before('<li><a href="#top'+id+'">Tab'+ id + '</li>');
            createNewSubTab(id);
    });

    $('div.tab-content').on("click",".addBottom",function(e) {
        e.preventDefault();
        var parentId = $(this).closest('ul').data("parent");
        var id = $("#bottomNav" + parentId).children().length; 
        $(this).closest('li').before('<li><a href="#bottom'+parentId+id+'">Tab'+ parentId + id + '</li>'); 
   });

    var createNewSubTab = function(id){
        var bottomTabPane = $("<div></div>").attr({
            id: "top" + id,
            "class": "tab-pane fade"
        });
        var tabContainer = $("<div></div>").attr({
            "class":"tabbable"
        });
        var bottomPanel = $("<ul></ul>").attr({
                    id:"bottomNav"+id,
                    "class":"nav nav-tabs",
                    "data-parent": id
       });
       var bottomAdd = $("<li></li>").append("<a href='#' class='addBottom' data-toggle='tab'>+ Add Tab</a>");
       bottomTabPane.append(tabContainer);
       tabContainer.append(bottomPanel);
       bottomPanel.append(bottomAdd);
       $(".tab-content").append(bottomTabPane);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM