1

I'm populating a 'DataTable' table via ajax and it works fine. However, I'd like to also return a pipe-separated serialised string of unique IDs which form the rows to be displayed. I'm using the dataSrc option in a standard ajax call:

$('#displayRecords').dataTable( {
    "lengthMenu": [ [10, 25, 50, 100, -1], [10, 25, 50, 100, "All"] ],
    "ajax": {
    "url": "globalJSON/recordsTable.php",
    "dataSrc": "recordsTable"
    }
});

So that works but on top of the recordsTable array within the json return, I'd also like to fetch the string 'recordsSerialised' and obviously use it in a wider scope.

I think using the ajax call as function is the answer but I am not sure how to implement it. The documentation reads as if I just fetch the json via an alternative $.ajax, $.post, $.whatever and pass the return into the function variables. Can anyone verify that I'm on the right lines. If not, any suggestions or corrections would be great.

1 Answer 1

1

You're on the right track. What you can do is call a function for dataSrc, as long as you return the appropriate data for the DataTable. Say your json response contains 2 arrays, recordsTable and recordsSerialized

$('#displayRecords').dataTable({
  "ajax": {
    "lengthMenu": [ [10, 25, 50, 100, -1], [10, 25, 50, 100, "All"] ],
    "url": "globalJSON/recordsTable.php",
    "dataSrc": function (json) {
      // do something with json.recordsSerialized here
      return json.recordsTable;
    }
  }
});

I'm sure you've been looking at this already but here's the link to the documentation anyway: https://datatables.net/reference/option/ajax

Sign up to request clarification or add additional context in comments.

1 Comment

Bingo. Exactly what I needed, many thanks. And yes, been reading the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.