提问人:Firefog 提问时间:3/16/2023 更新时间:3/16/2023 访问量:267
Codeigniter Datatable 服务器端库,其中条件不起作用
Codeigniter Datatable Server Side library with where condition is not working
问:
我正在为 Codeigniter 服务器端库制作一个服务器端库,我想添加一个 where 条件,例如下面的内部函数,但我如何实现 in 来查询?__construct
$this->where = (array_key_exists('where', $params) === TRUE && (is_array($params['where']) === TRUE || is_string($params['where']) === TRUE)) ? $params['where'] : [];
图书馆
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Serverside
{
private $table;
private $column_order;
private $column_search;
private $order;
private $where;
private $CI;
/**-----------------------------------------------------------------------------------------------------------------
* Constructor
* @param array $params Initialization parameters
* @return void
*/
function __construct( array $params ) {
//set codeigniter instance
$this->CI = & get_instance();
// Set table name
$this->table = (array_key_exists('table', $params) === TRUE && is_string($params['table']) === TRUE) ? $params['table'] : '';
// Set table column fields to display
$this->column_order = (array_key_exists('column_order', $params) === TRUE && is_array($params['column_order']) === TRUE) ? $params['column_order'] : [];
// Set searchable column fields
$this->column_search = (array_key_exists('column_search', $params) === TRUE && (is_array($params['column_search']) === TRUE || is_string($params['column_search']) === TRUE)) ? $params['column_search'] : [];
// Set default order
$this->order = (array_key_exists('order', $params) === TRUE && is_array($params['order']) === TRUE) ? $params['order'] : [];
}
/**
* Fetch members data from the database
* @param $postData
* based on the posted parameters
* @return
*/
public function getRows($postData){
$this->_get_datatables_query($postData);
if(!empty($postData) && $postData['length'] != -1){
$this->CI->db->limit($postData['length'], $postData['start']);
}
$query = $this->CI->db->get();
return $query->result();
}
/**
* Count all records
*/
public function countAll(){
$this->CI->db->from($this->table);
return $this->CI->db->count_all_results();
}
/**
* Count records based on the filter params
* @param $postData
* filter data based on the posted parameters
* @return mixed
*/
public function countFiltered($postData){
$this->_get_datatables_query($postData);
$query = $this->CI->db->get();
return $query->num_rows();
}
/**
* Perform the SQL queries needed for server-side processing requested
* @param $postData
* data|null based on the posted parameters
*/
private function _get_datatables_query( $postData=null){
$this->CI->db->from($this->table);
$i = 0;
// loop searchable columns
foreach($this->column_search as $item){
// if datatable send POST for search
if(!empty($postData) && $postData['search']['value']){
// first loop
if($i===0){
// open bracket
$this->CI->db->group_start();
$this->CI->db->like($item, $postData['search']['value']);
}else{
$this->CI->db->or_like($item, $postData['search']['value']);
}
// last loop
if(count($this->column_search) - 1 == $i){
// close bracket
$this->CI->db->group_end();
}
}
$i++;
}
// //var_dump($this->where);
// if($this->where){
// foreach ($this->where as $key => $value){
// $this->CI->db->group_start();
//
// $this->CI->db->where($key ,$value);
//
// $this->CI->db->group_end();
//
// $i++;
// }
// }
if(isset($postData['order']) && $postData['order'] !=''){
$this->CI->db->order_by($this->column_order[$postData['order']['0']['column']], $postData['order']['0']['dir']);
}else if(isset($this->order)){
$order = $this->order;
$this->CI->db->order_by(key($order), $order[key($order)]);
}
}
}
控制器方法示例:
public function customer_json()
{
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$data = array();
//Load Library
$this->load->library('serverside', array(
'table' => 'ci_customer', //Db Table name
'column_order' => array(null, 'id', 'firstname', 'age'),
'column_search' => array('id', 'firstname', 'age'),
'order' => array('id' => 'desc')
//want to add where condition here
));
// Fetch records server side
$dbData = $this->serverside->getRows($_POST);
$i = $_POST['start'] ?? '0';
foreach ($dbData as $users) {
$i++;
$data[] = array(
$i,
$users->firstname,
$users->age,
'<a title="Edit" href="' . base_url('admin/user/customer?id=' . $users->id) . '" class="update btn btn-sm btn-warning-light" > <i class="fa fa-pencil-square-o"></i></a>'
);
}
$output = array(
"draw" => $_POST['draw'] ?? '',
"recordsTotal" => $this->serverside->countAll(),
"recordsFiltered" => $this->serverside->countFiltered($_POST),
"data" => $data,
);
// Output to JSON format
echo json_encode($output);
}
HTML 标记 UP
<table id="DataTable" class="table table-bordered table-striped" width="100%">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Age</th>
<th>Action</th>
</tr>
</thead>
</table>
<script type="text/javascript">
let table = $('#DataTable').DataTable({
"processing": true,
"serverSide": true,
"aLengthMenu": [[15, 25, 50, 100 , -1], [ 15, 25, 50, 100, "All"]],
"iDisplayLength" : 10,
"bStateSave": true,
"order": [],
"ajax": {
"url": "<?php echo base_url('admin/users/customer_json'); ?>",
"type": "POST"
},
});
</script>
答: 暂无答案
上一个:减少服务器端的初始响应时间
评论