我正在尝试使用dataTable通过CodeIgniter管理我的数据。
dataTable加载良好,但是当我使用搜索时,它花费很长时间并加载所有数据并发送一个对象(如下图所示)以起作用。
这是我的JavaScript代码:
<script type="text/javascript">
jQuery(document).ready(function () {
var table=jQuery('#table-3').dataTable({
"sPaginationType": "bootstrap",
"aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
"bStateSave": true,
"processing": true,
"serverSide": true,
"ajax":{
"url": "<?php echo base_url('index/jsonForDbTableMain') ?>",
"dataType": "json",
"type": "POST",
"data":{ '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>' }
},
"columns": [
{ "data": "id" },
{ "data": "name" },
{ "data": "clId" },
{ "data": "itNum" },
{ "data": "eType" },
{ "data": "fs" },
],
"columnDefs": [
{
"targets": [ 0 ], //first column / numbering column
"orderable": false, //set not orderable
},],
});
table.columnFilter({
"sPlaceHolder" : "head:after"
});
});
</script>
CONTROLLER
public function jsonForDbTableMain(){
$columns = array(
0 =>'id',
1 =>'name',
2=> 'clId',
3=> 'itNum',
4=> 'eType',
5=> 'fs',
);
$limit = $this->input->post('length');
$start = $this->input->post('start');
$order = $columns[$this->input->post('order')[0]['column']];
$dir = $this->input->post('order')[0]['dir'];
$totalData = $this->autodb->productCount();
$totalFiltered = $totalData;
if(empty($this->input->post('search')['value']))
{
$proSearch = $this->autodb->allproduct($limit,$start,$order,$dir);
}
else {
$search = $this->input->post('search')['value'];
$proSearch = $this->autodb->product_search($limit,$start,$search,$order,$dir);
$totalFiltered = $this->autodb->product_search_count($search);
}
$data = array();
if(!empty($proSearch))
{
foreach ($proSearch as $pro)
{
$nestedData['id'] = $pro->id;
$nestedData['name'] = $pro->name;
$nestedData['clId'] = $pro->clId;
$nestedData['itNum'] = $pro->itNum;
$nestedData['eType'] = $pro->eType;
$nestedData['fs'] = $pro->fs;
$data[] = $nestedData;
}
}
$json_data = array(
"draw" => intval($this->input->post('draw')+1),
"recordsTotal" => intval($totalData),
"recordsFiltered" => intval($totalFiltered),
"data" => $data
);
echo json_encode($json_data);
exit();
}
我的问题是:如何通过CodeIgniter在dataTable中进行搜索?