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

前端 未结 3 1324
傲寒
傲寒 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:41

    Datatables send everything you need - if you take a look in your console under network you'll see, that they use the ajax-get method to send those requests to the server

    The GET Parameter are as follows

    draw
    columns
    start
    length
    search
    

    You can find the entire list here

    which means - you've to adapt your model properly...

    something like that should work

    public function all_user_request($task_id) 
    {
        $intStart = intval($this->input->get("start"));
        $intLength = intval($this->input->get("length"));
        $strSearch = (strlen($this->input->get("search")["value"]) >= 2) ?   $this->input->get("search",true)["value"]    :   false;
        $order = $this->input->get("order",true);
    
    
        $this->setQuery($task_id,$strSearch);
    
        $query = $this->db->get();
        $this->recordsTotal = $query->num_rows();
    
        $this->setQuery($task_id, $strSearch);
    
        if ($intStart >= 0 && $intLength > 0)
        {
            $this->db->limit($intLength,$intStart);
        }
    
        $strOrderField = 'username';
        $strDirection = "ASC";
        if (is_array($order))
        {
            switch($order[0]['column'])
            {
                case 1:
                    $strOrderField = 'type';
                    break;
                case 2:
                    $strOrderField = 'request';
                    break;
    
            }
            if (!empty($order[0]['dir']))    $strDirection = $order[0]['dir'];
        }
        $this->db->order_by($strOrderField,$strDirection);
    
    
        $query = $this->db->get();
    
        $arrData = $query->result();
    
        return $arrData;
    
    }
    
    public function getRecordsTotal()
    {
        return $this->recordsTotal;
    }
    
    private function setQuery($task_id, $strSearch="")
    {
        $this->db
            ->select('*')
            ->from('user_request')
            ->where('task_id', $task_id);
    
        if (!empty($strSearch))
        {
            $this->db->like('task_id', $strSearch);
        }
    
    }
    

    and your controller

    //controller
    $task = $this->input->post('task', TRUE);
    $user_request = $this->model->all_user_request($task);
    
    $data = [];
    foreach ($user_request as $ur)
    {
        $data[] = [
            $ur->username,
            $ur->type,
            $ur->request
        ];
    }
    
    $arrCompiledData = [
        'data' => $data,
        'draw' => $this->input->get('draw'),
        'recordsTotal' => $this->model->getRecordsTotal(),
        'recordsFiltered' => $this->model->getRecordsTotal(),
    ];
    
    $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($arrCompiledData));
    

    Please keep in mind i just wrote this down - maybe there are some typos, but you should be able to understand how the serverside processing of a datatables request should work.

提交回复
热议问题