Column filters server side
Column filters server side
Hello everyone, i wanna do this, but in server side: https://datatables.net/extensions/fixedheader/examples/options/columnFiltering.html
I use codeigniter 4, library for ajax TablesIgniter. And my global search is working, but i dont know how to link my functions from controller with filters from each column.
Jquery for datatable:
$(document).ready(function(){
$('#example thead tr').clone(true).appendTo( '#example thead' );
$('#example thead tr:eq(1) th').each( function (i) {
var title = $(this).text();
if(i == 6){
$(this).html( '<input id="Filter'+i+'" type="text" placeholder="'+ title +'" disabled/>' );
}else{
$(this).html( '<input id="Filter'+i+'" type="text" placeholder="'+ title +'" />' );
}
$( 'input', this ).on( 'keyup change', function () {
if ( table.column(i).search() !== this.value ) {
table
.column(i)
.search( this.value )
.draw();
}
} );
} );
var table = $('#example').DataTable({
"order":[],
"serverSide":true,
"searching": true,
"processing": true,
"lengthChange":false,
"orderCellsTop": true,
"fixedHeader": true,
"ajax":{
url:"<?=base_url('inspectors/fetch_all')?>",
type:'POST',
data:{
'id': $('#Filter0').val(),
'sbe_name': $('#Filter1').val(),
'au_name': $('#Filter2').val(),
'date_fr': $('#Filter3').val(),
'date_to': $('#Filter4').val(),
'duration': $('#Filter5').val(),
},
}
});
});
My functions from controller:
Here fetch_all() for global search and fetch_id() for search at id
/*Search all*/
public function fetch_all()
{
$modelInspector = new M_inspectors();
$data_table = new TablesIgniter();
$data_table->setTable($modelInspector->noticeTable())
->setDefaultOrder("Id_ins", "DESC")
->setSearch(["Id_ins","TitleS", "TitleA", "DateFr","DateTo","Duration"])
->setOrder(["Id_ins", "TitleS", "TitleA", "DateFr","DateTo","Duration"])
->setOutput(["Id_ins", "TitleS", "TitleA", "DateFr","DateTo","Duration", $modelInspector->button()]);
return $data_table->getDatatable();
}
/*Search ID*/
public function fetch_id()
{
$modelInspector = new M_inspectors();
$data_table = new TablesIgniter();
$data_table->setTable($modelInspector->noticeTable())
->setDefaultOrder("Id_ins", "DESC")
->setSearch(["Id_ins"])
->setOrder(["Id_ins", "TitleS", "TitleA", "DateFr","DateTo","Duration"])
->setOutput(["Id_ins", "TitleS", "TitleA", "DateFr","DateTo","Duration", $modelInspector->button()]);
return $data_table->getDatatable();
}
/********************************/
Functions from model:
/*Filters*/
public function noticeTable()
{
$builder = $this->db->table('inspectors')
->join('sbe','sbe.Id_sbe = inspectors.SbeId')
->join('authority','authority.Id_au = inspectors.AuthorityId');
return $builder;
}
public function button()
{
$action_button = function($row){
return '
<a type="button" onclick="delete_data(<?=$inspectorItem['."Id_ins".']?>)" class="btn btn-outline-danger btn-sm"><i class="bi bi-trash-fill"></i></a>
<a href="<?php base_url()?>/edit_ins/<?=$inspectorItem['."Id_ins".']?>" class="btn btn-outline-primary btn-sm" type="button"><i class="bi bi-pencil-square"></i></a>
';
};
return $action_button;
}
Please, explain how can i link my filters with function from controller
Answers
This SO thread may help. Do you need
serverSide
? How many records are you expecting to have?Colin
It's my task from job. Thank you, i hope it helps