1

Zend Framework を使用して jQuery DataTables を「新しい」フォームに実装しようとしています。コントローラーで次のアクションを設定します。

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);
}

また、ビューに次のコードを設定しました。

<?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>

問題は、現在の JSON 出力が次のようになっていることです。

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

DataTables が機能するには、この形式が必要ですが (サンプル ファイルからコピー):

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

何か案は?事前にどうもありがとうございました

4

1 に答える 1

2

試す:

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

行とフィールドを配列として構成するオブジェクトをエンコードする必要があります。問題は、PHP のいわゆる配列が実際には順序付けられたマップであるため、結果の JSON に配列を含めるには連想キーを取り除く必要があることです。

于 2012-07-12T23:19:31.370 に答える