mDataProp and returned JSON format

mDataProp and returned JSON format

beginner_beginner_ Posts: 55Questions: 2Answers: 0
edited February 2012 in DataTables 1.8
Hi all,

basically i'm trying to consume a webservice with datatables. From the JSON returned I only want to display certain properties, not all of them.
Is this possible? What about the order of the properties, does it matter?

Using firebug I can see that the request made by datatables is executed successfully and the expected data is returned but nothing is displayed in my table. Also when I change for example the iDisplayLength per page, a new request is made and sEcho is increased by 1.

[code]
"aoColumns": [
{"mDataProp": "structure","sWidth": "200px" },
{"mDataProp": "id", "sWidth": "30px"},
{"mDataProp": "rNumber", "sWidth": "50px"},
{"mDataProp": "desc"}
],
[/code]
Also the return JSON is valid and I double checked the correct spelling of the properties. Any ideas what the issue might be?

Replies

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    > From the JSON returned I only want to display certain properties, not all of them. Is this possible?

    Yes! Just do it they way you have done - mDataProp will only get the properties you ask for.

    > What about the order of the properties, does it matter?

    No - order doesn't matter for objects.

    > Also the return JSON is valid and I double checked the correct spelling of the properties. Any ideas what the issue might be?

    Not from just that I'm afraid :-). Can you also post your JSON please, or ideal a link to your test page.

    Allan
  • beginner_beginner_ Posts: 55Questions: 2Answers: 0
    edited February 2012
    Hi allan, cant point you to a test page (intranet).

    Below the web page:

    Note that the certain custom (fnRender) works perfectly fine on another page that also gets data from JSON using server-side processing. Commenting it out does not resolve the issue.

    [code]
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">









    cd_includeWrapperFile("../local/js/chemdraw/");

    My App




    Title







    $(document).ready(function() {
    var oldStart = 0;
    var displayLength = 2;
    var scrollY = "570";

    $('#result').dataTable( {
    //"bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "../getList",
    "sAjaxDataProp": "aaData",
    "iDisplayLength": displayLength,
    "aLengthMenu": [[1, 2, 3, 10], [1, 2, 3, 10]],
    "sScrollY": scrollY,
    "sPaginationType": "full_numbers",
    "bSort": false,
    "bFilter": false,
    //"bAutoWidth": true,
    "aoColumns": [
    {"mDataProp": "structure","sWidth": "200px"
    "fnRender": function ( oObj ) {

    if (oObj.aData[0] != ''){

    var html = cd_getSpecificObjectTag("chemical/x-cdx", "190", "210",
    "CDCtrl" + oObj.iDataRow,"","True","False","True",
    'data:chemical/cdx;base64,' + oObj.aData[0]);
    return html;
    } else {
    return '';
    }
    }
    },
    {"mDataProp": "id", "sWidth": "30px"},
    {"mDataProp": "rNumber", "sWidth": "50px"},
    {"mDataProp": "desc"}
    ],
    "fnServerData": function ( sSource, aoData, fnCallback ) { );
    var oSettings = this.fnSettings();

    $.ajax( {
    "dataType": 'json',
    "type": "GET",
    "url": sSource + '/' + oSettings._iDisplayStart + '/' + + oSettings._iDisplayLength,
    "data": aoData,
    "success": fnCallback
    } );
    }
    } );
    } );





    Structure
    ID
    R-Number
    Description








    Copyright 2012




    [/code]

    And here the returned JSON:
    [code]
    {
    "sEcho": 1,
    "iTotalRecords": "1881",
    "iTotalDisplayRecords": "1881",
    "aaData": [
    {
    "id": "2",
    "rNumber": "82-3871-0",
    "cas": "4359-48-1",
    "desc": "also 83-7104, 60-7796, 63-1631.",
    "litPat": "",
    "no": "R7909",
    "structure": "very long base64 encoded data"
    },
    {
    "id": "5",
    "rNumber": "57-0003-0",
    "cas": "29355-73-6",
    "desc": "mixture of 1,3-oxolane- and 1,3-oxane-isomers.K-8459.",
    "litPat": "",
    "no": "R9903",
    "structure": "very long base64 encoded data"
    }
    ]
    }
    [/code]


    EDIT:

    Using Firebug I saw that fnRender is never called. I then saved the returned JSON to file, set "sAjaxSource": "data.txt" to that file and bServerSide to false. Then the data is displayed correctly. I then but back my original sAjaxSource while leaving bServerSide on false and it does not work anymore. The issue seems to occur after data is fetched but before it is rendered.
  • beginner_beginner_ Posts: 55Questions: 2Answers: 0
    Found the issue. The data is fetched through a web service which I created with php tonic library. The examples from that library use [code]Response::FOUND[/code] if a resource was found. This sets html status to 302 and jquery or datatables do not "tolerate" this, success function is never called. Just changing it to [code]Response::OK[/code] (= status 200) and everything works as expected.

    Just as I thought. Trivial issue but wasted hours finding it...
This discussion has been closed.