CodeIgniter中如何让DataTable每次加载10条记录而非全量加载?
解决方案:CodeIgniter + DataTables 实现服务器端分页
当然可以实现!这就是DataTables的服务器端分页功能,专门解决大数据量一次性加载慢的问题。核心思路是:前端只负责发送分页、排序、搜索的请求参数,后端CodeIgniter根据这些参数从数据库查询对应页的10条数据,返回给前端渲染,这样每次请求只加载10条,速度会快很多。
第一步:修改前端视图的DataTables初始化代码
你需要开启DataTables的serverSide模式,并指定后端接口地址,同时配置列字段对应关系:
$(document).ready(function() { $('#sample_2').DataTable({ "processing": true, // 显示加载中的提示动画 "serverSide": true, // 开启服务器端处理模式(关键!) "pageLength": 10, // 每页默认显示10条 "ajax": { "url": "<?php echo base_url('your_controller/get_paginated_products'); ?>", // 替换成你的控制器接口地址 "type": "POST", // 如果你的CodeIgniter开启了CSRF防护,需要带上token "headers": { "X-CSRF-Token": "<?php echo csrf_hash(); ?>" } }, "columns": [ {"data": "id", "visible": false}, // 对应你隐藏的No.列,字段名要和后端返回的一致 {"data": "product_name"}, // 对应Product列,替换成你实际的字段名 // 其他列依次添加,比如{"data": "price"}、{"data": "create_time"}等,要和后端返回的字段一一对应 ] }); });
第二步:在CodeIgniter控制器中编写处理接口
创建一个方法来接收前端的分页、排序、搜索参数,调用模型查询数据并返回符合DataTables格式的JSON:
public function get_paginated_products() { // 获取DataTables传递的请求参数 $draw = $this->input->post('draw'); // 前端请求的标识,原样返回即可 $start = $this->input->post('start'); // 当前页的起始位置(比如第2页就是10) $length = $this->input->post('length'); // 每页显示的条数 $search_value = $this->input->post('search')['value']; // 搜索框的内容 $order_col_index = $this->input->post('order')[0]['column']; // 排序的列索引 $order_dir = $this->input->post('order')[0]['dir']; // 排序方向(asc/desc) // 定义列索引对应的数据库字段名,要和前端columns的顺序一致 $column_map = [ 0 => 'id', 1 => 'product_name', // 其他列的字段名依次对应 ]; $order_field = $column_map[$order_col_index]; // 调用模型获取数据 $paginated_data = $this->product_model->get_paginated_data($start, $length, $search_value, $order_field, $order_dir); $total_records = $this->product_model->get_total_records(); $filtered_records = $this->product_model->get_filtered_records($search_value); // 构造DataTables需要的返回格式 $output = [ "draw" => intval($draw), "recordsTotal" => intval($total_records), // 总记录数 "recordsFiltered" => intval($filtered_records), // 过滤后的记录数(搜索时用) "data" => $paginated_data // 当前页的数据集 ]; // 输出JSON给前端 echo json_encode($output); }
第三步:在模型中编写数据库查询逻辑
创建三个方法分别处理总记录数、过滤后记录数、分页数据集的查询:
class Product_model extends CI_Model { // 获取总记录数 public function get_total_records() { return $this->db->count_all('your_product_table'); // 替换成你的产品表名 } // 获取搜索过滤后的记录数 public function get_filtered_records($search_value) { if (!empty($search_value)) { // 可以添加多个字段模糊搜索,比如or_like其他字段 $this->db->like('product_name', $search_value); // $this->db->or_like('product_code', $search_value); } return $this->db->count_all_results('your_product_table'); } // 获取分页+排序+过滤后的数据集 public function get_paginated_data($start, $length, $search_value, $order_field, $order_dir) { $this->db->select('id, product_name'); // 选择你需要返回的字段,要和前端columns对应 if (!empty($search_value)) { $this->db->like('product_name', $search_value); // 同上,可添加其他搜索字段 } $this->db->order_by($order_field, $order_dir); // 排序 $this->db->limit($length, $start); // 分页:条数,起始位置 $query = $this->db->get('your_product_table'); return $query->result_array(); // 返回数组格式,方便前端处理 } }
几个注意事项
- 字段对应要一致:前端
columns里的data值,必须和模型返回的数组键名完全匹配,否则前端无法渲染数据。 - CSRF防护:如果你的CodeIgniter开启了CSRF,一定要在ajax请求中带上CSRF token,否则会被拦截。
- 数据库索引优化:如果产品表数据量很大,建议给经常搜索、排序的字段(比如
product_name)添加数据库索引,提升查询速度。 - 测试调试:可以用浏览器的开发者工具查看ajax请求的参数和返回的JSON数据,排查问题。
内容的提问来源于stack exchange,提问作者user7962849
相关产品推荐
相关产品推荐

