简体   繁体   中英

Bootstrap 3 Nav Pills Nav Tabs Dynamically loading / displaying

I have a created a php function, which allows jQuery to create a tab-content div for Bootstrap pills / tabs. I can get it to load the data dynamically, I just cant get it to show the data below. The pills / tabs them selves show fine, its the content that doesnt show once I click on the relevant tab. The contact loads in the desired tab. but doesn't give it an active class.

function BStabs($divId)
{
    ?>
    <script>
    jQuery(document).ready(function()
    {
        var div = '<?php echo $divId;?>';
        var html = "";
        html += '<div class="tab-content">';
        jQuery('#'+div+' ul').each(function()
        {
            jQuery(this).addClass('nav nav-pills');
        });
        jQuery('#'+div+' ul li a').each(function()
        {
            jQuery(this).attr('data-toggle', 'pill');

            var href = $(this).attr("data-target");
            html += '<div id="'+href+'" role="tabpanel" class="tab-pane"></div>';
            jQuery(this).attr('aria-controls',href);
            jQuery(this).attr('role','tab');
        });
        html += '</div>';
        jQuery('#'+div).append(html);

        var first = '#'+div+' ul li:first a';
        var url = $(first).attr("rel");
        var href = $(first).attr("data-target");
        var pane = $(first);
        $('#'+href).load(url,function(result){      
            pane.tab('show');
        });
        jQuery('.nav-pills > li > a').click(function(e)
        {
            var url = jQuery(this).attr("rel");
            var href = jQuery(this).attr("data-target");
            var pane = jQuery(this);
            jQuery('#'+href).load(url,function(result){      
                pane.tab('show');
            });
        });
    });
    </script>
    <?
}

Below is where I call the function:

echo "<div id=\"myMessages\">";
echo "<ul>";
echo "<li><a data-target=\"todos\" rel=\"/URLforAJAXfunction\">A1</a></li>";
echo "<li><a data-target=\"asstodos\" rel=\"/URLforAJAXfunction\">A2</a></li>";
echo "</ul>";
echo "</div>";
BStabs("myMessages");

I have adapted the code mainly from this Bootply http://www.bootply.com/63891

I have also added a JSFiddle link http://jsfiddle.net/9n6qLt0g/ for people to play with if they want. Really need this to work. I want the JS to create the tab content div and all relevant sub divs then become active when clicked on

OK so I have been playing with this a while now. Its a bit of a hack I think, but none the less ive got it working:


JS fiddle if anyone wants to look (improve code) http://jsfiddle.net/daza110/9n6qLt0g/3/

jQuery(document).ready(function()
{
    var div = 'myMessages';
    var html = "";
    html += '<div class="tab-content">';
    jQuery('#'+div+' ul').each(function()
                               {
        jQuery(this).addClass('nav nav-pills');
    });
    jQuery('#'+div+' ul li a').each(function()
                                    {
        jQuery(this).attr('data-toggle', 'pill');

        var href = jQuery(this).attr("data-target");
        html += '<div id="'+href+'" role="tabpanel" class="tab-pane fade"><p>'+href+'</p></div>';
        jQuery(this).attr('aria-controls',href);
        jQuery(this).attr('role','tab');
    });
    html += '</div>';
    jQuery('#'+div).append(html);

    var first = '#'+div+' ul li:first a';
    var url = jQuery(first).attr("rel");
    var href = jQuery(first).attr("data-target");
    var pane = jQuery(first);

    jQuery('#'+href).load(url,function(result){      
        pane.tab('show');
        jQuery('#'+href).addClass("active in");
    });
    jQuery('.nav-pills > li > a').click(function(e)
    {
        jQuery('#'+div+' .tab-content > div').each(function(e)
        {
            jQuery(this).removeClass("active in");
        });
        var url = jQuery(this).attr("rel");
        var href = jQuery(this).attr("data-target");
        var pane = jQuery(this);
        jQuery('#'+href).load(url,function(result){      
            pane.tab('show');
            jQuery('#'+href).addClass("active in");
        });
    });
});

What Ive added is when the dynamic tabs are created, they are given the fade class too. Then when the page loads and tab is automatically selected, ot when the tab is clicked on, it removes all classes on "active in" within the container div, and adds the class "active in" to the relevant content tab.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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