How to pass an Ajax URL with params inside a ajax.url() method?

How to pass an Ajax URL with params inside a ajax.url() method?

aranhaqgaranhaqg Posts: 3Questions: 1Answers: 0

Hi guys,

I'm trying to change the url of an existing table and load it. How can I pass params in this ajax.url method?

I wanna do somethig like this:

var oTable;

$(function(){
        
        oTable = $('.sortable').DataTable({
            bRetrieve: true,
            bProcessing: true,
                    bServerSide: true,
                   ajax: 'my_url.php',
                   sPaginationType: 'full_numbers',
            
            });

        $("#my_checkbox").change(function(){
             //Wanna use my new url and params here 
                     contratosTable.ajax.url(new_url, params).load();
        }); 
});

Thanks for attention!

This question has an accepted answers - jump to answer

Answers

  • DirceuNazarethDirceuNazareth Posts: 44Questions: 1Answers: 12
    edited September 2016
    var myCustomParams = {};
    
    var getMyNewParameters = function(){
       return myCustomParams;
    }
    
    var setMyNewParameters = function(params){
       myCustomParams = params;
    }
    
    $('#example').dataTable( {
      "ajax": {
        "url": "data.json",
        "contentType": "application/json",
        "type": "POST",
        "data": function ( d ) {
            var customParams = getMyNewParameters();
            var params = jQuery.isEmptyObject(customParams) ? d : customParams;
            return params;
        }
      }
    } );
    
    $("#my_checkbox").change(function(){
        //Your new parameters will be set
        setMyNewParameters($("#my_checkbox").val());
        contratosTable.ajax.url(new_url).load();
    });
    
    
    

    ajax.data

  • aranhaqgaranhaqg Posts: 3Questions: 1Answers: 0

    I'm sorry, DirceuNazareth.

    But where is contratosTable.ajax.url(new_url, params).load() is supposed to be oTable.ajax.url(new_url, params).load().

    I wanna pass the params at the new URL at the same table (oTable) that was deffined before.

    Could you tell me how to pass params in the .ajax.url method?

    CORRECT CODE:

    var oTable;
     
    $(function(){
             
            oTable = $('.sortable').DataTable({
                bRetrieve: true,
                bProcessing: true,
                        bServerSide: true,
                       ajax: 'my_url.php',
                       sPaginationType: 'full_numbers',
                 
                });
     
            $("#my_checkbox").change(function(){
                 //Wanna use my new url and params here
                         oTable.ajax.url(new_url, params).load();
            });
    });
    
  • DirceuNazarethDirceuNazareth Posts: 44Questions: 1Answers: 12
    Answer ✓

    I am noticing by your code that you may are using the version 1.9 or below... I am not so familiar with that. I change for 1.10 more than year, so I will give the explanation based on 1.10, because they are very well mirrored and you can find the correlations. If that not worth for you, feel free to ignore the comment.

    The DataTable API has a method to get the parameters of some table:

      var myParams = oTable.ajax.url.params ();
    

    If you want to pass the parameter to be get as $_GET in the PHP page you just need to added to your url:

    $("#my_checkbox").change(function(){
     //If you need do that you need a method to convert the myParams object to url string
                         var new_url = "my_url.php?";
                         new_url +="param1=abc";
                         new_url +="param2=def";
                         var finalUri = encodeURIComponent(new_url);
    
                         oTable.ajax.url(finalUri).load();
            });
    
    

    If you want to pass the parameter to be posted and recovery it on PHP using $_POST, it should not change the parameters anyway... but, if you want control the parameters, then you need change your initialization to instead of use ajax: url, use ajax: object,
    like:

                oTable = $('.sortable').DataTable({
                    bRetrieve: true,
                    bProcessing: true,
                    bServerSide: true,
                    ajax:{
                          url: 'my_url.php',
                          type: "POST",
                          data: function(d){
                             var params = jQuery.isEmptyObject(myFunctionParams()) ? d : myFunctionParams();
                                 return params;
                          }
                    }
                    sPaginationType: 'full_numbers',
                  
                });
    
    //and then
    
    var myFunctionParams = function(){
        //should not be necessary for same parameters... just in case.
        return  oTable.ajax.url.params ();
    }
    $("#my_checkbox").change(function(){
        // your parameter will be updated inside of DataTable because of the ajax.data
        oTable.ajax.url(new_url).load();
    });
    
  • aranhaqgaranhaqg Posts: 3Questions: 1Answers: 0

    Thanks!!

This discussion has been closed.