我正在从数据库中获取记录并将数据转换为json数组。我正确地得到json响应。但表格在我的视图中未显示任何条目。谢谢。

我的控制器



function test() {


    $list = $this->get_data_model->get_apartment_details();
        $data = array();
        $no = $_POST['start'];
        foreach ($list as $apartments) {
            $no++;
            $row = array();
            $row[] = $no;
            $row[] = $apartments->a_id;
            $row[] = $apartments->a_name;
            $data[] = $row;
        }

           $output = array(
                "draw"                    =>     5,
                "recordsTotal"          =>      2,
                "recordsFiltered"     =>     1,
                "data"                    =>    $data
           );

		echo json_encode($output);//
	}





我的看法



<section  class="tab-pane fade in active" id="newPanel">

                  <table style='width:100%'' class='table' id='example'>
                        <thead>
                            <tr>
                             <th> ID </th>
                             <th> Name </th>
                             <th> Activate  </th>
                             <th> Edit </th>
                             </tr>
                        </thead>


                   </table>
	            </section>





我的ajax电话



$('#example').DataTable( {

        "processing" : true,
        "serverSide" : true,
        "ajax" : {

                   "type" : "POST",
                   "url": "<?php echo base_url("apartments/test");?>",
                   "dataSrc" : ""

                 },

       "columns": [
                 { "data": "a_id"},
                 { "data": "a_name" }

            ],


		"dom": 'Bfrtip',
        "buttons": [
            {
                "extend": 'copyHtml5',
                "exportOptions": {
                 "columns": ':contains("Office")'
                }
            },
            'excelHtml5',
            'csvHtml5',
            'pdfHtml5'
        ]


    } );
} );

最佳答案

您的表ID属性和Ajax调用ID属性是不同的。希望下面的代码对您有所帮助。

$('#newPanel').DataTable( {

        "processing" : true,
        "serverSide" : true,
        "ajax" : {

                   "type" : "POST",
                   "url": "<?php echo base_url("apartments/test");?>",
                   "dataSrc" : ""

                 },

       "columns": [
                 { "data": "a_id"},
                 { "data": "a_name" }

            ],


        "dom": 'Bfrtip',
        "buttons": [
            {
                "extend": 'copyHtml5',
                "exportOptions": {
                 "columns": ':contains("Office")'
                }
            },
            'excelHtml5',
            'csvHtml5',
            'pdfHtml5'
        ]


    } );
} );

10-08 07:06
查看更多