No RECORD

No RECORD

Massimo74Massimo74 Posts: 13Questions: 0Answers: 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: 4,686Questions: 0Answers: 822

    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: 13Questions: 0Answers: 0

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

  • kthorngrenkthorngren Posts: 5,541Questions: 19Answers: 1,232

    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: 13Questions: 0Answers: 0

    i find "[]"

  • Massimo74Massimo74 Posts: 13Questions: 0Answers: 0
    edited May 17

    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: 5,541Questions: 19Answers: 1,232
    edited May 17

    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: 13Questions: 0Answers: 0

    ok solved thanks

Sign In or Register to comment.