因此,我正在CodeIgniter框架中创建一个页面,其中使用dataTables中的ajax请求显示数据库中的数据。
首先,这是我的数据库配置:
'dsn' => '',
'hostname' => 'mysql:host=localhost; dbname=elearning; charset=utf8;',
'username' => 'root',
'password' => '',
'database' => 'elearning',
'dbdriver' => 'pdo',
这是我的模特:
public function __construct(){
parent::__construct();
$this->load->database();
}
function all(){
return $this->db->get($this->table);
}
function _get_datatables_query(){
$this->db->from($this->table);
$i = 0;
foreach ($this->column_search as $item){
if($_POST['search']['value']){
if($i===0) {
$this->db->group_start();
$this->db->like($item, $_POST['search']['value']);
}
else{
$this->db->or_like($item, $_POST['search']['value']);
}
if(count($this->column_search) - 1 == $i)
$this->db->group_end();
}
$i++;
}
if(isset($_POST['order'])){
$this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
else if(isset($this->order)){
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
function get_datatables(){
$this->_get_datatables_query();
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
function count_filtered(){
$this->_get_datatables_query();
$query = $this->db->get();
return $query->num_rows();
}
function count_all(){
$this->db->from($this->table);
return $this->db->count_all_results();
}
在我的控制器中,是用于获取数据drom数据库,将其解析为JSON并放入DataTables中的代码:
class Unit extends CI_Controller {
var $order = array('id_unit' => 'desc');
var $table = 'unit';
var $idq = 'id_unit';
var $column_order = array('nama_unit',null);
var $column_search = array('nama_unit');
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('myModel');
}
function ajax_list () {
$list = $this->myModel->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $li) {
$no++;
$row = array();
$row[] = $li->unit_name;
if($li->photo)
$row[] = '<a href="'.base_url('web_file/admin/logo_unit/'.$li->photo).'" target="_blank"><img src="'.base_url('web_file/admin/logo_unit/'.$li->photo).'" class="img-responsive" /></a>';
else
$row[] = '(No photo)';
$row[] = $li->lesson_count;
$row[] = '
<a class="btn btn-sm btn-primary" href="javascript:void(0)"
title="Edit" onclick="edit_unit('."'".$li->id_unit."'".')">
<i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm btn-danger" href="javascript:void(0)"
title="Hapus" onclick="delete_unit('."'".$li->id_unit."'".')">
<i class="glyphicon glyphicon-trash"></i> Delete</a>';
$data[] = $row;
}
$output = array("draw" => $_POST['draw'],
"recordsTotal" => $this->myModel->count_all(),
"recordsFiltered" => $this->myModel->count_filtered(),
"data" => $data,
);
echo json_encode($output);
}
}
这是我的ajax,用于请求显示数据:
var table, save_method;
var base_url = '<?php echo base_url();?>';
$(document).ready(function(){
table = $('#table').DataTable({
"processing": true,
"serverSide": true,
"order": [],
"ajax": {
"url": "<?php echo site_url('back/unit/ajax_list')?>",
"type": "POST"
},
"columnDefs": [{
"targets": [ -1 ],
"orderable": false,
},
],
});
});
这是探针,当我尝试调用函数Ajax_list时,总是收到错误通知:
Undefined $_POST[search]
Undefined $_POST[start]
Undefined $_POST[value]
Undefined $_POST[order]
但是函数ajax_list确实返回了JSON 这是一个经过验证的JSON,因为我已经在JSONLint中对其进行了检查。
下一个问题是表格始终以:
响应“ DataTables警告:table id = table-无效的JSON响应。有关此错误的更多信息,请参见http://datatables.net/tn/1”。
如何解决此错误。非常感谢您的回应。我想问的是帕顿,这是我第一次在这里问问题。谢谢你和最诚挚的问候。