Reputation: 581
I have table that I populate via ajax call
Here is table code
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Ф.И.О</th>
<th scope="col">Дата рождения</th>
<th scope="col">Телефон</th>
<th scope="col">График</th>
<th scope="col">Адрес</th>
<th scope="col">Паспортные данные</th>
<th scope="col">Мед Книжка</th>
</tr>
</thead>
<tbody id="people" style="overflow: auto;">
</tbody>
And here is code of js script to populate it
function AllPeople() {
let getPeopleUrl = '/peopleforworks/index';
$.ajax({
url: getPeopleUrl,
contentType: 'application/json; charset=utf-8',
type: 'GET',
dataType: 'json',
processData: false,
success: function (data) {
$("#people tr").remove();
var list = data;
for (var i = 0; i <= list.length - 1; i++) {
var tableData = '<tr>' + '<td>' +
(i + 1) +
'</td>' +
'<td > ' +
list[i].FIO +
'</td>' +
'<td > ' +
moment(list[i].Birthday).format('DD/MM/YYYY') +
'</td>' +
'<td> ' +
list[i].TelephoneNumber +
'</td>' +
'<td > ' +
list[i].WorkTime +
'</td>' +
'<td> ' +
list[i].Adress +
'</td>' +
'<td> ' +
list[i].PassportData +
'</td>' +
'<td> ' +
list[i].MedicalBook +
'</td>'
+
'</tr>';
$('#people').append(tableData);
}
}
})
}
It works great and data is passing to table
But when I call this table (it's on modal) second time it's not clear values in tbody
I tried to do it like this $("#people tr").remove();
, but it not works and I have no errors.
How I can clear values correctly?
Upvotes: 0
Views: 66
Reputation: 30
If you want to clear all the rows.Then use below code.
var table = $('#example').DataTable();
table.clear().draw();
For more information follow api link https://datatables.net/reference/api/clear()
Upvotes: 0