How to use AJAX loading with Bootstrap tabs?

后端 未结 10 1576
無奈伤痛
無奈伤痛 2020-12-02 08:30

I used bootstrap-tabs.js and it has worked perfectly.

But I didn\'t find information about how to load content through AJAX request.

So, how to use AJAX load

相关标签:
10条回答
  • 2020-12-02 08:48

    I use this function and it's really nice because she prevent's you from load ajax again when going back to that tab.

    Tip: on tab href, just put the destination. Add this to footer:

    $("#templates_tabs").tabs({
        beforeLoad: function( event, ui ) {
          if (ui.tab.data("loaded")) {
              event.preventDefault();
    
              return;
          }
    
    
          ui.ajaxSettings.cache = false,
          ui.jqXHR.success(function() {
    
              ui.tab.data( "loaded", true );
          }),
          ui.jqXHR.error(function () {
    
    
              ui.panel.html(
              "Not possible to load. Are you connected?");
          });
        }
      });
    
    0 讨论(0)
  • 2020-12-02 08:49

    Here is a solution I have found - and modified to suit my needs:

    $("#tabletabs").tab(); // initialize tabs
    $("#tabletabs").bind("show", function(e) { 
    var contentID = $(e.target).attr("data-target");
    var contentURL = $(e.target).attr("href");
    if (typeof(contentURL) != 'undefined') { // Check if URL exists
    $(contentID).load(contentURL, function(){
    $("#tabletabs").tab(); // reinitialize tabs
    });
    } else {
    $(contentID).tab('show');
    }
    });
    $('#tabletabs a:first').tab("show"); // Load and display content for first tab
    });
    
    0 讨论(0)
  • 2020-12-02 08:55

    There is an error in your code user1177811.

    It has to be $('[data-toggle="tabajax"]').

    Also there is room for improvement. The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. So i added #content to load the content div of the remote page.

    $this.tab('show'); is now only called, when response was a success.

    Here is the code

     $('[data-toggle="tabajax"]').click(function(e) {
            e.preventDefault();
            $this = $(this);
            var loadurl = $(this).attr('href');
            var targ = $(this).attr('data-target');
            $(targ).load(loadurl+'?ajax=true #content', function(){
                $this.tab('show');
            });
        });
    
    0 讨论(0)
  • 2020-12-02 08:57

    Here is the Bootstrap tab Ajax example, you can use it...

    <ul class="nav nav-tabs tabs-up" id="friends">
          <li><a href="/gh/gist/response.html/3843293/" data-target="#contacts" class="media_node active span" id="contacts_tab" data-toggle="tabajax" rel="tooltip"> Contacts </a></li>
          <li><a href="/gh/gist/response.html/3843301/" data-target="#friends_list" class="media_node span" id="friends_list_tab" data-toggle="tabajax" rel="tooltip"> Friends list</a></li>
          <li><a href="/gh/gist/response.html/3843306/" data-target="#awaiting_request" class="media_node span" id="awaiting_request_tab" data-toggle="tabajax" rel="tooltip">Awaiting request</a></li>
    </ul>
    
    <div class="tab-content">
    <div class="tab-pane active" id="contacts">
    </div>
          <div class="tab-pane" id="friends_list">
    
          </div>
          <div class="tab-pane  urlbox span8" id="awaiting_request">
    
          </div>
        </div>
    

    and here is the AJAX call

    $('[data-toggle="tabajax"]').click(function(e) {
        var $this = $(this),
            loadurl = $this.attr('href'),
            targ = $this.attr('data-target');
    
        $.get(loadurl, function(data) {
            $(targ).html(data);
        });
    
        $this.tab('show');
        return false;
    });
    
    0 讨论(0)
  • 2020-12-02 09:04

    In Bootstrap 2.0 and up you have to bind to the 'show' event instead.

    Here's an example HTML and JavaScript:

    <div class="tabbable">
        <ul class="nav nav-tabs" id="myTabs">    
            <li><a href="#home"  class="active" data-toggle="tab">Home</a></li>
            <li><a href="#foo" data-toggle="tab">Foo</a></li>
            <li><a href="#bar" data-toggle="tab">Bar</li>
        </ul>
        <div>
            <div class="tab-pane active" id="home"></div>
            <div class="tab-pane" id="foo"></div>
            <div class="tab-pane" id="bar"></div>
        </div>
    </div>
    

    JavaScript:

    $(function() {
        var baseURL = 'http://yourdomain.com/ajax/';
        //load content for first tab and initialize
        $('#home').load(baseURL+'home', function() {
            $('#myTab').tab(); //initialize tabs
        });    
        $('#myTab').bind('show', function(e) {    
           var pattern=/#.+/gi //use regex to get anchor(==selector)
           var contentID = e.target.toString().match(pattern)[0]; //get anchor         
           //load content for selected tab
           $(contentID).load(baseURL+contentID.replace('#',''), function(){
                $('#myTab').tab(); //reinitialize tabs
           });
        });
    });
    

    I wrote a post about it here: http://www.mightywebdeveloper.com/coding/bootstrap-2-tabs-jquery-ajax-load-content/

    0 讨论(0)
  • 2020-12-02 09:05

    Here is how I do it. I utilize an attribute data-href which holds the url I want to load on show. This will not reload data that is already loaded unless you set $('#tab').data('loaded'); to 0 or remove it. It also handles relative vs. absolute urls by detecting the location of the first slash. I am not sure of the compatibility with all browsers but it works in our situation and allows for the functionality we are looking for.

    Javascript:

    //Ajax tabs
    $('.nav-tabs a[data-href][data-toggle="tab"]').on('show', function(e) {
      var $this = $(this);
      if ($this.data('loaded') != 1)
      {
        var firstSlash = $this.attr('data-href').indexOf('/');
        var url = '';
        if (firstSlash !== 0)
          url = window.location.href + '/' + $this.attr('data-href');
        else
          url = $this.attr('data-href');
        //Load the page
    
        $($this.attr('href')).load(url, function(data) {
          $this.data('loaded', 1);
        });
      }
    });
    

    HTML:

    <div class="tabbable">
      <ul class="nav nav-tabs">
        <li class="active">
          <a href="#js-tab1" data-href="tab1.php" data-toggle="tab">Tab 1</a>
        </li>
      </ul>
      <div class="tab-content">
        <div class="tab-pane active" id="js-tab1">
          <p>
            This content isn't used, but instead replaced with contents of tab1.php.
          </p>
          <p>
            You can put a loader image in here if you want
          </p>
        </div>
      </div>
    </div>
    
    0 讨论(0)
提交回复
热议问题