1

I am trying to implement jQuery DataTables into a "new" form with Zend Framework, I set up the following action in the controller:

public function ajaxFindQuotesAction()
{
    $this        -> setNoRender();
    $quoteTable  =  new Model_QuotesTable();
    $select      =  $quoteTable->select();
    $select      -> from($quoteTable, array('qte_id', 'qte_description'));
    $rows        =  $quoteTable->fetchAll($select);
    $json        =  Zend_Json::encode($rows->toArray());
    echo($json);
}

I also set up in the view the following code:

<?php $this->inlineScript()->captureStart(); ?>
    $(document).ready(function() {
        $('#example').dataTable( {
            "bProcessing": true,
            "sAjaxSource": '/jobs/ajax-find-quotes'
        } );
    } );
<?php $this->inlineScript()->captureEnd(); ?>

<table class="display dataTable" id="example" >
        <thead>
            <tr>
                <th>ID</th>
                <th>Title</th>
            </tr>
         </thead>
</table>

Problem is, the current JSON output is the following:

[
    {
        "column_1":"value 1",
        "column_2":"value 2"
    },
    {
        "qte_id":"3",
        "qte_description":"go to the zoo"
    }
]

While DataTables to work wants this format (copied from the example file):

{
  "aaData": [
    [
      "value 1",
      "value 2"
    ],
    [
      "Trident",
      "Internet Explorer 5.0"
    ]
  ]
}

Any ideas? Thank you very much in advance

1 Answer 1

2

Try:

$data = array_values(array_map('array_values', $rows->toArray()));
$json = Zend_Json::encode(array_combine(
            array('aaData'),
            array($data)
        ));

You need to encode an object, which consists of rows and fields as arrays. The problem is that PHP's so-called arrays are actually ordered maps, so you need to get rid of the associative keys to have arrays in resulting JSON.

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.