繁体   English   中英

Zend Framework中的服务器端处理jquery数据表

[英]Server-side processing jquery datatables in Zend Framework

我正在尝试使用数据表和服务器端处理。 这是我的看法:

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

在我的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 );
}

如您所见,我只是尝试加载诸如“ test”,“ test2”,...之类的字符串。
我首先要正确加载数据,然后创建过滤,排序...。

当我加载这个我只是得到这个错误:

DataTables警告(表ID ='datatables'):DataTables警告:无法解析来自服务器的JSON数据。 这是由JSON格式错误引起的。

这是我的回应:

在此处输入图片说明

在这里检查。

他只显示现有的HTML,但不显示发送的json。

有一个动作帮助程序json输出json,它将处理禁用布局和以正确的标头将数据作为json发送。

我没有在您的代码中看到任何错误,但我建议您这样做

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM