How to pass new POST parameters on Ajax.reload?

How to pass new POST parameters on Ajax.reload?

venlentinevenlentine Posts: 2Questions: 1Answers: 0

I want to be able to reload my datatable with AJAX, but need to be able to pass POST parameters.
Here is how I initialize the table
$('#list').DataTable( {
"ajax": {
"url": '${ctx}/work/list_ajax.json',
"type": 'POST'
},

Here is how I reload the AJAX source:
$('#list').DataTable().ajax.reload(null, false).draw();

How can I pass new/updated parameters into the ajax.reload call?

This question has accepted answers - jump to:

Answers

  • venlentinevenlentine Posts: 2Questions: 1Answers: 0

    I find solution!

    Custom function to ajax.data

    eg:

     "ajax": {
                    "url": '${ctx}/work/list_ajax.json',
                    "type": 'POST',
                    "data": buildSearchData
                }
    
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Yup - bob on :-). Thanks for posting your solution.

    Allan

  • viandanteviandante Posts: 22Questions: 10Answers: 1
    edited September 2014

    But with this solution... the default parameters, draw, search etc... are not sent...

    I search another solution...

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    But with this solution... the default parameters, draw, search etc... are not sent...

    As long as you have serverSide enabled, then yes they should be. If they are not, please link to a test case showing the problem.

    Allan

  • geniuscdgeniuscd Posts: 2Questions: 0Answers: 0
    edited October 2014

    I can confirm the issue. im using v1.9.3

    using this code

    table.dataTable({
        "processing": true,
            "serverSide": true,        
            "ajax": {
                "url" : "transactions.php",
             "type": "POST",
             "data" : { 
                "cmd" : "refresh",
                "from": $("#from-date")+" "+$("#from-time").val(),
                "to"  : $("#to-date").val()+" "+$("#to-time").val()
            }
    });
    

    when reloading, the "to" and "from" stays the same, even if I change the UI input, so the workaround from valentine works, but now the default params arent sent.

    "data" with buildSearchData function

    function buildSearchData(){
     var obj = {
         "cmd" : "refresh",
                "from": $("#from-date")+" "+$("#from-time").val(),
                "to"  : $("#to-date").val()+" "+$("#to-time").val()
    };
    return obj;
    }
    

    I was thinking of re-typing the defaults params in the function manually.
    but how can i get them?

  • geniuscdgeniuscd Posts: 2Questions: 0Answers: 0
    edited October 2014

    Ok so here is the work around, hope it helps someone

    function buildAjaxData (){
            var settings = $("#transactions").dataTable().fnSettings();
            console.log(settings);
            
            var obj = { 
                //default params
                "draw" : settings.iDraw,
                "start" : settings._iDisplayStart,
                "length" : settings._iDisplayLength,
                "columns" : "",
                "order": "",
                
                "cmd" : "refresh",
                "from": $("#from-date").val()+" "+$("#from-time").val(),
                "to"  : $("#to-date").val()+" "+$("#to-time").val()
                };
                
                //building the columns
                var col = new Array(); // array
                
                for(var index in settings.aoColumns){
                    var data = settings.aoColumns[index];
                    col.push(data.sName);
                                    
                }
                
                var ord = {
                    "column" : settings.aLastSort[0].col,
                    "dir" : settings.aLastSort[0].dir
                };
                
                //assigning
                obj.columns = col;
                obj.order = ord;
                
            return obj;
            
            
        }
    
  • PleyerPleyer Posts: 3Questions: 0Answers: 2
    edited November 2014 Answer ✓
        "ajax": {
            "url": 'vendas_filter.php',
            "type": 'POST',
            "data": $('#formFilter').serializeArray()
        }
    

    But not working, cant "refresh" the data....

  • PleyerPleyer Posts: 3Questions: 0Answers: 2

    i can do that setting a new ajaxurl (via get) but i cant via post..

    var tableAjax = $('#table_vendas').DataTable();

    tableAjax.ajax.url('vendas_filter.php?'+$('#formFilter').serialize()).load();

  • PleyerPleyer Posts: 3Questions: 0Answers: 2
    Answer ✓

    Ok.

    I solved that way:

    "ajax": {
    "url": 'filter.php',
    "type": 'POST',
    "data": function ( d ) {
    return $('#formFilter').serialize();
    }
    },

This discussion has been closed.