gpt4 book ai didi

jquery数据表服务器端分页mvc与动态列

转载 作者:行者123 更新时间:2023-12-01 05:26:25 25 4
gpt4 key购买 nike

我已使用以下链接代码在数据表中进行服务器端分页。

http://www.dotnetawesome.com/2015/11/jquery-datatable-server-side-pagination-sorting.html

但在我的场景中。列是动态的并且从数据库中获取。

$("#myTable").DataTable({
"processing": true, // for show progress bar
"serverSide": true, // for process server side
"filter": false, // this is for disable filter (search box)
"orderMulti": false, // for disable multiple column at once
"ajax": {
"url": "/home/LoadData",
"type": "POST",
"datatype": "json"
},
"columns": [
{ "data": "ContactName", "name": "ContactName", "autoWidth": true },
{ "data": "CompanyName", "name": "CompanyName", "autoWidth": true },
{ "data": "Phone", "name": "Phone", "autoWidth": true },
{ "data": "Country", "name": "Country", "autoWidth": true },
{ "data": "City", "name": "City", "autoWidth": true },
{ "data": "PostalCode", "name": "PostalCode", "autoWidth": true }
]
});

请帮助我如何在此代码中提供动态列。

最佳答案

我的建议如下:

  • 准备第二个 Web 服务,名为/home/GetColumns。该服务将返回以下 JSON:
{
"columns": [
{ "data": "ContactName", "name": "ContactName", "autoWidth": true },
{ "data": "CompanyName", "name": "CompanyName", "autoWidth": true },
{ "data": "Phone", "name": "Phone", "autoWidth": true },
{ "data": "Country", "name": "Country", "autoWidth": true },
{ "data": "City", "name": "City", "autoWidth": true },
{ "data": "PostalCode", "name": "PostalCode", "autoWidth": true }
]

}

  • 发出 Ajax 请求并获取这些列。
  • 如果操作成功,请将列信息作为变量提供给您的代码片段。

示例代码:

$.ajax({url: "home/GetColumns", dataType: "json", success: function(result){
$("#myTable").DataTable({
"processing": true, // for show progress bar
"serverSide": true, // for process server side
"filter": false, // this is for disable filter (search box)
"orderMulti": false, // for disable multiple column at once
"ajax": {
"url": "/home/LoadData",
"type": "POST",
"datatype": "json"
},
"columns": result.columns
});
}});

注意:您可以从返回静态信息的模拟服务开始,然后在确保前端正常工作的情况下实现真正的逻辑。

关于jquery数据表服务器端分页mvc与动态列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39576702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com