我正在尝试从控制器操作中以JSON格式获取一些数据,然后使用AJAX将其发送到DataTables,显示了数据,但是当我搜索或排序数据时就消失了,并且显示了“未找到数据”消息,也不再有任何页面这只是一张长桌子。
HTML表格:
<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>
脚本:
$(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");
}
});
}
管制员的行动:
[HttpGet]
public ActionResult GetActif()
{
var list = ListCharges.list.FindAll(x => x.conge.etat == "Actif");
return Json(list);
}
最佳答案
使用外部jQuery方法(例如$.ajax()
,$.post()
,$.get()
等)填充DataTable是极其糟糕的做法,因为您最终需要处理各种变通方法,以使数据在需要的位置和时间加载到表中。相反,我建议使用ajax
选项。
另一个不好的选择是手动编写表主体HTML。如果仅使用columns
/ columnDefs
选项指向列数据源,DataTables可以为您完美地做到这一点。
为了使某些表列呈现为任意HTML,还有另一个选项columns.render
。
最后,您可以使用columns.createdCell
选项将HTML属性附加到单元格中。
因此,您完整的jQuery代码可能看起来像:
$(document).ready(() => {
$('#demoGrid').dataTable({
ajax: {
url: '/ChargeAffaire/GetActif'
},
language: {
search: "Rechercher",
lengthMenu: "Afficher _MENU_ chargés par page",
info: "Page: _PAGE_ / _PAGES_",
paginate: {
next: "Suivante",
previous: "Précédente"
}
},
columns: [
{title: 'Matricule', data: 'matricule'},
{title: 'Intitulé', data: 'intitule'},
{title: 'Nombre de compte', data: () => ' 59'},
{
title: '',
data: rowData => `
<a class="cls no-href" id="detail" onclick="update_url('consulter/rowData.id')" data-toggle="modal" data-target="#exampleModal">consulter</a>
<br/>
<a class="no-href" id="conge" onclick="updateConge(rowData.id)" data-toggle="modal" data-target="#dateMission">Ajouter un congé</a>
<br/>
<a class="no-href" id="ajout" onclick="updateAction(rowData.id)" data-toggle="modal" data-target="#ajoutModal">Ajouter un compte</a>`,
createdCell: td => $(td).attr('id', 'linkContainer')
}
],
//append custom classes for the first 3 <th> and id attribute to <tbody>
renderCallback: function(){
this.api().columns().every(function(){
if(this.index() < 3) $(this.header()).addClass('th-sm');
});
$('#demoGrid tbody').attr('id', 'chargeHolder');
}
});
});
HTML可能很简单,例如:
<table id="demoGrid" class="table table table-hover dt-responsive nowrap" width="100%" cellspacing="0"></table>
我建议将您的CSS放在单独的文件中。
为了在必要时重新加载数据,您可以简单地调用
ajax.reload()
方法,并在需要时受益于ajax.data
选项作为回调来操纵发送到后端脚本的参数。