Welcome Guest, Not a member yet? Register   Sign In
Datatables for Codeigniter 3
#1

Hi
Does anyone have datatables library or anything to use with CI3, and to support joins because of search in database table.
Main problem with this i have is that i need to make model for each database table and new controller. For example i have Vehicles controller and model, and under vehicles model i want to have vehicles insurance, vehicles services tables but i don't want to copy/paste same code for datatables in same file.
Currently i am using it like this
Model:
PHP Code:
    var $table 'vehicle';
    var $select_column = array('Id''VehicleName''DateOfInsurance''Insurer''PolicyNumber''Amount''Bill''NextInsuranceDate''Remark');
    var $order_column = array('Id''VehicleName''DateOfInsurance''Insurer''PolicyNumber''Amount''Bill''NextInsuranceDate''Remark');

    public function make_query()
    {
        $this->db->select($this->select_column);
        $this->db->from($this->table);
        // sent request for search
        if (isset($_POST['search']['value'])) {
            $this->db->group_start();
            $this->db->like('Id'$_POST['search']['value']);
            $this->db->or_like('VehicleName'$_POST['search']['value']);
            $this->db->or_like('DateOfInsurance'$_POST['search']['value']);
            $this->db->or_like('Insurer'$_POST['search']['value']);
            $this->db->or_like('PolicyNumber'$_POST['search']['value']);
            $this->db->or_like('Amount'$_POST['search']['value']);
            $this->db->or_like('Bill'$_POST['search']['value']);
            $this->db->or_like('NextInsuranceDate'$_POST['search']['value']);
            $this->db->or_like('Remark'$_POST['search']['value']);
            $this->db->group_end();
        }

        // sent request for ordering
        if (isset($_POST['order'])) {
            $this->db->order_by($this->order_column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        } else {
            $this->db->order_by('Id''desc');
        }
    }

    public function make_datatables()
    {
        $this->make_query();
        if ($_POST['length'] != -1) {
            $this->db->limit($_POST['length'], $_POST['start']);
        }
        $query $this->db->get();
        return $query->result();
    }

    public function get_filtered_data()
    {
        $this->make_query();
        $query $this->db->get();
        return $query->num_rows();
    }

    public function get_all_data()
    {
        $this->db->from($this->table);
        return $this->db->count_all_results();
    

Controller:
PHP Code:
$data = array();

 
// fetch vehicles records
 
$fetch_data $this->My_model->make_datatables();

 foreach (
$fetch_data as $veh) {

    $data[] = array(
       $veh->VehicleName,
       (!empty($veh->DateOfInsurance) ? date('d.m.Y'strtotime($veh->DateOfInsurance)) : ''),
       $veh->Amount,
       $veh->Insurer,
       $veh->Remark,
       (!empty($veh->NextInsuranceDate) ? date('d.m.Y'strtotime($veh->NextInsuranceDate)) : '')
    );
 }

 
$output = array(
   'draw' => intval($_POST['draw']),
   'recordsTotal' => $this->My_Model->get_all_data(),
   'recordsFiltered' => $this->My_model->get_filtered_data(),
   'data' => $data
 
);

 echo 
json_encode($output); 
Reply
#2

There is a library available maybe this makes it easier for you?

https://github.com/cahyadsn/ci-datatables
Reply
#3

This may also help you out.

DataTables AJAX Pagination with Search and Sort in CodeIgniter 3
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(This post was last modified: 12-01-2022, 09:12 AM by brianjamestorr.)

(11-30-2022, 12:30 PM)superior Wrote: There is a library available maybe this makes it easier for you?

https://github.com/cahyadsn/ci-datatables

Thanks man this solution is perfect and really easy to use  Angel

I use it this way in model/controller because of logic that i don't want to do in model. 

I hope this will help someone.

My model:

PHP Code:
$this->datatables->select('Id, Title, Plate');
$this->datatables->from('vehicles');
$results $this->datatables->generate('raw');

return 
$results

My controller:

PHP Code:
$data = [];

 
$result $this->Vozila_model->dohvati_vozila(); 

 foreach (
json_decode($result)->aaData as $val) {
   $buttons '';

   $data[] = array(
     $val[0],
     $val[1],
     $val[2],
     $buttons
   
);
 }

 
$output = array(
   'draw' => json_decode($result)->sEcho,
   'recordsTotal' => json_decode($result)->iTotalRecords,
 
   'recordsFiltered' => json_decode($result)->iTotalDisplayRecords,
   'data' => $data
 
);

 echo 
json_encode($output); 

My view, ajax:
Code:
var table = $('#data-table').DataTable({
    processing: true,
    serverSide: true,
    ajax: {
        url: '<?php echo base_url('vozila/gg'); ?>',
        type: 'post'
    }
});

(12-01-2022, 12:19 AM)InsiteFX Wrote: This may also help you out.

DataTables AJAX Pagination with Search and Sort in CodeIgniter 3

I tried this code and this one is too much to write or to place in function for reuse, also those 3 lines throwing error because default when you load table you doesn't have indexes set of  
PHP Code:
$postData['order'
until you click on header of the table.
If someone wants to use this you need to check for
PHP Code:
isset($_POST['order']) 
first and if is not set make default order by id or some other column.


PHP Code:
$rowperpage $postData['length']; // Rows display per page
$columnIndex $postData['order'][0]['column']; // Column index
$columnName $postData['columns'][$columnIndex]['data']; // Column name 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB