datatable server side pagination with Dynamic Columns
datatable server side pagination with Dynamic Columns
Dear Team,
In my scenario, columns are dynamic and should fetch from database. But it is not working if i fetch the data as follows.
If i put the columns (response data from URL) directly mapping to datatables it is working perfectly. But in case of building columns server side its not working.
Please help me out to fix this Issue
Please find my code and the server side response below.
var myTable=$('#myTable').DataTable(
{
autoWidth: false,
responsive : true,
serverSide: true,
ajax:{
type: "GET",
url : 'getDynamicData.action'
beforeSend: function (request) {
request.setRequestHeader("x-auth-token", tokenValue);
}
},
destroy: true,
select: {
style: 'multi'
});
My Response from Server is
{
"draw": 2,
"recordsTotal": 1,
"recordsFiltered": 1,
"data": [
{
"vhTxnKey": 58,
"vhTxnNo": "17/0000001",
"vhTxnDate": "2017-08-21",
"vhTxnAccDate": "2017-08-10",
"accPeriod": "June",
"vhPeriodKey": 28,
"vhNotesP": "Update Data",
"vhNotesS": "TEST DATA",
"vhTxnStatus": "N"
}
],
"columns": [
{
"id": 0,
"data": "vhTxnKey",
"title": "SRL No"
},
{
"id": 1,
"data": "vhTxnNo",
"title": "Txn Ref"
},
{
"id": 2,
"data": "vhTxnDate",
"title": "Txn Date"
},
{
"id": 3,
"data": "vhTxnAccDate",
"title": "Accounting Date"
},
{
"id": 4,
"data": "accPeriod",
"title": "Accounting Period"
},
{
"id": 5,
"data": "vhNotesP",
"title": "Notes"
},
{
"id": 6,
"data": "vhNotesS",
"title": "Notes"
},
{
"id": 7,
"data": "vhTxnStatus",
"title": "Status"
}
]
}
Fetching one Record.
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Answers
Hi,
The FAQs have some detail about this:
That is basically what you would need to do if you want to define the columns from the JSON. Also, since you are using server-side processing, the data would not be able to be included in that initial data response (DataTables would ignore it, since it must make its own Ajax call to the server to get the data if you are using server-side processing).
Regards,
Allan