datatable動態曾加欄位

Posted by Leo Yang on 2023-11-22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//定義DataTable
var options =
{
"processing": true,
"paging": true,
"searching": true,
"bFilter": false,
"bDestroy": true,
"scrollY": "455px",
"scrollX": true,
"scrollCollapse": true,
"ajax": {
"url": 'apiurl',
"type": "POST",
"dataType": "json",
"data": { "QueryMediaType": $('#hidSelMediaType').val() }
},
"columns": ColumnsRule,
"lengthMenu": [[10, 15, 25, 50, -1], [10, 15, 25, 50, "All"]],
columnDefs: [
{
targets: 0, render: function (e)
{
return '<button type=\'button\' class=\'btn btn-info btn-sm\' data-toggle=\'modal\' data-target=\'#exampleModal\' onclick="AddObservableList(\'' + e + '\')" >加入觀察</button>';
}
}
],
"order": [[3, "desc"]],
"initComplete": function (settings, json) {
hideLoadMask();
}
};

//從別的API取得每個欄位設定
var allSetting = responseJson.Data[0].FieldSetting.split('.');

//foreach 每個欄位
for (var i = 1; i < allSetting.length; i++)
{
//設定欄位怎麼渲染,下面例子是判斷這個欄位要不要顯示
options.columnDefs.push(
{
visible: allSetting[i-1] == '0' ? false:true,
targets: i
});
}

//把DataTable設定設定到DataTableDom上
MasterTable = $('#example').DataTable(options);