[英]Get data from controller action to jquery datatable
我正在嘗試從控制器操作中以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);
}
使用$.ajax()
, $.post()
, $.get()
類的外部 jQuery方法填充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
選項作為回調來處理發送給后端腳本的參數(如果需要)。
控制器:
[HttpGet] public ActionResult GetActif(){
total = list.Count,
rows = (from u in list
select new
{
id = u.Id,
Name = u.sName,
}).ToArray()
};
return JsonData;
}
對於腳本:
dataType: 'json',
success: function (result) {
$("#action").text("");
var html = '';$.each(result.rows, function (key, item) {
html += '<tr>';
html += '<td class="text-center">' + item.id + '</td>';
html += '<td class="text-center">' + item.Name + '</td>';});
$('#demoGrid tbody').html(html);
$('#demoGrid').DataTable({}, error: function (error) {
return error;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.