I'm trying to get some data from controller action in JSON format then send it to DataTables using AJAX, the data is shown but when I search or sort the data disappears and "no data found" message displayed, also there's no longer any pages it's just one long table.
HTML table:
<table id="demoGrid" class="table table table-hover dt-responsive nowrap" width="100%" cellspacing="0">
<thead>
<tr class="styleHeaderTab">
<th class="th-sm">
Matricule
</th>
<th class="th-sm">
Intitulé
</th>
<th class="th-sm">
Nombre de compte
</th>
<th class="">
</th>
</tr>
</thead>
<tbody id="chargeHolder"></tbody>
</table>
Script:
$(document).ready(() => getActif());
$('#demoGrid').dataTable({
"language": {
"search": "Rechercher",
"lengthMenu": "Afficher _MENU_ chargés par page",
"info": "Page: _PAGE_ / _PAGES_",
"paginate": {
"next": "Suivante",
"previous": "Précédente"
}
}
});
function getActif() {
$.ajax({
url: '/ChargeAffaire/GetActif',
method: 'get',
dataType: 'json',
error: (err) => console.log(err),
success: (res) => {
let s="";
for (let i=0;i<res.length;i++) {
s +=`<tr>
<td>${res[i].matricule}</td>
<td>${res[i].intitule}</td>
<td> 59</td>
<td id="linkContainer">
<a class="cls no-href" id="detail" onclick="update_url('consulter/${res[i].id}')" data-toggle="modal" data-target="#exampleModal">consulter</a>
<br/>
<a class="no-href" id="conge" onclick="updateConge(${res[i].id})" data-toggle="modal" data-target="#dateMission">Ajouter un congé</a>
<br/>
<a class="no-href" id="ajout" onclick="updateAction(${res[i].id})" data-toggle="modal" data-target="#ajoutModal">Ajouter un compte</a>
</td>
</tr>`;
}
$("#chargeHolder").html(s);
$(".no-href").css({"cursor":"pointer","color":"#077bb1"});
$(".no-href").parent().css("text-align","center");
}
});
}
Controller's action:
[HttpGet]
public ActionResult GetActif()
{
var list = ListCharges.list.FindAll(x => x.conge.etat == "Actif");
return Json(list);
}