Datatables Server Side Codeigniter 3

Sebuah data yang sudah terlalu banyak akan mengalami proses load data yang lambat, apalagi data table tersebut menggunakan Datatables yang biasa. Sehingga saat data diakses maka Datatables akan menampilkan semua data dari query. Oleh sebab itu kita membutuhkan datatables server side agar setiap page data pada table tidak di load sekaligus melainkan per limit.

  • Code Pada Controller
function index()
{
$this->load->view('include/header');
$this->load->view('page_transaction/V_transaction_out_sparepart');
$this->load->view('include/footer');
}

function get_data_sparepart()
{
$list = $this->M_transaction->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $field) {

$no++;

$row = array();
$row[] = $no;
$row[] = $field->no_barang;
$row[] = $field->nama_barang;

$data[] = $row;
}

$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->M_transaction->count_all(),
"recordsFiltered" => $this->M_transaction->count_filtered(),
"data" => $data,
);
//output dalam format JSON
echo json_encode($output);
}
  • Code Pada Model
var $table = 'vw_sparepart'; //nama tabel dari database
var $column_order = array(null, 'no_barang','nama_barang'); //field yang ada di table user
var $column_search = array('no_barang','nama_barang'); //field yang diizin untuk pencarian
var $order = array('no_barang' => 'asc'); // default order

private function _get_datatables_query()
{

$warehouse_code = $this->session->userdata('employee_warehouse');
$array = array('warehouse_code' => $warehouse_code, 'stock !=' => 0);
$this->db->from($this->table);
$this->db->where($array);

$i = 0;

foreach ($this->column_search as $item) // looping awal
{
if($_POST['search']['value']) // jika datatable mengirimkan pencarian dengan metode POST
{

if($i===0) // looping awal
{
$this->db->group_start();
$this->db->like($item, $_POST['search']['value']);
}
else
{
$this->db->or_like($item, $_POST['search']['value']);
}

if(count($this->column_search) - 1 == $i)
$this->db->group_end();
}
$i++;
}

if(isset($_POST['order']))
{
$this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}

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

function count_filtered()
{
$this->_get_datatables_query();
$query = $this->db->get();
return $query->num_rows();
}

public function count_all()
{
$warehouse_code = $this->session->userdata('employee_warehouse');
$array = array('warehouse_code' => $warehouse_code, 'stock !=' => 0);
$this->db->from($this->table);
$this->db->where($array);
return $this->db->count_all_results();
}
  • Code Pada View
	<title>Transaction Sparepart Out List</title>
<link rel="stylesheet" href="<?php echo base_url(); ?>asset/css/table.css">
<link href="<?php echo base_url('asset/datatables_serverside/datatables/css/jquery.dataTables.min.css')?>" rel="stylesheet">
</head>
<body>
<div class="containerweb">

<?php echo $this->session->flashdata('msg');?>
<div style="height:30px;"><b>DAFTAR TRANSAKSI PENGELUARAN BARANG :</b></div>

<div style="overflow-x:auto;">
<table id="table" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>No</th>
<th>Kode Barang</th>
<th>Nama Barang</th>
</tr>
</thead>
</table>
</div>
</div>

</div>
</body>
<footer>
<script src="<?php echo base_url('asset/datatables_serverside/jquery/jquery-2.2.3.min.js')?>"></script>
<script src="<?php echo base_url('asset/datatables_serverside/datatables/js/jquery.dataTables.min.js')?>"></script>
<script type="text/javascript">
var table;
$(document).ready(function() {

//datatables
table = $('#table').DataTable({

"processing": true,
"serverSide": true,
"order": [],

"ajax": {
"url": "<?php echo site_url('Transaction/get_data_sparepart')?>",
"type": "POST"
},

"columnDefs": [
{
"targets": [ 0 ],
"orderable": false,
},
],

});

});

</script>

Selesai, dan sekarang silahkan dicoba akses ke table datanya maka akan sangat cepat proses aksesnya.



Total 0 comment with 0 comment reply