dataTables support which format JSON?

dataTables support which format JSON?

hybluseahyblusea Posts: 8Questions: 0Answers: 0
edited November 2012 in General
The server-side is C #, MVC. the server returns the Json like this:
{
"sEcho": "1",
"iTotalRecords": 48,
"iTotalDisplayRecords": 48,
"aaData": [
{
"ID": "50",
"SN": "000111222350",
"RECEIVE_TIME": "2001/10/30 14:00:00",
"MSG_CONTENT": "abcdefg",
"MSG_TYPE": "1",
"MSG_SNEDER": "05",
"MSG_RECEVER": null
}
]
}

but datatables throws a warning:Requested unknown parameter '0' from the data source for row 0.

Replies

  • girishmrgirishmr Posts: 137Questions: 0Answers: 0
    Check your markup columns and the data being sent back. Columns should match.
  • hybluseahyblusea Posts: 8Questions: 0Answers: 0
    quote]girishmr said:
    Check your markup columns and the data being sent back.
    Columns should match. [/quote]

    [code]"aoColumns": [
    { "sName": "ID" },
    { "sName": "SN" },
    { "sName": "RECEIVE_TIME" },
    { "sName": "MSG_CONTENT" },
    { "sName": "MSG_TYPE" },
    { "sName": "MSG_SENDER" },
    { "sName": "MSG_RECEVER" }
    ][/code]

    The client and server defined column match..
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    > "MSG_RECEVER": null

    How should `null` be displayed? An empty string usually, but `null !== ''` (unless you are Oracle). So you need to use the sDefaultContent option to tell DataTables what to do with null data.

    Allan
  • hybluseahyblusea Posts: 8Questions: 0Answers: 0
    [quote]allan said:
    "MSG_RECEVER": null[/quote]

    I have set the default values in to the database, there is no null value in json, but the problem still occurs.
    [code]{"sEcho":"1","iTotalRecords":48,"iTotalDisplayRecords":48,"aaData":[{"ID":"50","SN":"000111222350","RECEIVE_TIME":"2001/10/30 14:00:00","MSG_CONTENT":"23asdf134a","MSG_TYPE":"1","MSG_SNEDER":"05","MSG_RECEVER":"1"}}]}[/code]
  • girishmrgirishmr Posts: 137Questions: 0Answers: 0
    Can you share the piece of code that prepares the data to be sent back (aaData[]) and the markup?

    Sometimes when you retrieve data from DB and it is NULL you have to set spaces ($tr [] = ' ' ) in your server side script before encoding and sending it back to the client to match an empty field on the markup

    Sample code given below. Without complete information it is difficult to find a solution but hoping this might provide some help.


    [code]

    while($aDetailTxn = mysql_fetch_assoc($active_dtxn_result)) {
    $tr = array();
    foreach($aDetailTxn as $key => $value) {
    if($key == 'TRANS_DETAIL_ID') {
    $tr[] = $aDetailTxn['TRANS_DETAIL_ID'];
    $tr[] = $aDetailTxn['TRANS_ID'];
    $tr[] = $aDetailTxn['ACCOUNT_ID'];
    $tr[] = $aDetailTxn['ACCOUNT_NAME'];

    $invoiceDate = $oDate->getMysqldate2date($aDetailTxn['INVOICE_DATE']);
    $tr[] = $invoiceDate;

    $tr[] = $aDetailTxn['INVOICE_NUMBER'];

    if(is_null($aDetailTxn['FROM_DATE']) && is_null($aDetailTxn['TO_DATE'])) {
    $tr[] = ''; //This is what I described above
    $tr[] = '';
    } else {
    $invoiceFromDate = $oDate->getMysqldate2date($aDetailTxn['FROM_DATE']);
    $tr[] = $invoiceFromDate;
    $invoiceToDate = $oDate->getMysqldate2date($aDetailTxn['TO_DATE']);
    $tr[] = $invoiceToDate;

    }

    $tr[] = $aDetailTxn['AMOUNT'];
    $tr[] = $aDetailTxn['REMARKS'];
    }
    }
    $data['aaData'][] = $tr;
    }
    [/code]
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    @hyblusea - The JSON you provided above is invalid which isn't going to help.
  • hybluseahyblusea Posts: 8Questions: 0Answers: 0
    I think I found the reason is MSG_SENDER field name wrong. Further, in the client, aoColumnDefs need such definitions:
    [code]"aoColumns": [
    { "mData": "ID" },
    { "mData": "SN" },
    { "mData": "RECEIVE_TIME" },
    { "mData": "MSG_CONTENT" },
    { "mData": "MSG_TYPE" },
    { "mData": "MSG_SENDER" },
    { "mData": "MSG_RECEVER" }
    ][/code]

    Now seems to have been solved. thanks girishmr and allan
  • girishmrgirishmr Posts: 137Questions: 0Answers: 0
    Should return something like this - (Run it in JSONlint)
    [code]
    {
    {
    "sEcho": "1",
    "iTotalRecords": 48,
    "iTotalDisplayRecords": 48,
    "aaData": [
    [
    "50",
    "000111222350",
    "2001/10/30 14:00:00",
    "23asdf134a",
    "1",
    "05",
    "1"
    ]
    ]
    }}
    [/code]


    And your JSON is of the following format. Fields are prefixed with the data. Something to do with your serverside script I hope.

    [code]
    {
    "sEcho": "1",
    "iTotalRecords": 48,
    "iTotalDisplayRecords": 48,
    "aaData": [
    [
    "ID": "50",
    "SN": "000111222350",
    "RECEIVE_TIME": "2001/10/30 14:00:00",
    "MSG_CONTENT": "23asdf134a",
    "MSG_TYPE": "1",
    "MSG_SNEDER": "05",
    "MSG_RECEVER": "1"
    ]

    }
    ]
    }
    [/code]
  • girishmrgirishmr Posts: 137Questions: 0Answers: 0
    Oh great
This discussion has been closed.