ione ione - 1 month ago 13
jQuery Question

multi column searching datatables

I have datatables of serverside. I had problems with multiple column searching,all column work,if i search only one by one column.but if i multi column for searching,filter didn't work.example.if i search only "name" filter work,and if i search only one start_date filter work too.but if i search name and date simultaneously.filter didn't work .
so here my code:



//my script:
var table = $('#dataTables');

// begin first table
table.dataTable({

"ajax": {
"url": "<?php echo site_url('leader') ?>",
"type": "POST",
"data": function(d) {
d.full_name = $('#full_name').val();
d.start_date = $('#start_date').val(); //whats right code?
d.end_date = $('#end_date').val(); //whats right code?

},
},
.....
.....
$('#btn-search').click(function(){
var data_tables = $('#dataTables').DataTable();
data_tables
.search( this.value )
.draw();
})

<!-- my view: -->
<div class="form-inline">
<div class="form-group">
<input type="text" name="full_name" class="form-control" id="full_name" placeholder="Name"/>
</div>

<div class="form-group">
<div class="input-group date date-picker" data-date-format="dd-mm-yyyy">
<input type="text" name="start_date" class="form-control" readonly id="start_date" size="8">
<span class="input-group-btn">
<button class="btn default" type="button">
<i class="fa fa-calendar"></i>
</button>
</span>
</div>
</div>
<div class="form-group">
<div class="input-group date date-picker" data-date-format="dd-mm-yyyy">
<input type="text" name="end_date" class="form-control" readonly id="end_date" size="8">
<span class="input-group-btn">
<button class="btn default" type="button">
<i class="fa fa-calendar"></i>
</button>
</span>
</div>
</div>
<button class="btn btn-warning" id="btn-search">Search</button>
</div>





serverside:



if ($full_name)
{
$where = "And full_name = '$full_name'";
}
else if($start_date)
{
$where = "And start_date >= to_date('$start_date','DD-MM-YYYY') ";
}
else if($end_date)
{
$where = "And end_date <= to_date('$end_date','DD-MM-YYYY') ";
}

else
{
$where = " "; //how to combine 3 above
}





how to create condition if i search multicolumn in datatables?

Answer

your server side code needs a change try something like this:

$where = "";
if (!empty($full_name))
{
    $where .= "And full_name = '$full_name' ";
}
if(!empty($start_date))
{
    $where .= "And start_date >= to_date('$start_date','DD-MM-YYYY') ";
}

if(!empty($end_date))
{
    $where = "And end_date <= to_date('$end_date','DD-MM-YYYY') ";
}

on a sidenote: you tagged this question as a Codeigniter related question - whats the reason behind for not using the query builder? (your approach is a bit "unsecure"...)