我进行了ajax调用来获取动态数据,其中标头不固定显示多少列,以以下格式获取数据后,无法导出或限制行数,标头可以超过30个。 任何帮助将不胜感激。
这是我的html代码:
<div class="table-responsive" id="live_tracking_table_cantainer">
<table class="table table-striped table-bordered dataTable livertrackingtable">
</table>
</div>
Ajax call :
$(".livetracking").click(function () {
var data = $("#livetrackingform").serialize();
$.ajax({
type: 'post',
url: '/getcampaigndata',
data: data,
success: function (dataSet) {
if (dataSet == '' || dataSet == 'undefined') {
$("#live_tracking_table_cantainer").css('text-align', 'center');
$("#live_tracking_table_cantainer").html('No data found');
return;
}
var my_columns = [];
$.each(dataSet[0], function (key, value) {
var my_item = {};
my_item.data = key;
my_item.title = key;
my_columns.push(my_item);
});
$('#live_tracking_table_cantainer').empty();
$table = $('<table style="width: 100%">');
$table.addClass("table table-striped table-bordered dataTable livertrackingtable");
$($table).DataTable({
data: dataSet,
"columns": my_columns,
"order": [],
"columnDefs": [{
"targets": 'no-sort',
"orderable": false,
}],
"aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
]
});
$table.appendTo('#live_tracking_table_cantainer');
},
error: function (data) {
console.log(data);
}
});
});
What I am getting from ajax call :
[{created_at: "23-04-2019 05:22 PM", Name: "Test", Designation: "ZSM", Zone: "North", Region: "",…},…]
0: {created_at: "23-04-2019 05:22 PM", Name: "Test", Designation: "ZSM", Zone: "North", Region: "",…}
1: {created_at: "23-04-2019 05:28 PM", Name: "Test", Designation: "ZSM", Zone: "North", Region: "",…}
2: {created_at: "23-04-2019 05:30 PM", Name: "Test", Designation: "ZSM", Zone: "North", Region: "",…}
3: {created_at: "25-04-2019 05:24 PM", Name: "Test", Designation: "ZSM", Zone: "North", Region: "",…}
4: {created_at: "29-04-2019 06:23 PM", Name: "Test", Designation: "ZSM", Zone: "North", Region: "",…}
5: {created_at: "29-04-2019 06:24 PM", Name: "Test", Designation: "ZSM", Zone: "North", Region: "",…}