【问题标题】:Server-side processing jquery datatables in Zend FrameworkZend 框架中的服务器端处理 jquery 数据表
【发布时间】:2013-09-24 19:56:59
【问题描述】:

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

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

在我的结果控制器中:

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 格式错误引起的。

这是我在回复中得到的:

或查看here

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

【问题讨论】:

    标签: php jquery zend-framework datatables server-side


    【解决方案1】:

    有一个动作助手 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);
    }
    

    【讨论】:

    • 谢谢!但是更改代码后我仍然遇到相同的错误...。你知道如何在 javascript 中检查我返回的 json 吗?
    • 如果你有萤火虫,你可以很容易地做到这一点。打开你的萤火虫网络面板,看看发送了什么请求。如果您没有使用 post 方法而不是右键单击请求并单击新选项卡中的打开响应,如果 json 输出正确,则问题出在您的前端,否则问题出在您的服务器端代码
    • 添加问题的响应截图
    • 点击我正在获取预览标签的响应标签
    • @nielsv 检查现在更改您的 sAjaxSource 变量
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 2018-11-24
    相关资源
    最近更新 更多