我正在尝试使用Codeigniter上的数据表服务器端处理来显示相同的数据。我应该使用'其中'条款和我正在使用Emran ul hadi脚本课程。(访问https://emranulhadi.wordpress.com/2014/06/05/join-and-extra-condition-support-at-datatables-library-ssp-class/#comment-196)。 我的控制器脚本:
$sql_details = array(
'user' => 'root',
'pass' => '',
'db' => 'kreatx',
'host' => 'localhost'
);
$index = $this->uri->segment(3);
$table = 'user';
$columns = array(
array('db' => 'Name', 'dt' => 0),
array('db' => 'Lastname', 'dt' => 1),
array('db' => 'Email', 'dt' => 2),
array('db' => 'Username', 'dt' => 3),
array('db' => 'Password', 'dt' => 4)
);
$primaryKey = 'ID';
$this->load->model('employees_model');
$department = $this->employees_model->get_department($index);
require( 'SSP.php' );
$where = 'Department = '.$index.'';
echo json_encode(
SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns,
null ,$where )
);
我的观点脚本:
$('#employees').DataTable({
"responsive":true,
"processing":true,
"serverSide":true,
"searching":true,
"ordering":true,
"order":[],
"ajax":{
url:"<?php echo base_url() .
'employees/get_employees/'.$index.''; ?>",
type:"POST"
},
"columnDefs":[
{
"targets":[4],
"orderable":false,
},
],
});
表格显示正确,没有错误。但搜索和订单确实无效。 如果我尝试搜索它只是说prrocessing并显示相同的表。 订购也有同样的问题。 有什么想法吗? 谢谢!
答案 0 :(得分:0)
在脚本上,将“服务器端”参数设置为“真”
这意味着所有过滤和排序都必须在您的php脚本中。每当您更改顺序或尝试应用过滤器时,dataTables javascript都会向您在ajax参数下列出的php页面发送ajax调用。 Ajax调用将包含您可以解析的get或post变量中所需的所有内容。
您需要用php编写代码以解析URL参数并动态创建一个SQL查询来过滤数据库结果。
我以前没有在Codeignitor中完成此操作,但这只是我在Laravel中尝试使用的简单示例(请注意,您需要对自己的数据库列进行硬编码,或者如果您是高级用户,则可以将代码循环到数据库列名称的数组)
public function MyExampleJson(Request $request)
{
$len = $_GET['length'];
$start = $_GET['start'];
$select = "SELECT *,1,2 ";
$presql = " FROM entities a ";
$whereused = false;
if($_GET['search']['value']) {
$presql .= " WHERE (id LIKE '%". $_GET['search']['value']."%' ";
$presql .= " OR column01 LIKE '%". $_GET['search']['value']."%' ";
$presql .= " OR column02 LIKE '%". $_GET['search']['value']."%' ";
$presql .= " OR column03 LIKE '%". $_GET['search']['value']."%' ";
$presql .= " OR column04 LIKE '%". $_GET['search']['value']."%' ";
$presql .= ") ";
$whereused = true;
}
$orderby = "";
$columns = array('column01','column02','column03','column04');
$order = $columns[$request->input('order.0.column')];
$dir = $request->input('order.0.dir');
$columnsearcharray = $request->columns;
foreach ($columnsearcharray as $key => $value)
{
if ($value['search']['value']) {
if ($whereused) {
$presql .= " AND ";
} else {
$presql .= " WHERE ";
$whereused = true;
}
$presql .= $columns[$key] . " LIKE '%" . $value['search']['value'] . "%' ";
}
}
$orderby = "Order By " . $order . " " . $dir;
$sql = $select.$presql.$orderby." LIMIT ".$start.",".$len;
$qcount = DB::select("SELECT COUNT(a.id) c".$presql);
$count = $qcount[0]->c;
$results = DB::select($sql);
$ret = [];
foreach ($results as $row) {
$r = [];
foreach ($row as $value) {
$r[] = $value;
}
$ret[] = $r;
}
$ret['data'] = $ret;
$ret['recordsTotal'] = $count;
$ret['iTotalDisplayRecords'] = $count;
$ret['recordsFiltered'] = count($ret);
$ret['draw'] = $_GET['draw'];
echo json_encode($ret);
}