Итак, я только начал изучать Ajax и хочу применить его с DataTable на стороне сервера. И теперь, у меня есть эта проблема, функция поиска возвращает ошибку с предупреждением DataTables: таблица id = mitsuha - Ajax ошибка. Для получения дополнительной информации об этой ошибке см. http://datatables.net/tn/7
Контроллер
function get_ajax()
{
$list = $this->code->get_datatables();
$data = array();
$no = @$_POST['start'];
foreach ($list as $item) {
$no++;
$row = array();
$row[] = $no.".";
$row[] = $item->code_name;
$row[] = $item->language_name;
$row[] = $item->difficulty_name;
$row[] = $item->nama_user;
$row[] = '<a href="'.base_url('code/view/'.$item->code_id).'" class="btn btn-info btn-xs"><i class="fa fa-eye"></i></a>
<a href="'.base_url('code/update/'.$item->code_id).'" class="btn btn-primary btn-xs"><i class="fa fa-edit"></i></a>';
$data[] = $row;
}
$output = array(
"draw" => @$_POST['draw'],
"recordsTotal" => $this->code->count_all(),
"recordsFiltered" => $this->code->count_filtered(),
"data" => $data,
);
echo json_encode($output);
}
Модель
var $column_order = array(null, 'code.code_name', 'language.language_name', 'user.nama_user, difficulty.difficulty_name');
var $column_search = array('code.code_name', 'language.language_name', 'user.nama_user, difficulty.difficulty_name');
var $order = array('code_id' => 'desc');
private function _get_datatables_query() {
$this->db->select('code.*, language.language_name, user.nama_user, difficulty.difficulty_name');
$this->db->from('code');
$this->db->join('language', 'code.language = language.language_id');
$this->db->join('user', 'code.contributor = user.user_id');
$this->db->join('difficulty', 'code.difficulty = difficulty.difficulty_id', 'left');
$i = 0;
foreach ($this->column_search as $item) { // loop column
if($_POST['search']['value']) { // if datatable send POST for search
if($i===0) { // first loop
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item, $_POST['search']['value']);
} else {
$this->db->or_like($item, $_POST['search']['value']);
}
if(count($this->column_search) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$i++;
}
if(isset($_POST['order'])) { // here order processing
$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('code');
return $this->db->count_all_results();
}
и ajax
<script>
$(function(){
$('#mitsuha').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "<?php echo base_url('code/get_ajax') ?>",
"type": "POST",
},
"columnDefs": [
{
// orderable untuk mengaktifkan atau non fungsi order
"targets": [0, 5],
"orderable": false
}
],
"order": []
});
})
Заранее спасибо!