我目前正在与大量数据(5000)的数据表工作。为了能够用进度加载数据,我添加了一个进度条,说明在每个单位时间内加载了多少数据。但是下面的代码不再工作了。
let handleProgressBar = (id, value, total) => {
let percent = Math.round((value / total) * 10000) / 100;
$(id + " > div").html(percent + "%");
$(id + " > div").css('width', percent + "%");
}
let table = $('#data').DataTable();
fetch('https://jsonplaceholder.typicode.com/photos')
.then((res) => res.json())
.then((res) => {
res.forEach(async(data, index)=>{
table.row.add([
data.id,
data.albumId,
data.title,
data.url
]);
handleProgressBar('#progress-bar', index+1, res.length);
await new Promise(r => setTimeout(r, 1)); // sleep 1 ms
});
table.draw();
});
.progress-bar-striped {
overflow: hidden;
height: 20px;
margin-bottom: 20px;
background-color: #f5f5f5;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
-moz-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
}
.progress-bar-striped>div {
background-image: linear-gradient(45deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent);
background-size: 40px 40px;
float: left;
width: 0%;
height: 100%;
font-size: 12px;
line-height: 20px;
color: #000000;
font-weight: bold;
text-align: center;
-webkit-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
-moz-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
/*-webkit-transition: width 3s ease;
-moz-transition: width 3s ease;
-o-transition: width 3s ease;
transition: width 3s ease;*/
animation: progress-bar-stripes 2s linear infinite;
background-color: #288ade;
}
.progress-bar-striped p {
margin: 0;
}
@keyframes progress-bar-stripes {
0% {
background-position: 40px 0;
}
100% {
background-position: 0 0;
}
}
<link href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<div id="progress-bar" class="progress-bar-striped">
<div style="width: 0%;">
<p>0%</p>
</div>
</div>
<table id="data" class="table display table-stripped">
<thead>
<tr>
<th>ID</th>
<th>Album ID</th>
<th>Title</th>
<th>URL Photo</th>
</tr>
</thead>
<tbody></tbody>
</table>
表已经成功加载,但是进度条显示的是100%
,而不是应该从0%
增加到100%
。我想知道它的工作(,但不完美),如果我不使用datatable如下面的代码所示。
let handleProgressBar = (id, value, total) => {
let percent = Math.round((value / total) * 10000) / 100;
$(id + " > div").html(percent + "%");
$(id + " > div").css('width', percent + "%");
}
(async() =>{
let n = 5000;
handleProgressBar('#progress-bar', 0, n);
for(let i = 1; i <= n; i++) {
handleProgressBar('#progress-bar', i, n);
await new Promise(r => setTimeout(r, 1)); // sleep 1 ms
}
})();
.progress-bar-striped {
overflow: hidden;
height: 20px;
margin-bottom: 20px;
background-color: #f5f5f5;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
-moz-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
}
.progress-bar-striped>div {
background-image: linear-gradient(45deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent);
background-size: 40px 40px;
float: left;
width: 0%;
height: 100%;
font-size: 12px;
line-height: 20px;
color: #000000;
font-weight: bold;
text-align: center;
-webkit-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
-moz-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
/*-webkit-transition: width 3s ease;
-moz-transition: width 3s ease;
-o-transition: width 3s ease;
transition: width 3s ease;*/
animation: progress-bar-stripes 2s linear infinite;
background-color: #288ade;
}
.progress-bar-striped p {
margin: 0;
}
@keyframes progress-bar-stripes {
0% {
background-position: 40px 0;
}
100% {
background-position: 0 0;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="progress-bar" class="progress-bar-striped">
<div style="width: 0%;">
<p>0%</p>
</div>
</div>
我不知道错误在哪里。谁可以为我提供一个完美的方式来处理进度条与大量的数据表?谢谢。
我已经找到解决办法了。错误是我把async
字放在res.forEach
里面。当我把它放在fetch.then
之后,并使用for
循环代替forEach
时,函数执行的行为改变了,可以成功地完成。loadNumber
变量可用于确定每单位时间将绘制多少数据表。
let handleProgressBar = (id, value, total) => {
let percent = Math.round((value / total) * 10000) / 100;
$(id + " > div").html(percent + "%");
$(id + " > div").css('width', percent + "%");
}
let table = $('#data').DataTable({
dom: 'ilpftr'
});
fetch('https://jsonplaceholder.typicode.com/photos')
.then((res) => res.json())
.then(async(res) => {
let loadNumber = 50;
for(let i = 0; i < res.length; i++) {
table.row.add([
res[i].id,
res[i].albumId,
res[i].title,
res[i].url
]);
if (i % loadNumber == 0) {
table.draw();
handleProgressBar('#progress-bar', i+1, res.length);
await new Promise(r => setTimeout(r, 1)); // sleep 1 ms
}
}
table.draw();
handleProgressBar('#progress-bar', res.length, res.length);
});
.progress-bar-striped {
overflow: hidden;
height: 20px;
margin-bottom: 20px;
background-color: #f5f5f5;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
-moz-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
}
.progress-bar-striped>div {
background-image: linear-gradient(45deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent);
background-size: 40px 40px;
float: left;
width: 0%;
height: 100%;
font-size: 12px;
line-height: 20px;
color: #000000;
font-weight: bold;
text-align: center;
-webkit-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
-moz-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
/*-webkit-transition: width 3s ease;
-moz-transition: width 3s ease;
-o-transition: width 3s ease;
transition: width 3s ease;*/
animation: progress-bar-stripes 2s linear infinite;
background-color: #288ade;
}
.progress-bar-striped p {
margin: 0;
}
@keyframes progress-bar-stripes {
0% {
background-position: 40px 0;
}
100% {
background-position: 0 0;
}
}
<link href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<div id="progress-bar" class="progress-bar-striped">
<div style="width: 0%;">
<p>0%</p>
</div>
</div>
<table id="data" class="table display table-stripped">
<thead>
<tr>
<th>ID</th>
<th>Album ID</th>
<th>Title</th>
<th>URL Photo</th>
</tr>
</thead>
<tbody></tbody>
</table>