[英]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: 这个怎么运作:
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"
。
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
。
Inside the content of the tab I have buttons which use jQuery's load
function to load the data. 在选项卡的内容中,我有一些按钮,它们使用jQuery的
load
功能来加载数据。
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 ),每次单击选项卡时都会加载内容。
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(){});
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 . 无论您使用何种后端语言或框架,浏览器都只能理解HTML , CSS和JavaScript 。 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, 您可以按照以下方法来改善页面加载和最终用户体验,
如果您使用的是Spring Framework,那么请使用Pagination概念。它将确保只需要获取所需的数据并且速度很快
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.