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

时间:2022-10-24 21:34:14

I'm trying to populate this HTML table with DataTable jquery plugin, using an AJAX call receiving this JSON from the php script,

我试图用DataTable jquery插件填充这个HTML表,使用AJAX调用从php脚本接收这个JSON,

Response from the server:

响应从服务器:

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

HTML file:

HTML文件:

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

and then my php service(made in symfony 1.4), as I stated before this is returning a correct JSON according to a JSON online validator:

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

 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;




  }

Datatable throws the following error:

Datatable抛出以下错误:

"DataTables warning: table id=newsletter_datatable - Requested unknown parameter '0' for row 0. For more information about this error, please see http://datatables.net/tn/4"

“DataTables警告:表id=newsletter = datatable—请求的未知参数‘0’作为第0行。有关此错误的更多信息,请参见http://datatables.net/tn/4"

I look at other cases but they were different cases at a first glance.....

我看了其他的案例,但它们是不同的案例。

EDIT: I already solved it....the problem was on the script on the server, the indexes needed to be numbers....now it's populating the table correctly

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

Final code:

最后的代码:

-Response from the server now is(apparently the plugin accepts only this format from what I've seen with other people's bugs):

-现在服务器的响应是(显然插件只接受这种格式,因为我看到其他人的bug):

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

-HTML file code is:

html文件代码:

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

and php service code is(I've only changed the indexes of the associative arrays from field names to numbers):

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

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(
                                "0" => $news['anio'],
                                "1" => $news['mes'],
                                "2" => $news['quincena'],
                            );
                      ++$i;
                    }

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

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




  }

1 个解决方案

#1


1  

FYI : You could have solved your original problem simply by doing it right :

你可以通过正确的做法来解决你最初的问题:

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

Use data to define which attribute from each newsletter item that should go into the column.

使用数据定义应该进入列的每个通讯项目的哪个属性。

#1


1  

FYI : You could have solved your original problem simply by doing it right :

你可以通过正确的做法来解决你最初的问题:

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

Use data to define which attribute from each newsletter item that should go into the column.

使用数据定义应该进入列的每个通讯项目的哪个属性。