简体   繁体   English

仅在选项卡处于活动状态时加载数据

[英]Load Data only when tab is active?

I am rendering the all data into page once(on single url ' http://localhost:9111/sale/ '). 我将所有数据渲染到页面一次(在单个网址' 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 ? 现在我想知道,如何仅为活动选项卡加载数据?

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. 我为我的CSS框架使用SemanticUI, #tab-nav > a以标签#tab-nav > a目标。 Following my naming policy, in each tab I have also added ID attribute, for instance id="firsttab" . 按照我的命名策略,在每个选项卡中我还添加了ID属性,例如id="firsttab"

  2. The DIVs with content all have ID attribute, for instance id="firsttab_content" . 具有内容的DIV都具有ID属性,例如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 . 请注意如何使用target变量定义目标元素: $("#" + target + "_content .filter_importance .menu div:last-of-type")生成#firsttab_content

  3. Inside the content of the tab I have buttons which use jQuery's load function to load the data. 在选项卡的内容中,我有一些按钮,它们使用jQuery的load功能来加载数据。

  4. The above solution clicks on one of the buttons - trigger('click') . 上面的解决方案点击了其中一个按钮 - 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。如果你使用one('click', function() ,内容将只加载一次。如果修改代码并make on('click', function() (注意e one - > on ),每次单击选项卡时都会加载内容。

  1. if you want to set a certain tab as default and load the content inside it, put the `trigger('click') inside the 如果你想将某个标签设置为默认值并加载其中的内容,请将`trigger('click')置于其中

    $("document").ready(function() { }); $(“document”)。ready(function(){});

  2. You can decide on which content should be preloaded with this code: 您可以决定使用此代码预加载哪些内容:

    $("document").ready(function() { $(“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. 请注意,您可能希望使用其他代码激活选项卡 - 我是通过从后端的URL读取哈希来实现的。 Usually it is done by adding class active to the element, in this case "#firsttab a" . 通常通过向元素添加active类来完成,在本例中为"#firsttab a"

Irrespective of any backend languages or the frameworks you user, browser understands only HTML , CSS and JavaScript . 无论您使用何种后端语言或框架,浏览器都只能理解HTMLCSSJavaScript It seems that you are developing application in Python with Django web framework. 您似乎正在使用Django Web框架在Python中开发应用程序。

You can follow below approaches to improve your page load and end user experiences, 您可以按照以下方法来改善页面加载和最终用户体验,

  • Show Only Minimum & Important Data - Only render minimal information from server side where ever user's eyes first look into it when the page is loaded. 仅显示最小和重要数据 - 仅在服务器端呈现最少信息,用户的眼睛在加载页面时首先查看该信息。
  • Other Parts Can Be Empty - Have a place holder(ie empty tab pages) to fill the data later when page loaded into the browser. 其他部分可以为空 - 当页面加载到浏览器中时,有一个占位符(即空标签页)以填充数据。
  • Use Ajax - Once page loaded in browser give an Ajax call to load the data. 使用Ajax - 一旦在浏览器中加载页面,就会调用Ajax来加载数据。
  • Loading ... - Meantime you can show some information saying "Loading ..." or animated images to inform the user about data loading. 正在加载... - 同时您可以显示一些信息,说“正在加载...”或动画图像,以通知用户数据加载。
  • Use Client Side Javascript MVC Frameworks - Instead of rendering everything in server side, you can move most of the rendering task to client side using client side JavaScript MVC frameworks such as Angular.js , Backbone.js , Ember.js , React.js , Vue.js , etc. 使用客户端Javascript MVC框架 - 您可以使用客户端JavaScript MVC框架(如Angular.jsBackbone.jsEmber.jsReact.js)将大部分渲染任务移动到客户端,而不是在服务器端渲染所有内容。 Vue.js
  • Pre-aggregate Large Dataset - Sometimes, it is good to load pre-aggregated data for the first time. 预聚合大数据集 - 有时,最好先加载预聚合数据。 When user clicks further, you can display detailed information specific to user's interest. 当用户进一步点击时,您可以显示特定于用户兴趣的详细信息。
  • Use Pagination - If you are going to show data in a table, only retrieve and show a limited number of rows using pagination approach like, how Google search result is displayed 使用分页 - 如果要在表格中显示数据,只能使用分页方法检索和显示有限数量的行,例如Google搜索结果的显示方式
  • Google's - Make the Web Faster - Finally, to speed up and optimize your web app's performance follow Google's - Make the Web Faster guide lines. 谷歌 - 让网络更快 - 最后,加快和优化您的网络应用程序的性能遵循谷歌 - 使网络更快的指南。

如果您使用的是Spring Framework,那么请使用Pagination概念。它将确保只需要获取所需的数据并且速度很快

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

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