datatable server side pagination with Dynamic Columns

datatable server side pagination with Dynamic Columns

sanjeevmambatsanjeevmambat Posts: 8Questions: 2Answers: 0
edited August 2017 in Free community support

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

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    Hi,

    The FAQs have some detail about this:

    Q. Can I define my columns in Ajax loaded JSON?
    A. This is not currently a native feature in DataTables (although it is likely to be added in future). Instead, you would need to load the JSON data using $.ajax and assign the column definition array using the columns option.

    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

This discussion has been closed.