简体   繁体   English

如何在下一页内容完全加载之前显示加载或进度条?

[英]How to show loading or progress bar until the next page content fully loaded?

I only know this kind of progress bar but this is static one;我只知道这种进度条,但这是静态的; how can I make it dynamic with my ajax call?如何通过我的 ajax 调用使其动态化?

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> <div class="progress" style="height:30px"> <div class="progress-bar" style="width:60%;height:30px"></div> </div>

I want to show a progress bar or loading box until my next content or page loaded.我想在加载下一个内容或页面之前显示进度条或加载框。

I have an HTML Table having drop-downs.我有一个带有下拉菜单的 HTML 表格。 When I call on any drop-down an ajax call is made and I am displaying content on the next page.当我调用任何下拉菜单时,都会进行 ajax 调用,并且我会在下一页上显示内容。 My content is a HTML table but the table had some large amount of data so it took some time to load.我的内容是一个 HTML 表格,但该表格包含大量数据,因此加载需要一些时间。 At that time I want to show an progress bar or any loading box until my new page content fully loaded.那时我想显示一个进度条或任何加载框,直到我的新页面内容完全加载。

Here is my one page having a HTML table with drop-down这是我的一页,有一个带有下拉列表的 HTML 表格

 var currentlyClickedOutlet = ""; var currentlyClickedBilldate = ""; $(document).ready(function() { $dropdown = $("#contextMenu"); $(".actionButton").click(function() { //move dropdown menu $(this).after($dropdown); //update links $(this).dropdown(); currentlyClickedOutlet = $(this).attr("data-place"); currentlyClickedBilldate = $(this).attr("data-plac"); }); }); data = [{ "amount": 476426, "billdate": "2018-09-01", "outlet": "JAYANAGAR" }, { "amount": 92141, "billdate": "2018-09-01", "outlet": "MALLESHWARAM" }, { "amount": 115313, "billdate": "2018-09-01", "outlet": "KOLAR" }, { "amount": 511153, "billdate": "2018-09-02", "outlet": "JAYANAGAR" }, { "amount": 115704, "billdate": "2018-09-02", "outlet": "MALLESHWARAM" }, { "amount": 83597, "billdate": "2018-09-02", "outlet": "KOLAR" }, { "amount": 167421, "billdate": "2018-09-03", "outlet": "JAYANAGAR" }, { "amount": 53775, "billdate": "2018-09-03", "outlet": "KOLAR" }, { "amount": 269712, "billdate": "2018-09-04", "outlet": "JAYANAGAR" }, { "amount": 58850, "billdate": "2018-09-04", "outlet": "MALLESHWARAM" }, { "amount": 82999, "billdate": "2018-09-04", "outlet": "KOLAR" } ] let formatData = function(data) { let billdates = []; let outlets = []; data.forEach(element => { if (billdates.indexOf(element.billdate) == -1) { billdates.push(element.billdate); } if (outlets.indexOf(element.outlet) == -1) { outlets.push(element.outlet); } }); return { data: data, billdates: billdates, outlets: outlets, }; }; let renderTable = function(data, divId, filterdata) { billdates = data.billdates; outlets = data.outlets; data = data.data; let tbl = document.getElementById(divId); let table = document.createElement("table"); let thead = document.createElement("thead"); let headerRow = document.createElement("tr"); let th = document.createElement("th"); th.innerHTML = "Bill_____Date"; th.classList.add("text-center"); headerRow.appendChild(th); let grandTotal = 0; let outletWiseTotal = {}; th = document.createElement("th"); th.innerHTML = "Total1"; th.classList.add("text-center"); headerRow.appendChild(th); outlets.forEach(element => { th = document.createElement("th"); th.innerHTML = element; th.classList.add("text-center"); headerRow.appendChild(th); outletWiseTotal[element] = 0; data.forEach(el => { if (el.outlet == element) { outletWiseTotal[element] += parseInt(el.amount); } }); grandTotal += outletWiseTotal[element]; }); thead.appendChild(headerRow); headerRow = document.createElement("tr"); th = document.createElement("th"); th.innerHTML = "Total"; th.classList.add("text-center"); headerRow.appendChild(th); outlets.forEach(element => { th = document.createElement("th"); th.innerHTML = outletWiseTotal[element]; th.classList.add("text-right"); headerRow.appendChild(th); }); th = document.createElement("th"); th.innerHTML = grandTotal; th.classList.add("text-right"); // grand total headerRow.insertBefore(th, headerRow.children[1]); thead.appendChild(headerRow); table.appendChild(thead); let tbody = document.createElement("tbody"); billdates.forEach(element => { let row = document.createElement("tr"); td = document.createElement("td"); td.innerHTML = element; row.appendChild(td); let total = 0; outlets.forEach(outlet => { let el = 0; data.forEach(d => { if (d.billdate == element && d.outlet == outlet) { total += parseInt(d.amount); el = d.amount; } }); td = document.createElement("td"); a = document.createElement("a"); td.classList.add("text-right"); td.classList.add("dropdown"); a.classList.add("btn"); a.classList.add("btn-secondary"); a.classList.add("actionButton"); a.classList.add("btn") a.classList.add("btn-secondary"); a.classList.add("dropdown-toggle"); a.classList.add("dropdown-toggle-split"); /* a.classList.add("text-center"); */ a.setAttribute("data-place", outlet); a.setAttribute("data-plac", element); a.setAttribute("data-toggle", "dropdown"); a.innerHTML = el; td.appendChild(a); row.appendChild(td); }); td = document.createElement("td"); td.innerHTML = total; td.classList.add("text-right"); row.insertBefore(td, row.children[1]); tbody.appendChild(row); }); table.appendChild(tbody); tbl.innerHTML = ""; tbl.appendChild(table); table.classList.add("table"); table.classList.add("table-striped"); table.classList.add("table-bordered"); table.classList.add("table-hover"); } let formatedData = formatData(data); renderTable(formatedData, 'tbl', '');
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script> <div id="tbl"></div> <ul id="contextMenu" class="dropdown-menu" role="menu"> <li><a href="test.jsp" class="link1 dropdown-item">BillSummary</a></li> <li><a href="test1.jsp" class="link2 dropdown-item">Item Summary</a></li> </ul>

I know Bootstrap progress bar, but how to do that dynamically?我知道 Bootstrap 进度条,但如何动态地做到这一点?

I just want to display a progress bar or loading box until my new page loaded or want to achieve something like don't jump to the next page until the new page's content is fully loaded.我只想显示一个进度条或加载框,直到我的新页面加载完毕,或者想实现类似在新页面的内容完全加载之前不跳转到下一页的效果。

For an easy loading box I do this:对于一个易于装载的盒子,我这样做:

Add in your html添加到你的html

<div id="waiter"></div>

In css:在 CSS 中:

#waiter {
position: fixed;
border: 12px solid #f3f3f3; /* Light grey */
border-top: 12px solid #3498db; /* Blue */
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 2s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); } }

And show/hide it when needed with display css property.并在需要时使用 display css 属性显示/隐藏它。

See this page for a complete tutorial.有关完整教程,请参阅此页面

You should use form events on jQuery( unload , beforeunload ) for example, you can create a function to show progress dialog like this:例如,您应该在 jQuery( unloadbeforeunload )上使用表单事件,您可以创建一个函数来显示进度对话框,如下所示:

function showProgress(){
      $('.progress').show();
}

and another function to hide progress dialog:和另一个隐藏进度对话框的功能:

function hideProgress(){
      $('.progress').hide();
}

now bind unload , beforeunload events of forms, the below code shows progress dialog until page loaded fully(before page load and after each submit):现在绑定unloadbeforeunload事件,下面的代码显示进度对话框,直到页面完全加载(页面加载之前和每次提交之后):

$(window).on("unload", function () {
    showWaiting();
});

$(window).on('beforeunload', function () {
    showWaiting();

});

for Ajax calls, before each request call showWaiting();对于 Ajax 调用,在每个请求调用showWaiting(); method and after Ajax complete event call hideProgress()方法和 Ajax 完成事件调用hideProgress()之后

i did it creating a iframe with display to none, then, i put some class to each a element, cancel default behavior and load the new page in the iframe, and check the load event from that iframe, when the load is ready i replace window.location.href to the iframe location, and the navigator charge inmediatly我创建了一个没有显示的 iframe,然后,我为每个元素放置了一些类,取消默认行为并在 iframe 中加载新页面,并检查来自该 iframe 的加载事件,当加载准备好时我替换window.location.href 到 iframe 位置,导航器立即充电

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

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