0

I'm trying to work with datatables and server-side processing. This is what I have in my view:

<table id="datatables" class="display">
  <thead>
    <tr>
       <th>Your Name</th>
       <th>Date and Time</th>
       <th>Actions</th>
    </tr>
  </thead>
  <tbody>

  </tbody>
</table>

<script>
$(document).ready( function () {
    // DATATABLES CONFIGURATION
    $('#datatables').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "results/loadReportsAction"
    } );
});
</script>

In my ResultsController :

public function loadReportsAction() {

    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();

    $row = array();

    $output = array(
        "iTotalRecords" => 34,
        "iTotalDisplayRecords" => 34,
        "aaData" => array()
    );

    $sessions = SessionQuery::create()->findByQuizId(3);

    foreach($sessions as $session){
        $row[] = "test";

        $row[] = "test2";
        $row[] = "test3";
        $output['aaData'][] = $row;
        $row = array();
    }
    echo json_encode( $output );
}

As you can see I just try to load strings like "test", "test2", ... .
I first want to make the data loading right and then create the filtering, sorting, ... .

When I load this I just get this error:

DataTables warning (table id = 'datatables'): DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.

This is what I get in my response:

enter image description here

Or check it here .

He just shows the existing HTML but not the json that was sent.

1 Answer 1

3

There is a action helper json to output json which will handle both disabling layout and sending data as json with correct headers.

I don't see anything wrong in your code but I suggest you do this

<table id="datatables" class="display">
  <thead>
    <tr>
       <th>Your Name</th>
       <th>Date and Time</th>
       <th>Actions</th>
    </tr>
  </thead>
  <tbody>

  </tbody>
</table>

<script>
$(document).ready( function () {
    // DATATABLES CONFIGURATION
    $('#datatables').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "results/load-reports"// Change this line
    } );
});
</script>

public function loadReportsAction() {

    $this->_helper->layout->disableLayout();// Comment this line
    $this->_helper->viewRenderer->setNoRender();// Comment this line

    $row = array();

    $output = array(
        "iTotalRecords" => 34,
        "iTotalDisplayRecords" => 34,
        "aaData" => array()
    );

    $sessions = SessionQuery::create()->findByQuizId(3);

    foreach($sessions as $session){
        $row[] = "test";

        $row[] = "test2";
        $row[] = "test3";
        $output['aaData'][] = $row;
        $row = array();
    }
    echo json_encode( $output );// Comment this line
    $this->_helper->json->sendJson($output);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! But I still get the same error with changed code ... . Do you know how I can check my returned json in javascript?
If you have firebug you can do this very easily. Open your firebug net panel and see what request is send. If you are not using post method than right click on the request and click open response in new tab if the json output is coming correctly than the problem is at your frontend else the problem is in your server side code
add the response screenshot with the question
click on the response tab I am getting preview tab
@nielsv check now change your sAjaxSource variable

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.