2017-05-18 21 views
1

我正在從數據庫中提取記錄並將數據轉換爲json數組。我得到正確的JSON響應。但表格顯示我的觀點沒有條目..請看一次。謝謝。如何在codeigniter中做數據表?

我控制器

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 
 
      ); 
 
      
 
\t \t echo json_encode($output);// 
 
\t }

我看來

<section class="tab-pane fade in active" id="newPanel"> 
 
\t    \t 
 
        <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> \t 
 
\t    </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" } 
 
       
 
      ], 
 

 

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

+0

如果你想要你可以使用這個http://opendatatable.com替代數據表 –

回答

0

你的表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' 
     ] 


    }); 
}); 
相關問題