我在标签搜索时遇到问题,当我输入一些字符时会显示错误信息
我的控制器:
public function ajaxlist()
{
$list = $this->eee_model->get_datatables();
$data = array();
$no=0;
foreach ($list as $q) {
$no++;
$row = array();
$row[] = $no;
$row[] = $q->nofile;
$row[] = $q->est_no ."/".$q->file_no;
$row[] = $q->number;
$row[] = $q->nobilut;
$row[] = $q->nounit;
$row[] = $q->zone;
$row[] = $q->subz;
$row[] = '<a class="btn btn-sm btn-primary" href="'.site_url($q->file).'" "><i class="fa fa-file-pdf-o"></i></a>';
$row[] = '<a class="btn btn-sm btn-primary" href="'.site_url("estebyan/edit/".$q->id).'" title="Edit" target="_blank" "><i class="fa fa-edit"></i> edit</a>';
$row[] = '<a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_person('."'".$q->id."'".')"><i class="fa fa-trash"></i> delete</a>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->estebyan_model->count_all(),
"recordsFiltered" => $this->estebyan_model->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
我的模特:
var $table = 'Edata';
var $column_order = array(null, 'number','nofile','zone','subz','nobilut'); //set column field database for datatable orderable
var $column_search = array('number','nofile','zone','subz','nobilut'); //set column field database for datatable searchable
var $order = array('id' => 'asc'); // default order
public function __construct()
{
parent::__construct();
$this->load->database();
}
private function _get_datatables_query()
{
$this->db->from($this->table);
$i = 0;
foreach ($this->column_search as $item) // loop column
{
if(isset($_POST['search']['value']) ? $_POST['search']['value'] : null)
// 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();
}
public function count_all()
{
$this->db->from($this->table);
return $this->db->count_all_results();
}
我的观点:
<table id="table" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>#</th>
<th>nofile</th>
<th>fileno</th>
<th>number</th>
<th>nobulit</th>
<th>unit</th>
<th>Zone</th>
<th>Sub</th>
<th>attach</th>
<th >edit</th>
<th >delete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
我的剧本:
table = $('#table').DataTable({
"processing": true, //Feature control the processing indicator.
"serverSide": true, //Feature control DataTables' server-side processing mode.
"order": [], //Initial no order.
// Load data for the table's content from an Ajax source
"ajax": {
"url": "<?php echo site_url('estebyan/ajaxlist')?>",
"type": "POST"
},
//Set column definition initialisation properties.
"columnDefs": [
{
"targets": [ 0 ], //first column / numbering column
"orderable": false, //set not orderable
},
],
});
显示所有视图数据,但搜索无效!
答案 0 :(得分:1)
$.ajax({
type: "post",
url: "your url",
dataType: "json",
success: function(data){ // return json encode data
if(data.length > 0){
tabledata = [];
for (var i = 0; i < data.length; i++) {
tabledata.push([ // push data in array as per your column
data[i].name,
data[i].other_value,
data.[i].other_value]);
}
if (table != null) table.fnDestroy();
table = $('#tablename').dataTable({
"data": tabledata, // add your data array it will create datatable with search and pagination and other things
});
}
},
error: function(){
}
});
如果您没有datatable.js和datatable.css,请先包含并尝试此代码
答案 1 :(得分:0)
你的datatables_query中的订单数组中没有任何内容,请在订单处理之前尝试这样写。
$column_order = $this->column_order;