2014-12-23 90 views
2

我遇到了麻烦,在我的代码中发现问题。用php发送json填充数据表

我正在做一个搜索脚本,我想在Datatable中显示结果。我有一个搜索表单,将数据发送到我的php文件并返回查询结果的json_encode,然后在ajax成功时,我构建了我的表,将结果传递给“data”:response。

查询工作得很好,当我没有Datatables,但因为我需要分页和东西,我需要它在Datatables上工作。

为HTML表:

<div id="results"> 

     <table id="example" class="display compact hover stripe" cellspacing="0" width="100%"> 
      <thead> 

       <th>Cognome</th> 
       <th>Nome</th> 
       <th>Telefono</th> 
       <th>Provincia</th> 
       <th>Tipo Cliente</th> 
       <th>Amministrazione</th> 
       <th>Stato</th> 
       <th>Fonte</th> 
       <th>Data Ricezione</th> 
       <th></th> 

      </thead>   
     </table> 

    </div> 

的JavaScript数据表:

$('#submit_src').on("click", function() { 

    $.ajax({ 
     data: $("#ricerca").serialize(), 
     type: $("#ricerca").attr('method'), 
     url: $("#ricerca").attr('action'), 
     success: function(response) { 

      $('#example').dataTable({ 
       "data": response, 
       "sPaginationType": "listbox", 
       "bFilter": false, 
       "jQueryUI": true, 
       "processing": true, 
       'iDisplayLength': 25, 
      }); 
     } 

    }); 
    return false; 
}); 

“submit_src” 是我的按钮提交ofcourse和 “ricerca” 是我的表单的名称。

JSON的代码中,我得到的是:

{"sEcho":0,"iTotalRecords":"35036","iTotalDisplayRecords":"1","aaData":[["stuff","stuff","stuff","stuff","stuff","stuff","stuff","stuff","stuff","stuff"]]} 

错误:

DataTables warning: table id=example - Requested unknown parameter '1' for row 0. 

回答

0

我相信,以便它知道在哪里把返回的数据<tbody></tbody>需要用数据表。尝试添加到您的table#example

<table id="example" class="display compact hover stripe" cellspacing="0" width="100%"> 
     <thead> 

      <th>Cognome</th> 
      <th>Nome</th> 
      <th>Telefono</th> 
      <th>Provincia</th> 
      <th>Tipo Cliente</th> 
      <th>Amministrazione</th> 
      <th>Stato</th> 
      <th>Fonte</th> 
      <th>Data Ricezione</th> 
      <th></th> 

     </thead> 
     <tbody></tbody>   
    </table> 
+0

对不起,它没有工作。提交表单后,我添加了从警报中获得的错误,因此更容易进行调试。 – Dankorw

+0

将'response'写入'response ['aaData']'时会发生什么? – Tomanow

+0

错误未显示,但该表未填充。警报响应['aaData']显示'未定义'。 – Dankorw

1

此链接可能有助于您试图完成什么:http://datatables.net/release-datatables/examples/ajax/objects.html。它解释说DataTables需要数据的数组;但是,为了使用这些对象,可以使用columns.data选项来完成。

我也用DataTables没有<tbody>,所以这不应该是一个问题。

编辑: 试试下面,我能得到的 '东西' 在表中显示:

$('#example').dataTable({ 
    "data": response.aaData 
}); 

编辑2:

jQuery(document).ready(function() { 

    var response = { 
     "sEcho":0,"iTotalRecords":"35036","iTotalDisplayRecords":"1", 
     "aaData": [ 
     ["stuff","stuff","stuff","stuff","stuff","stuff","stuff","stuff","stuff","stuff"]] 
    }; 

    $('#example').dataTable({ 
     "data": response.aaData, 
     //"sPaginationType": "listbox", 
     "bFilter": false, 
     "jQueryUI": true, 
     "processing": true, 
     'iDisplayLength': 25 
    }); 

}); 
+0

我试过使用'response.aaData',但我仍然没有得到任何错误,并且表中没有填充。由于查询工作正常,并且json被正确地发送给我的脚本,所以我猜它可能是我的json的格式,这是不正确的,或者我正在以错误的方式处理它。 – Dankorw

+0

@Dankorw尝试我的第二个编辑。你的json应该没问题,我的反应与你的完全一致。我想知道是否该问题与该sPaginationType有关,因为直到我评论为止,我无法使用它。 –

+0

它看起来像sPaginationType有两个选项,'two_button'和'full_numbers'。当我把:'sPaginationType':'full_numbers'它的工作。 –

1

你可能会错过选项

 dataType : "json", 

对你的代码。如果没有指定,这可能会混淆从ajax脚本收到的数据类型。