Javascript 如何在jquery datacolumn的动态列中设置服务器端?
我已经为动态列创建了一个数据表,下面是数据表的代码Javascript 如何在jquery datacolumn的动态列中设置服务器端?,javascript,jquery,datatables,Javascript,Jquery,Datatables,我已经为动态列创建了一个数据表,下面是数据表的代码 <table id="tag" class="display table table-striped table-bordered" cellspacing="0" width="100%"> <thead> <tr>
<table id="tag" class="display table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
@foreach (var item in Model.Select((value, i) => new { i, value}))
{
<th id=@(item.i+1)>@item.value.Category</th>
}
<th id="@(Model.Count()+1)">Simulation Name</th>
<th id="@(Model.Count()+2)">Patient Name</th>
</tr>
</thead>
</table>
动态数据表的javascript代码是
$(document).ready(function () {
$("#tag").DataTable({
"aLengthMenu": [[5,10, 25, 50, 75, -1], [5,10, 25, 50, 75, "All"]],
"iDisplayLength": 5,
serverSide: true,
stateSave: true,
search: true,
ajax: {
type: 'POST',
dataType: 'json',
url: 'GetFilteredPatientTagss',
success: function (d) {
for (var i = 0; i < d.requestedRecords.length; i++) {
var result = Object.values(d.requestedRecords[i]);
$('#tag').DataTable().row.add(result).draw(false);
}
}
}
});
});
在这里,我已经完成了serverSide为true,如果我设置server-side:false,那么数据表就可以了,搜索、下拉和分页都来自客户端,但是当我设置serverSide:true时,它在API中执行无限循环,并且数据表中没有显示任何数据。我需要在此动态列中从服务器端搜索数据表并对其进行分页,其中没有固定的列数。如何做到这一点?不要在数据表中使用ajax.success。改用。正如该页上所说:不应更改ajax的成功选项—数据加载完成后,DataTables在内部使用它来执行表绘制。此外,不需要在DataTable定义中使用row.add将数据写入表中。如果将dataSrc指向JSON响应的正确部分,则只需这样做。DataTables将处理在JSON上的迭代和加载表。@andrewjames感谢它成功地处理了数据src,但当我通过分页或增加下拉列表进入下一页时,数据被添加而不是替换,我如何修复它?您可以通过您的问题向我们展示您的新方法。