DataTable with Ajax is not working well after use serverSide: true

前端 未结 3 1321
傲寒
傲寒 2020-12-11 06:49

I have an user table using DataTables, that will contain more than 200 rows. It\'s look so fine when I\'m using DataTables for default the \"pageLength\": 10, a

3条回答
  •  一个人的身影
    2020-12-11 07:27

    If you're using serverSide = true, you should provide your own filter count and total count. Also provide your own search function, ordering and etc. Use controller & model below for your reference.

    Controller

    $task = $this->input->post('task', TRUE);
    $user_request = $this->model->all_user_request($task);
    
    $output = array(
         'draw' => $this->input->post('draw', TRUE),
         'recordsTotal' => $user_request['recordsTotal'],
         'recordsFiltered => $user_request['recordsFiltered'],
         'data'           => empty($user_request['data'])? array() : $user_request['data']
    );
    
    echo json_encode($output);
    

    Model

    public function all_user_request($task_id) {
    
       $params = $this->input->post(null, TRUE);
    
       $search_fields = array('username','type','request'); //change this into your table fields
    
       $data = array();
       $this->db->start_cache();
       $this->db->select("username, type, request");
       $this->db->from("user_request");
       $this->db->where("task_id", $task_id);
    
        if(!empty($params['search']['value'])){
            $str = $params['search']['value'];
            $this->db->group_start();
            foreach($search_fields as $row){
                $this->db->or_like($row, $str, 'BOTH');
            }
            $this->db->group_end();
        }
    
       $data['recordsTotal'] = $this->db->count_all_results();
    
       $this->db->stop_cache();
       $this->db->limit($params['length'], $params['start']);
       $data['recordsFiltered'] = $this->db->count_all_results();
       $query = $this->db->get();
       $this->db->flush_cache();
        foreach($query->result_array() as $row){
            $data['data'][] = array_values($row);
        }
       return $data;
    }
    

提交回复
热议问题