DataTable with JS Source

956 Views Asked by At

I'm trying to generate some data with Sage Sdata using javascript and after that handle that data with datatables plugin but I'm getting error:

DataTables warning: table id=example - Requested unknown parameter '1' for row 0. For more information about this error, please see http://datatables.net/tn/4

if(aNode2)
{
    resultTextData += '[';
    resultTextData += "'" + aNode1.nodeValue + "'";
    resultTextData += ',';
    resultTextData += "'" +  aNode2.nodeValue + "'";
    resultTextData += ',';
    resultTextData += "'" + aNode3.nodeValue + "'";
    resultTextData += ']';
    resultTextData += ',';
}

var dataSet =   resultTextData ;

console.log(dataSet);

$('#example').dataTable({
    "data": dataSet,
    "aoColumns": [
        { "aDataSort": [ 0, 1 ] },
        { "aDataSort": [ 1, 0 ] },
        { "aDataSort": [ 2, 3 ] }
    ]

});
2

There are 2 best solutions below

0
On

the data option doesn't not accept string inputs. See https://datatables.net/reference/option/data

0
On

DataTables support loading from json-encoded string. Here is Example of converting XML result to array.

function FormatDataByIolist($data,$IOlist) {
    $returnAray = array();
    $RecordCount = count($data);
    for($i = 0; $i < $RecordCount; $i++) { 
        $tmpSingleRow = array();
        foreach($IOlist as $colName) {
            $columVal = (string)$data[$i]->$colName;
            $tmpSingleRow[$colName] = $columVal;
        }
        array_push($returnAray,$tmpSingleRow);
        unset($tmpSingleRow);
    }
    return $returnAray;
}

After it you need to convert array to json_encoded string that DataTable can understand.

$formatted = '{ "data": '.json_encode($returnAray).'}';

After writing this in backend you need to add following row in your datatable definition

"ajax": "your.php?action=getJson"

your.php?action=getJson Method should be GET and should return formatted json-string ( as shown ).

I thnik, this repository will help you to work with SData. ( It is model for working with SData ) https://github.com/AramKocharyan/Sage-SData