DataTables Server Side Processing with CodeIgniter

20 May


In this tutorial, we will see how to use popular dataTables with CodeIgniter framework. Note that, pagination is done in client side for this tutorial. Hence this example is not ideal for a big data set. Handling pagination in server side will be discussed in  a future tutorial.

Table format

                             <th>Need identified thru</th>
                                        <th>Total Estimate</th> 
                                        <th class="no-sort"></th>
                                        <th class="no-sort"></th>



Initializing and fetching data with ajax

    $(document).ready(function() {
            "ajax": "<?php echo base_url('test/get_json'); ?>",
            "pageLength": <?php echo $this->config->item('results_per_page'); ?>,
            "order": [[ 0, "desc" ]],
            "aoColumnDefs": [
                { "bVisible": false, "aTargets": [0] },
                    "bSortable": false,
                    "aTargets": ["no-sort"]  
            "dom": 'T<"clear">lfrtip',
            tableTools: {
                "sSwfPath": "<?php echo base_url("plugins/data_tables/extensions/TableTools/swf/copy_csv_xls_pdf.swf"); ?>"

Test Controller

class Test extends CI_Controller {
public function get_json() {
        $results = $this->test_model->load_grid();
        $data = array();
        foreach ($results  as $r) {
            array_push($data, array(
                anchor('test/view/' . $r['id'], 'View'),
                anchor('test/edit/' . $r['id'], 'Edit')

        echo json_encode(array('data' => $data));


class Test_model extends CI_Model {
public function load_grid() {
        $this->db->select("$this->tbl_urgent_needs.unid,$this->tbl_urgent_needs.year,$this->tbl_urgent_needs.needi,$this->tbl_urgent_needs.total_cost,$this-> AS mname,$this-> AS rname");
        $this->db->join("$this->tbl_roads", "$this->tbl_roads.rid = $this->tbl_urgent_needs.rd_id");
        $this->db->join("$this->tbl_maintenance_type", "$this-> = $this->tbl_urgent_needs.maint_type_id", "left");
        $this->db->order_by("$this->tbl_urgent_needs.unid", 'ASC');

        $query = $this->db->get();
        return $query->result_array();


Posted by on May 20, 2015 in Codeigniter, PHP



2 responses to “DataTables Server Side Processing with CodeIgniter

  1. ical

    June 9, 2015 at 11:05 am

    Hi sir, please continue this tutorial with pagination in server side. Thank You

  2. akera

    March 27, 2016 at 3:31 am

    thank you so much, you have no idea how much you’ve helped me.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: