I don't konw how to load the another JSON data, please help

I don't konw how to load the another JSON data, please help

vinsonvinson Posts: 22Questions: 0Answers: 0
edited February 2012 in General
i know how to load a json data which format like:
{ "aaData": [
["USA","300,000","880","2011","cc","/217180/2C1CDB7C5E96CD52C.JPG"]
] }

but i try many times, i still don't understand how to load a json data format like:
{"aaData": [
{
"area":"usa",
"size":"300,000",
"street_num":"880",
"year":"2011",
"icon_path":"/217180/2C1CDB7C5E96CD52C.JPG"
}
]}


Please help!!

I have a look a example http://datatables.net/release-datatables/examples/server_side/post.html
but i don't know "scripts/post.php" this file's path, and it can't be run in my localhost, which i download from the "DataTables-1.9.0.rar"

Replies

  • hlamprechthlamprecht Posts: 2Questions: 0Answers: 0
    Hi,

    have you set "aoColumns" at initialization?

    [code]
    [{"mDataProp":"area"},{"mDataProp":"size"},{"mDataProp":"street_num"},{"mDataProp":"year"},{"mDataProp":"icon_path"}]
    [/code]
  • vinsonvinson Posts: 22Questions: 0Answers: 0
    hi hlamprecht,

    thank u for ur helping,
    i set "aoColumns" at initialization, but it doesn't work and return a datatable warning:
    DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Pass your returned JSON through http://jsonlint.com - you can get the return from the server in Firefox / Inspector.

    Allan
  • vinsonvinson Posts: 22Questions: 0Answers: 0
    My code:
    $('#example').dataTable( {
    "aoColumns":[{"mDataProp":"area"},{"mDataProp":"building_name"},{"mDataProp":"street_num"},{"mDataProp":"build_sqft"},{"mDataProp":"icon_path"}],
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": 'posts.php'
    } );

    posts.php content:

    <?php
    $json='
    {"searchresults": [
    {
    "area":"usa",
    "building_name":"First",
    "street_name":"street one",
    "build_sqft":"1000",
    "icon_path":"http://farm8.staticflickr.com/7016/6835796043_f7275cf2cb_m.jpg"
    },
    {
    "area":"Canda",
    "building_name":"Two",
    "street_name":"street two",
    "build_sqft":"1500",
    "icon_path":"http://farm8.staticflickr.com/7016/6835796043_f7275cf2cb_m.jpg"
    },
    {
    "area":"Aus",
    "building_name":"Third",
    "street_name":"street three",
    "build_sqft":"2000",
    "icon_path":"http://farm8.staticflickr.com/7016/6835796043_f7275cf2cb_m.jpg"
    }
    ]}
    ';

    echo $json;


    I check the return JSON through http://jsonlint.com, the result is Valid JSON
  • vinsonvinson Posts: 22Questions: 0Answers: 0
    problem fix, thank u for ur guys' reply, the problem is i add:

    "sEcho": 3,
    "iTotalRecords": 2,
    "iTotalDisplayRecords": 2,

    in the json title and set "aoColumns" at initialization.

    ^.^
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    I suspect you also need to set sServerDataProp since by default it will look for aaData and not "searchresults".

    Allan
  • vinsonvinson Posts: 22Questions: 0Answers: 0
    hi allan,

    i have corrected the the json data name to aaData,

    it load the json data is ok, but can't be sort and search in the datatable default sorting and searching

    Help!!
  • vinsonvinson Posts: 22Questions: 0Answers: 0
    when i click the sorting or searching, the datatable stay in "Processing ............."
  • vinsonvinson Posts: 22Questions: 0Answers: 0
    function fnFormatDetails ( oTable, nTr )
    {
    var aData = oTable.fnGetData( nTr );
    var sOut = '';
    sOut += 'Rendering engine:'+aData[1]+' '+aData[4]+'';
    sOut += 'Link to source:Could provide a link here';
    sOut += 'Extra info:
  • vinsonvinson Posts: 22Questions: 0Answers: 0
    problem fix
    i delete "bServerSide": true, the datatable's sorting and searching become work, but i don't know the reason.
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Probably because you hadn't fully implemented server-side processing on the server-side, as required by DataTables: http://datatables.net/usage/server-side . You need to have the server do the sorting, filtering etc when server-side processing and also sEcho should be echoed back to the client.

    Sounds like you want client-side processing though, so yes, removing bServerSide is the correct thing to do.

    Allan
  • vinsonvinson Posts: 22Questions: 0Answers: 0
    thank u Allan
This discussion has been closed.