Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Data only when tab is active?

I am rendering the all data into page once(on single url 'http://localhost:9111/sale/').

And showing into different tabs on that page.

<div class="tab-base">
             <!--Nav Tabs-->
             <ul class="nav nav-tabs">
                <li class="active">
                   <a data-toggle="tab" href="#demo-lft-tab-1">Daily Sale</a>
                </li>
                <li>
                   <a data-toggle="tab" href="#demo-lft-tab-2">Channel Wise Sale</a>
                </li>
                <li>
                   <a data-toggle="tab" href="#demo-lft-tab-3">Returns</a>
                </li>
               </ul>
            </div>
     <div class="tab-content">
<form method="POST" action="{% url 'sale' %}">
   {% csrf_token %}

   <div id="demo-lft-tab-1" class="tab-pane fade active in">
      <div class="panel-body">
         <table data-toggle="table">
            <thead >
               <tr>
                  <th data-align="center" data-sortable="true">Day</th>
                  <th data-align="center" data-sortable="true">Sale Value</th>
                  <th data-align="center" data-sortable="true">Quantity Sold</th>
               </tr>
            </thead>
            <tbody>
               {% for day, val, qty in sale_data %}
               <tr>
                  <td>{{day}}</td>
                  <td>{{val}}</td>
                  <td>{{qty}}</td>
               </tr>
               {% endfor %}
            </tbody>
         </table>
      </div>
   </div>



<div id="demo-lft-tab-2" class="tab-pane fade">
   <div class="panel-body">
      <table data-toggle="table" >
         <thead>
            <tr>
               <th data-align="center" data-sortable="true">Channel</th>
               <th data-align="center" data-sortable="true">Brand</th>
               <th data-align="center" data-sortable="true">Category</th>
               <th data-align="center" data-sortable="true">Selling Price</th>
               <th data-align="center" data-sortable="true">Quantity Sold</th>
               <th data-align="center" data-sortable="true">Percentage %</th>
            </tr>
         </thead>
         <tbody>
            {% for channel,brand,category,selling_price,quantity_sold, percentage in sal_data %}
            <tr>
               <td>{{channel}}</td>
               <td>{{brand}}</td>
               <td>{{category}}</td>
               <td>{{selling_price}}</td>
               <td>{{quantity_sold}}</td>
               <td>{{percentage}}%</td>
            </tr>
            {% endfor %}
         </tbody>
      </table>
   </div>
</div>

<!--   here is more code for other tab same as above  -->
<!--  -->


</form>


</div>

But when render data is large then page load takes more time.

Now I want know, How to load the data only for active tab ?

like image 865
Shubham Batra Avatar asked Feb 18 '16 07:02

Shubham Batra


1 Answers

Here is my solution:

    $('#tab-nav > a').one('click', function() {
        // event.preventDefault();
        var target = $(this).attr('id');
        $("#" + target + "_content .filter_importance .menu div:last-of-type").trigger('click'); // here you can simplify by the use the `load` function instead of triggering a `click` event. 
    });

How it works:

  1. I use SemanticUI for my CSS framework and #tab-nav > a targets the tabs. Following my naming policy, in each tab I have also added ID attribute, for instance id="firsttab".

  2. The DIVs with content all have ID attribute, for instance id="firsttab_content". Please note how the target element is defined with the use of target variable: $("#" + target + "_content .filter_importance .menu div:last-of-type") produces #firsttab_content.

  3. Inside the content of the tab I have buttons which use jQuery's load function to load the data.

  4. The above solution clicks on one of the buttons - trigger('click').

Notes: 1. if you use one('click', function() the content will be loaded only once. If you modify the code and make on('click', function() (pay attention to e one --> on), the content would be loaded each time you click on the tab.

  1. if you want to set a certain tab as default and load the content inside it, put the `trigger('click') inside the

    $("document").ready(function() { });

  2. You can decide on which content should be preloaded with this code:

    $("document").ready(function() {

    // loads content for tab open on page load
    var hash = window.location.hash.substr(1); // gets the value of hash from URL
    $("#" + hash + "_content .filter_importance .menu div:last-of-type").trigger('click'); // targets and clicks the button which loads data inside the tab content area.
    

    });

Be advised that you might want to activate the tab with additional code - I do it by reading hash from URL in my backend. Usually it is done by adding class active to the element, in this case "#firsttab a".

like image 174
Peter Avatar answered Nov 02 '22 10:11

Peter