[Jquery DataTable] 动态生成列名

 

 1     $.ajax({
 2         type: "GET",
 3         contentType: "application/json; charset=utf-8",
 4         url: "",
 5         success: function (res) {
 6 
 7                 $("#TableID").empty();  //Table ID
 8 
 9                 var columns = [];
10                 var TableHeader = "<thead><tr>";   
11 
12                 $.each(res.data[0], function (key, value) {
13                     columns.push({ "data": key });
14                     TableHeader += "<th>" + key + "</th>";
15                 });
16 
17                 $("#TableID").append(TableHeader);
18 
19                 $('#TableID').dataTable({
20                    //.....
21                    "columns": columns
22                 });
23         },
24         error: function (xmlHttpRequest, textStatus, errorThrown) {
25         }
26     });

 

posted @ 2022-05-05 10:41  WikiChen  阅读(40)  评论(0编辑  收藏  举报