2017-08-30 45 views
1

我試圖使用DataTables將搜索結果顯示到表中。我得到的搜索結果爲JSON,也驗證了對有效的JSON,但我得到這個數據表警告:使用codeigniter的數據表不會將結果添加到表

表ID = volunteer_result - 無效JSON響應。

有關此錯誤的更多信息,請參閱http://datatables.net/tn/1

我已經使用以下碼:

觀點:searchdemo.php

<div class="form-group"> 
    <button id="search_demo" class="btn btn-form">Search</button> 
</div> 
<div id="demo_result_div" class="table-responsive" style="background-color:#fff;"> 
    <table id="demo_result" class="table table-bordered table-striped"> 
     <thead> 
      <tr> 
       <th>Name</th> 
       <th>Role</th> 
      </tr> 
     </thead> 
    </table> 
</div> 

JS:searchdemo.js

var table; 
$('#search_demo').click(function() { 
    //datatables 
    table = $('#demo_result').DataTable({ 

     "processing": true, //Feature control the processing indicator. 
     "serverSide": true, //Feature control DataTables' servermside processing mode. 
     //"order": [], //Initial no order. 
     "iDisplayLength" : 10, 

     // Load data for the table's content from an Ajax source 
     "ajax": { 
      "url": "<?php echo site_url('/demo/searchdemo')?>", 
      "type": "POST", 
      "dataType": "json", 
      "dataSrc": function (jsonData) { 
       return jsonData.data; 
      } 
     }, 

     //Set column definition initialisation properties. 
     "columnDefs": [ 
     { 
      "targets": [ 0 ], //first column/numbering column 
      "orderable": false, //set not orderable 
     }, 
     ], 
    }); 
}); 

控制器:Demo.php

class Demo extends CI_Controller { 
    public function __construct() { 
     parent::__construct(); 
     $this->load->model('demo/Demo_model'); 
    } 

    public function index() { 
    } 
    public function searchdemo() { 
     $data['branch_list'] = $this->Demo_model->get_company_branches(); 

     // set form validation rules 
     $this->form_validation->set_rules('branch', 'Branch', 'trim|required'); 

     // submit 
     if ($this->form_validation->run() == FALSE) { 
      // validation failed, send validation errors to the view 
      $data['branch_list'] = $this->Demo_model->get_company_branches(); 
      $this->load->view('templates/header'); 
      $this->load->view('demo/searchdemo',$data); 
      $this->load->view('templates/footer'); 
     } else {     
      // set variables from the form    
      $formdata = array(
       'branch_id' => $this->input->post('branch'), 
       'length' => intval($this->input->post('length')), 
       'start' => intval($this->input->post('start')), 
       'order' => $this->input->post('order') 
      ); 
      $users_list = $this->Demo_model->get_user_details($formdata); 
      if(!empty($users_list)){ 
       $data = array(); 
       foreach ($users_list as $user) { 
        $row = array(); 
        $row['name'] = $user->name; 
        $row['role'] = $user->role; 
        $data[] = $row; 
       } 
      } 
     } 

     $output = array(
      "draw" => intval($this->input->post('draw')), 
      "recordsTotal" => $this->Demo_model->count_all_active($formdata), 
      "recordsFiltered" => $this->Demo_model->count_filtered($formdata), 
      "data" => $data 
     ); 

     //output to json format 
     echo json_encode($output); 
    } 
} 

模型:Demo_model.php

class Demo_model extends CI_Model { 

    var $table = "users u"; 
    var $select_column = array("u.id", "u.name", "ur.role"); 
    var $column_order = array(null, "u.name","ur.role"); 
    var $order = array('u.name' => 'asc'); // default order 

    function make_query($formdata) { 
     if($formdata['branch_id']) { 
      $this->db->select($this->select_column); 
      $this->db->from($this->table);  
      $this->db->where('u.active','yes'); 
      $this->db->where('u.branch_id',$formdata['branch_id']); 
      $this->db->join('user_role ur','u.role_id=ur.id'); 
     } 
     if(isset($_POST['order'])) { // here order processing 
      $this->db->order_by($this->column_order[$formdata['order']['0']['column']], $formdata['order']['0']['dir']); 
     } 
     else if(isset($this->order)) { 
      $order = $this->order; 
      $this->db->order_by(key($order), $order[key($order)]); 
     } 
    } 

    function get_user_details($formdata){ 
     $this->make_query($formdata); 

     if(isset($formdata['length']) && $formdata['length'] < 1) { 
      $formdata['length']= '10'; 
     } else { 
      $formdata['length']= $formdata['length']; 
     } 
     if(isset($formdata['start']) && $formdata['start'] > 1) { 
      $formdata['start']= $formdata['start']; 
     } 
     $this->db->limit($formdata['length'], $formdata['start']); 
     $query = $this->db->get(); 
     return $query->result(); 
    } 
} 

當我從下拉一個分支,然後單擊搜索按鈕,它只是返回下面的JSON到Web瀏覽器,而不是顯示結果到搜索頁表。

{ 
    "draw": 0, 
    "recordsTotal": 2000, 
    "recordsFiltered": 2, 
    "data": [ 
     { 
      "name": "Raman", 
      "role": "manager" 
     }, 
     { 
      "name": "Maharaja", 
      "role": "admin" 
     } 
    ] 
} 

我提到這個鏈接https://www.phpflow.com/php/server-side-datatable-sorting-searching-pagination-using-codeigniter-ii/

我已經嘗試了數據表無效JSON響應的許多幫助URL,但沒有得到任何運氣。

回答

0

您需要使用columns.data選項爲每列定義數據源。

更改您的初始化代碼如下所示:

var table = $('#demo_result').DataTable({ 
    "processing": true, 
    "serverSide": true, 
    "ajax": { 
     "url": "<?php echo site_url('/demo/searchdemo')?>", 
     "type": "POST" 
    }, 
    "columns": [ 
     { "data": "name", "orderable": false }, 
     { "data": "role" } 
    ] 
}); 
+0

我試過,但我得到了同樣的警告「無效JSON響應」 – Shan

+0

@Shan,也'draw'絕不會'在請求0',它通常從'1'開始,一定有其他錯誤。你確定'$ this-> input-> post('draw')'得到請求參數'draw'嗎? –

相關問題