Reputation: 31
So I have been looking around for an answer on how to do this and haven't had much luck so I thought I would post something on here. What I'm looking for is a way to set a default value for a filter so when the page loads it will automatically search the datatable according to this initial value. For example. I would like to pre-load the range_date filter with a specific date - A date that will actually be taken from a $_GET variable, so this value can not be hard coded. My current column code looks like this....
{column_number : 4, filter_type: "range_date", date_format: 'yy-mm-dd'},`enter code here`
So far I have found one source about adding exFilterColumn
but there are no examples on how to get this working with filter_type: "range_date"
Any ideas?
Upvotes: 1
Views: 1454
Reputation: 31
After a bit of digging I have figured out how to get exFilterColumn
working with a range_date filter. First I had to add the following to my column initialization:
filter_delay: 1500, filter_default_label: 'Search'
Then I had to add
yadcf.exFilterColumn(tableOne, [
[1, {
from: '2016-11-02',
to: '2016-11-03'
}],
]);
My complete code looks like this:
// Appointment datatable
var table = $('#tableID').dataTable( {
"processing": true,
"serverSide": true,
"ajax":{
url :"ajax/users-get-table-data.php" // json datasource
type: "post", // method , by default get
data:"workOrderStatusID=2",
},
}).yadcf([
{column_number : 0, filter_type: "text", filter_default_label: ['Search']},
{column_number : 1, filter_type: "range_date", date_format: 'yy-mm-dd', filter_delay: 1500, filter_default_label: 'Search'}
]);
yadcf.exFilterColumn(table, [
// 1 is the matching column of my targeted range_date filter
[1, {
<? //setting the date from get variable
$date = mysqli_real_escape_string($mysqli,$_GET['date']);
$tomorrow = date('Y-m-d',strtotime($date . "+1 days"));
?>
from: '<? echo $date; ?>',
to: '<? echo $tomorrow; ?>'
}],
]);
Incase you are reading this and want to pre load a regular column filter (not a range date) you can use the following:
yadcf.exFilterColumn(table, [
// 1 being the column number, and "value" being the value you want it to initially search for when page loads
[1, "value"]
]);
Upvotes: 2