我正在使用Codeigniter和服务器端数据表。我在分页时遇到问题。我有2万条记录,每页显示10条记录。
但是问题是,正在显示分页,但是我也在页面上获得了所有2万条记录。
查看此分页截图
第二个问题是,我在数据库中的记录少于10个。它显示在我的视图上,但要花很多时间才能显示。有什么方法可以显示快速记录?
控制器
public function displayrecords(){
$order_list=$this->System_model->uploadedList();
// Datatables Variables
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$data['draw'] = 1;
$data['recordsTotal'] = count($order_list);
$data['recordsFiltered'] = count($order_list);
$data['data'] = [];
foreach ($order_list as $key => $row)
{
$arr_result = array(
"id" =>"<input name='select_all[]' type='checkbox' class='lead_ids customCheckbox' id='".$row->u_id."'>",
"companyname" => $row->u_companyname,
"customername" => $row->u_firstname.' '.$row->u_lastname,
"mobileno" => $row->u_mobileno,
"action" => "<a href=''>click</a>"
);
$data['data'][] = $arr_result;
}
//print_r($arr_result);
echo json_encode($data);
exit;
}
模型
public function uploadedList(){
$this->db->select('*');
$this->db->from('tbl_Upload');
$this->db->where('is_active', 0);
$query = $this->db->get();
return $query->result();
}
JS
$('#uploadedLead-list').DataTable( {
language: {
sLengthMenu: "Show _MENU_",// remove entries text
searchPlaceholder: "Search",
emptyTable: "No record found",
search:""
},
"ordering": false,// remove sorting effect from header
"processing": true,
"serverSide": true,
"scrollX": true,
"bInfo" : false,
"pageLength": 10,
"ajax": {
"url" : baseUrl + "/System_control/displayrecords",
"type" : "POST"
// "dataSrc": ""
},
"columns": [
{ "data": "id","className": "reg_bg" },
{ "data": "companyname" },
{ "data": "customername" },
{ "data": "mobileno" },
{ "data": "action" }
]
});
您能帮我解决这个问题吗?
答案 0 :(得分:0)
您的方法应如下所示
CONTROLLER
public function displayrecords() {
// Datatables Variables
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
// Get total records
$total_records = $this->System_model->getTotalCount();
// These params should be used with limit query [LIMIT 0, 10 for first page, LIMIT 10, 10 for second page and so on]
$order_list = $this->System_model->uploadedList($start, $length);
$data['draw'] = $draw;
$data['recordsTotal'] = $total_records;
$data['recordsFiltered'] = $total_records;
// Prepare data to output
$data['data'] = [];
foreach ($order_list as $key => $row) {
$arr_result = array(
"id" =>"<input name='select_all[]' type='checkbox' class='lead_ids customCheckbox' id='".$row->u_id."'>",
"companyname" => $row->u_companyname,
"customername" => $row->u_firstname.' '.$row->u_lastname,
"mobileno" => $row->u_mobileno,
"action" => "<a href=''>click</a>"
);
$data['data'][] = $arr_result;
}
// echo encoded json
echo json_encode($data);
}
模型
public function uploadedList($length, $start) {
$this->db->select('*');
$this->db->from('tbl_Upload');
$this->db->where('is_active', 0);
$this->db->limit($length, $start);
$query = $this->db->get();
return $query->result();
}
public function getTotalCount() {
return $this->db->count_all('tbl_Upload');
}