Datatables(jquery插件)没有用AJAX填充表,返回有效的JSON

时间:2016-05-02 19:07:12

标签: php json datatables symfony-1.4

我正在尝试使用DataTable jquery插件填充此HTML表,使用从php脚本接收此JSON的AJAX调用,

来自服务器的响应:

changeCell

HTML文件:

{
    "newsletters": [{
        "anio": "2016",
        "mes": "1",
        "quincena": "1"
    }, {
        "anio": "2016",
        "mes": "1",
        "quincena": "2"
    }]
}

然后我的php服务(在symfony 1.4中制作),正如我之前所说,根据JSON在线验证器返回正确的JSON:

<div id="tabla_newsletters" >
     <table id="newsletter_datatable">
         <thead>
            <tr>
                <th>anio</th>
                <th>mes</th>
                <th>quincena</th>
            </tr>
        </thead>
        <tbody>

        </tbody>

     </table>
</div>


<script>
$(document).ready(function(){   


            var table = $('#newsletter_datatable').DataTable( {
                ajax: {
                    url: '/newsletter/getNewsletters',
                    dataSrc: 'newsletters'
                },
                columns:[
                          { 'newsletters': 'anio' },
                          { 'newsletters': 'mes' },
                          { 'newsletters': 'quincena' }
                    ],    
            } );

    });
</script>

Datatable会引发以下错误:

“DataTables警告:table id = newsletter_datatable - 第0行请求的未知参数'0'。有关此错误的详细信息,请参阅http://datatables.net/tn/4

我看其他案例,但乍一看他们是不同的案件......

编辑:我已经解决了....问题出现在服务器上的脚本上,索引需要是数字....现在它正确填充表格

最终代码:

- 现在来自服务器的响应(显然插件只接受我从其他人的bug看到的格式):

 public function executeGetNewsletters(sfWebRequest $request){

    $conn = Doctrine_Manager::getInstance()->getCurrentConnection();

    $qry=$conn->execute("Select anio,mes,quincena from newsletters");

            $newsletters = $qry;

            $dataNews = array();
            $i=0;
                foreach ($newsletters as $news)
                    {

                           $dataNews[$i] = array(
                                "anio" => $news['anio'],
                                "mes" => $news['mes'],
                                "quincena" => $news['quincena'],
                            );
                      ++$i;
                    }

            $output = array(
                "newsletters" => $dataNews,
            );

            $json=$this->renderText(json_encode($output));
            return $json;




  }

-HTML文件代码是:

{"newsletters":[["2016","1","1"],["2016","1","2"]]}

和php服务代码是(我只是将关联数组的索引从字段名称更改为数字):

<div id="tabla_newsletters" >
     <table id="newsletter_datatable">
         <thead>
            <tr>
                <th>anio</th>
                <th>mes</th>
                <th>quincena</th>
            </tr>
        </thead>
        <tbody>

        </tbody>

     </table>
</div>


<script>
$(document).ready(function(){   


            var table = $('#newsletter_datatable').DataTable( {
                ajax: {
                    url: '/newsletter/getNewsletters',
                    dataSrc: 'newsletters'
                }
            } );

    });
</script>

1 个答案:

答案 0 :(得分:1)

仅供参考:您可以通过正确的方式解决原始问题:

columns:[
  { data: 'anio' }, //NOT  { 'newsletters': 'anio' }
  { data: 'mes' },
  { data: 'quincena' }
],  

使用data定义应该进入该列的每个简报项目中的哪个属性。