Sujan Shrestha
Sujan Shrestha

Reputation: 1040

server side pagination in jquery datatable

I am having displaying datas. Because it contains more than 1000 rows of data. I want to have a server side pagination instead of what jquery datatable offers. As i have noticed, datatables load all the 1000 queries and does client side pagination only. This is my controller

 function view_ftth_report()
{

    if ($this->data['permission_ftth_report'] == '1') {
    $this->load->model('staff_activity_model');
    $this->data['records'] = $this->staff_activity_model->view_ftth_report();

    $this->data['main_template'] = 'staff_activity/staff_activity_main';
    $this->data['inner_template'] = 'staff_activity/ftth/staff_activity_ftth_report_view';
    $this->data['inner_nav'] = 'staff_activity/staff_activity_nav';
    $this->load->view('common/common', $this->data);
        }
    else{
        $this->data['main_template'] = 'staff_activity/staff_activity_main';
        $this->data['inner_template'] = 'staff_activity/staff_activity_denied';
        $this->data['inner_nav'] = 'staff_activity/staff_activity_nav_denied';
        $this->load->view('common/common', $this->data);
    }
}

This is my model.

function view_ftth_report(){
    $sql = "SELECT * FROM ct_staff_activity_ftth ORDER BY date DESC limit 20  ";
    $data = array();
    $result = $this->db->query($sql);
    foreach($result->result_array() as $row)
    {
        $data[] = $row;
    }
    return $data;
}

And this is my view. I have used ajax to call the records .

<script type="text/javascript">

$(function() {
    $("#data_tbl").dataTable({
        "iDisplayLength": 50
    });

});
</script>
<div class="row">
<div class="col-md-12">
    <div class="box ">
        <div class="box-body">
            <div class="row">
                <div class="col-md-12">
                        <a href="<?php echo base_url();?>staff_activity/date_report_index"><button style="margin: 15px; padding: 10px; border-radius: 10px; border-collapse: collapse " class="btn btn-success btn-sm">Filter By Date</button></a>

                    <div class="col-md-12 table-responsive">
                        <table class="table table-striped table-responsive" id="data_tbl">
                            <h4>Recent Entries</h4>
                            <thead>
                            <tr>
                                <th>Sn.</th>
                                <th>Client's Name</th>
                                <th>Address</th>
                                <th>Fiber Length</th>
                                <th>Phone Number</th>
                                <th>Package</th>
                                <th>Result</th>
                                <th>Date</th>
                                <th>Team Name</th>
                                <th>Remarks</th>
                                <th>Edit</th>
                                <th>Delete</th>

                            </tr>
                            </thead>
                            <tbody id="result">
 <?php

                            $count = 0;

                            foreach($records as $rec){
                                $count = $count+1;

                                ?>
                                <tr>
                                    <td><?php echo $count; ?></td>
                                    <td><?php echo $rec['client_name']; ?></td>
                                    <td><?php echo $rec['address']; ?></td>
                                    <td><?php echo $rec['fiber_length']; ?></td>
                                    <td><?php echo $rec['phone_number']; ?></td>
                                    <td><?php echo $rec['package']; ?></td>
                                    <td><?php echo $rec['result']; ?></td>
                                    <td><?php echo $rec['date']; ?></td>
                                    <td><?php echo $rec['team_name']; ?></td>
                                    <td><?php echo $rec['remarks']; ?></td>
                                    <td><a href="<?php echo base_url(); ?>staff_activity/edit_ftth_report/<?php echo $rec['id']?>">Edit</a></td>
                                    <td><a href="javascript:void(0)" onclick="chk('<?php echo base_url(); ?>staff_activity/delete_ftth_report/<?php echo $rec['id']  ?>')">Delete</a></td>

                                </tr>

                                <?php

                            }

                            ?>
                            </tbody>

                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Upvotes: 0

Views: 1445

Answers (1)

Kushal Suthar
Kushal Suthar

Reputation: 423

I also Integrate DataTable recently in my project. Actually in codeigniter there is some library available for DataTable. Please find the below some useful libraries

  1. Ignited-Datatables
  2. Codeigniter-DataTables

So you just follow the instraction given in readme. I am providing example for Ignited-Datatables which i integrate in my project .

CODEIGNITER

  1. First move DataTable.php in your application\library folder.
  2. Load in your controller's constructor

    $this->load->library('Datatables');

  3. $this->datatables->select( pass the column which you want to display in table and manage order also respect to your table).

  4. $this->datatables->from('ct_staff_activity_ftth'); Pass the Table name here.

JAVASCRIPT

  1. Include dataTable.min.js in your view file.
  2. Put the code in document ready stage.

    $('#data_tbl').dataTable( { processing: true, serverSide: true, ajax: { "url": "/index.php/DataTableExample/dataTable", "type": "POST" }, columns: [ { data: "s.s_name" }, {data : "c.c_name"}, {data : "c.c_zip"}, { data: "$.city_state_zip" } //refers to the expression in the "More Advanced DatatableModel Implementation" ], aaSorting: [ [0, 'desc'] // provide the default sorting column with order numer. ] });

Hope it will help you. if you need any further assistance let me know.

Upvotes: 1

Related Questions