No RECORD

No RECORD

Massimo74Massimo74 Posts: 85Questions: 1Answers: 0

using Ajax sourced data (https://datatables.net/examples/data_sources/ajax) but when I have no record gives me the following error: DataTables warning: table id=table_group - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1.
how can I check it and write a "no record present" message ...THANKS

Replies

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Hi @Massimo74 ,

    The best thing to do would be to ensure the JSON is valid when there's no records - that way DataTables will display that message. You just need to return an empty array, i.e. [],

    Cheers,

    Colin

  • Massimo74Massimo74 Posts: 85Questions: 1Answers: 0

    ok i returned an empty array now error no longer but the word "Loading ..."

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    See if there is an error in your browser's console. Take a look at the XHR response in the browser's developer tools.

    What do you find?

    Kevin

  • Massimo74Massimo74 Posts: 85Questions: 1Answers: 0

    i find "[]"

  • Massimo74Massimo74 Posts: 85Questions: 1Answers: 0
    edited May 2019

    Code PHP

    $result = mysqli_query($connect, $query);
    if ($result->num_rows > 0) {
       while($row = $result->fetch_assoc()) {
               $data[]=$row;
               }
               $result = ["sEcho" => 1,
                            "iTotalRecords" => count($data),
                            "iTotalDisplayRecords" => count($data),
                            "aaData" => $data];
             echo json_encode($result);
    }
    else {
      echo json_encode([]);
    }
    
  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736
    edited May 2019

    You still need the other objects like iTotalRecords and aaData. What Colin meant is that your aaData object should be [].

    Looks like you are using the legacy Datatables Server Side Processing as described here:
    https://legacy.datatables.net/usage/server-side

    You are setting "sEcho" => 1, to 1 each time. It should reflect the sEcho parameter sent to the server, not just 1. See the docs for how to handle this parameter.

    Kevin

  • Massimo74Massimo74 Posts: 85Questions: 1Answers: 0

    ok solved thanks

This discussion has been closed.