Updating the ajax.data and reloading the datatable

Updating the ajax.data and reloading the datatable

greenafricangreenafrican Posts: 3Questions: 2Answers: 0

I have a datatable (1.10) that uses an AJAX data source, which accepts a parameter, as below:

    var table = $('#example').DataTable( {
        ajax: {
            url: "http://example.com/end/point",
            data: {"id": "12345"}
    } );

This works really well.

I have a chart on the page, which when clicked must reload the datatable with the "id" from the chart element value.

Ideally, i'd like to use something similar to this but with the new "id" parameter included.

table.ajax.url( "http://example.com/end/point").load();

I have tried:

var idSettings = table.settings();
$.extend(true, {}, idSettings, {ajax:{data:{"id":"54321"}}});
table.ajax.reload();

and

var idSettings = table.settings();
idSettings.ajax.data = {"id":"54321"};
table.ajax.reload();

But neither works and when I inspect the settings object the ajax.data hasn't been updated.

Is there anyway to use the API to reload the data based on a new "id" parameter?

(I'd prefer not to chain the id onto the URL as a string as it feels clumsy)

Answers

  • wjhumphreyswjhumphreys Posts: 52Questions: 9Answers: 5
    edited August 2014
    "ajax": {
      "url": ""http://example.com/end/point"",
      "type": "POST",  // POST or GET here
      "data": function ( d ) {
        d.id = $("#YOUR_CHART_ID").val();
      }
    },
    
    // Re-draw the table.
    YOURTABLE.draw();
    
This discussion has been closed.