Have script that already produces JSON, need to call it and load table

Have script that already produces JSON, need to call it and load table

MrBaseball34MrBaseball34 Posts: 96Questions: 0Answers: 0
edited August 2011 in General
I have a script, in ExpressionEngine, that I call like this in my current javascript ajax:
[code]



$.ajax({
url :"/events_json/0/" + page + '/' + state_list+ '/' +month_list + '/0/' + program_list + '/' + zip_code + '/' + miles,
data: {},
dataType: 'json',
type:"POST",
async: false,
cache:false,
...
});
[/code]

I want to be able to call this and load a DataTable but not sure how to do it. Taking a look at the JSONP example, It make the DB call itself and I don't want any of that processing. The page parameter in this URL handles pagination. How would I handle passing the other parameters, state_list, month_list, program_list, zip_code and miles?
Here is a sample of the data returned, the event_count value is the total number avail for pagination there are 15 per page, I am only showing 3.
[code]



{"events":{"NCAO": {"Prog_Code":"AO","Ev_Code":"NCAO","F_Date":"August 23, 2011",
"L_City":"Asheville","L_State":"NC", "SeatsAvail":"21"},
"TXIC": {"Prog_Code":"IC","Ev_Code":"TXIC","F_Date":"August 23, 2011",
"L_City":"San Antonio","L_State":"TX","SeatsAvail":"22"},
"GAIP": {"Prog_Code":"IP","Ev_Code":"GAIP","F_Date":"August 23, 2011",
"L_City":"Atlanta","L_State":"GA","SeatsAvail":"18"}
},
"event_count":500
}
[/code]

Replies

  • MrBaseball34MrBaseball34 Posts: 96Questions: 0Answers: 0
    I think I may have something but wanted to know how to use the aoData parameter of fnServerData to pass values to the server-side script. How does the script get them and how to parse them into variables?
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    DataTables itself creates the aoData array of objects and passes it to fnServerData

    if you override fnServerData in the init, your function will get the aoData array. the aoData array is full of key/value pairs that will be sent as parameters to the server side script. to add more parameters, simply add more objects to the array, being sure to use the form { "name": "... param name", "value": ".. your value" }

    [code]
    /* POST data to server */
    $(document).ready(function() {
    $('#example').dataTable( {
    "sAjaxSource": "data_source.php",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    /* Add some data to send to the source, and send as 'POST' */
    aoData.push( { "name": "my_field", "value": "my_value" } );
    aoData.push( { "name": "another_field", "value": "another_value" } );

    $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": fnCallback
    } );
    }
    } );
    } );
    [/code]

    you could do much more, like remove params from the array, or perform actions before or after sending the AJAX request to the server script.

    ----

    [quote]MrBaseball34 said: How does the script get them and how to parse them into variables?[/quote]

    this, of course, depends on which server side language you are using. In PHP you get them from your $_GET[] array (if using HTTP GET) or $_POST[] array (if using POST)

    [code]
    if (isset($_POST["my_field"])) $my_field = $_POST["my_field"]; // get my_value
    if (isset($_POST["another_field"])) $my_field = $_POST["another_field"]; // get another_value
    [/code]
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    to get an idea of what DataTables puts into the aoData object, take a look at the left hand side table on this page:

    http://www.datatables.net/usage/server-side
This discussion has been closed.