0

I'm trying to generate a HTML data table from the result of a SQL query execution. The resulting data is in JSON format. I'm using the plugin "Datatables" to achieve this. I'm following this example

I don't get an error but the data table is empty. I'm obviously doing something wrong or missing something.

Here's the code excerpt. Could I please get some guidance on to the right path please.

function jsDataPlot(chartProps) {
    // Get the array from the element:
    var graphPropsStore = chartProps;

    // Loop through the array with the jQuery each function:
    $.each(graphPropsStore, function (k, graphPropsStoreProperty) {

        // The makeCall function returns a ajaxObject so the object gets put in var promise
        var dbResAjx = getResultFromSql(k);

        // Now fill the success function in this ajaxObject (could also use .error() or .done() )
        dbResAjx.success(function (response) {
            console.log(response);
            // When success, call the function and use the values out of the array above
            $('#divId').DataTable(response);
        });

        dbResAjx.error(function (response) {
            console.log(response);
        });
    });
}


    function getResultFromSql(paramId) {
// bla bla code
        return $.ajax({
            url: 'runMySqlQuery.php',
            type: 'post',
            data: {// some POST params}
        });
    }

JSON Result

[{"DATE":"2015-12-15","TYPE":"AAA","NAME":"asdasd"},{"DATE":"2015-12-15","TYPE":"BBB","NAME":"dsfsdfsdfsdf"},{"DATE":"2015-12-15","TYPE":"AAA","NAME":"reterter"},{"DATE":"2015-12-15","TYPE":"CCC","NAME":"ertertertert"}]
5
  • You can't call the DataTable() function directly with your data. See here for more information on how to set up datatables with AJAX. Commented Feb 24, 2016 at 10:45
  • #divId is your id of table?, if it is true you have to pass the parameter data $('#divId').DataTable({data : response});. Commented Feb 24, 2016 at 14:21
  • Thankyou Cmedina. It still doesn't work. I get an index error that points me to look at http://datatables.net/manual/tech-notes/4 Commented Feb 24, 2016 at 18:23
  • @usert4jju7 you can show the content of your response Commented Feb 24, 2016 at 21:07
  • Thank you CMedina. I've updated the question Commented Feb 24, 2016 at 21:14

1 Answer 1

1

OK, in your JSON yu have this. DATE - TYPE - NAME

 [
    {"DATE":"2015-12-15","TYPE":"AAA","NAME":"asdasd"},
    {"DATE":"2015-12-15","TYPE":"BBB","NAME":"dsfsdfsdfsdf"},
    {"DATE":"2015-12-15","TYPE":"AAA","NAME":"reterter"},
    {"DATE":"2015-12-15","TYPE":"CCC","NAME":"ertertertert"}
 ]

then in your JS should define your columns ....

$('#divId').DataTable({
  columns : [
      {data: "DATE"},
      {data: "TYPE"},
      {data: "NAME"}
  ],
    data: response
});

example: https://jsfiddle.net/cmedina/7kfmyw6x/4/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.