DUDEZKIE
DUDEZKIE

Reputation: 415

CodeIgniter Datatables Sorting, Searching not working

please help me with this, I cannot figure out how to set up my codeigniter server side and datatable server side processing. Seems the data has been pushed through the tables but when I start searching and sorting columns, it is not working.

Please see my code

CI Server Side Controller

function admin_pages_datatable()
{
    $start = $this->input->post("start");
    $length = $this->input->post("length");
    $draw = $this->input->post("draw");
    $query = $this->db->query("SELECT * FROM tbl_fe_pages LIMIT {$start}, {$length}");
    $query_count = $this->db->query("SELECT * FROM tbl_fe_pages");
    $query_count = $query_count->num_rows();
    $query = $query->result();
    $datatable = array();
    $s = 1;
    foreach($query as $row){
        $datatable["data"][] = array(
            $s++,$row->subject,$row->descriptions,$row->type,$row->status,'F'
        );
    }

    $datatable["draw"] = $draw;
    $datatable["recordsTotal"] = 10;
    $datatable["recordsFiltered"] = $query_count;
    return json_encode($datatable);
}

Javascript

var table_items_all = $('#data').dataTable({
            "aaSorting": [[ 2, "desc" ]],
            "scrollY"           : "300px",
            "scrollCollapse"    : true,
            "processing"        : true,
            "serverSide"        : true,
            "ajax": {
                "url" : "http://localhost/myproject/maintenance/admin_pages_datatable",
                "type" : "POST"
            },
            "language": {
                "emptyTable":     "My Custom Message On Empty Table"
            },
            "aoColumns": 
            [
                null, null, null, null, null, {sClass: 'controls'}
            ],

        }); 

Thank you very much for the help :D

Upvotes: 1

Views: 3312

Answers (2)

Gyrocode.com
Gyrocode.com

Reputation: 58880

You have server-side processing enabled with "serverSide":true. In server-side processing mode filtering, paging and sorting calculations are all performed by a server.

See full list of parameters sent by the client in server-side processing mode. Among others, there are:

search[value] Global search value

order[i][column] Column to which ordering should be applied. This is an index reference to the columns array of information that is also submitted to the server.

order[i][dir] Ordering direction for this column. It will be asc or desc to indicate ascending ordering or descending ordering, respectively.

In the parameters above i is an integer which will change to indicate the array value. In most modern server-side scripting environments this data will automatically be available to you as an array.

So I assume you should add the following variables that will be arrays:

$search = $this->input->post("search");
$columns = $this->input->post("columns");
$order = $this->input->post("order");

Then you would need to modify your SQL query according to the data in these arrays.

Alternatively instead of reinventing the wheel, you may look for the library that already does server side processing logic for you. For example, there is github.com/zepernick/Codeigniter-DataTables - CodeIgniter Library For Server Side DataTables 1.10.

Upvotes: 1

acupofjose
acupofjose

Reputation: 2159

I believe the issue is because you are doing return json_encode() instead of echo json_encode(), note that when you return something in php, that data is only received (usable that is) from other php functions, you need to actually output the data on the page in this case for javascript to pick up.

Also, just for MVC's sake, you should be doing all of your database queries in the model, not the controller.

Should be model functions:

$query = $this->db->query("SELECT * FROM tbl_fe_pages LIMIT {$start}, {$length}");
$query_count = $this->db->query("SELECT * FROM tbl_fe_pages");

Upvotes: 0

Related Questions