[英]DataTable- all data is lost when i click on a event
I am trying out DataTables for the first time in a angular project, i managed to get the table template working with my data (that i get from a service) but everytime i press a button all the data disapears( no error is shown on logs) and the number of rows that should be apearing per page is not working aswell and i the first line row that i get in my table is "No data available in table" only when i refresh the page the data comes back again.我第一次在 angular 项目中尝试使用 DataTables,我设法让表格模板使用我的数据(我从服务中获得)但是每次我按下按钮时所有数据都会消失(日志上没有显示错误) 并且每页应该出现的行数也不起作用,我在表中得到的第一行是“表中没有可用数据”,只有当我刷新页面时数据再次返回。 Basicly not one feature is working.
基本上没有一个功能在工作。
The Html table is the following Html表如下
<div class="center">
<h1><i class='bx bxs-package'></i> Routes List</h1>
<table id="tbody" datatable [dtOptions]="dtoptions" class="row-border hover">
<thead>
<tr>
<th>Starting Warehouse ID</th>
<th>Destination Warehouse ID</th>
<th>Distance</th>
<th>Time</th>
<th>Energy</th>
</tr>
</thead>
</table>
</div>
the service i'm using我正在使用的服务
public extractData(res: any) {
return res || {};
}
getRoutes(): Observable<any> {
return this.http.get('http://localhost:3000/api/routes').pipe(map(this.extractData));
}
listRoutes(): void {
let tbody = document.getElementById('tbody') as HTMLTableElement;
let array = this.getRoutes();
array.forEach(function (i) {
for (let j = 0; j < i.length; j++) {
let tr = tbody.insertRow();
let td_idWarehouseStart = tr.insertCell();
let td_idWarehouseDestination = tr.insertCell();
let td_distance = tr.insertCell();
let td_time = tr.insertCell();
let td_energy = tr.insertCell();
td_idWarehouseStart.innerText = i[j].idWarehouseStart;
td_idWarehouseDestination.innerText = i[j].idWarehouseDestination;
td_distance.innerText = i[j].distance;
td_time.innerText = i[j].time;
td_energy.innerText = i[j].energy;
}
});
}
and this is the output in the web page这是 web 页面中的 output
enter image description here在此处输入图像描述
I have no idea how to fix this.我不知道如何解决这个问题。 I know it has something to do with my service, because if i dont use the service and push data hard coded into the html it works just fine
我知道这与我的服务有关,因为如果我不使用该服务并将硬编码的数据推送到 html,它就可以正常工作
As recomended by a reply I used the DataTables API and it now works just fine, i changed the service code to the following正如回复所推荐的,我使用了 DataTables API,它现在工作正常,我将服务代码更改为以下
var dataTable = $('#tbody').DataTable();
var routesArray = this.getRoutes();
routesArray.forEach(function (i){
for(let j=0;j<i.length;j++){
dataTable.row.add(
[
i[j].idWarehouseStart,
i[j].idWarehouseDestination,
i[j].distance,
i[j].time,
i[j].energy
]
).draw();
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.