Filters
Overview #
Filters are group of components to let user perform data filtering visually. The list of filters will be provided inside filters()
method of Resource or Glasses.
Define filter #
<?php
use koolreport\dashboard\admin\filters\SelectFilter;
class Country extends SelectFilter
{
protected function onCreated()
{
//Set the title of filter
$this->title("Country");
}
protected function apply($query, $value)
{
//Return condition-applied query
return $query->where("country", $value);
}
protected function options()
{
//Since this is SelectFilter so you have options() method
//to provide list of options for Select
//In here we list all available country from customers table
return AutoMaker::table("customers")
->select("country")
->distinct();
}
}
The most important method in a filter is the apply()
in which you will receive the $query
and $value
as parameters. Your job is to apply the condition into $query
with the help of $value
which is input from user and then return the $query
after applied.
Register filter #
To register filter, you just need to provide instance into filters()
method of Resource or Glasses.
protected function filters()
{
return [
Country::create(),
SalaryRange::create(),
...
];
}
List of filters #
SelectFilter #
SelectFilter will provide a <select>
element for your users to select. The SelectFilter have options()
methods to let you provide list of selected options.
Example:
protected function options()
{
//Simple array form
return [
"United State",
"Germany",
"France"
];
//Associate array "text"=>"value"
return [
"text"=>"value",
"United State"=>"usa",
"Germany"=>"germany",
]
//From PDOSource
return AutoMaker::table("customers")
->select("country")
->distinct();
}
The return from options can be array, associate array, DataStore or PDOSource.
Select2Filter #
Select2Filter uses single Select2 for input. It has the same options()
method like SelectFilter.
CheckBoxFilter #
CheckBoxFilter uses CheckBoxList for input. It has the same options()
method like SelectFilter.
RadioFilter #
CheckBoxFilter uses RadioList for input. It has the same options()
method like SelectFilter.
MultiSelectFilter #
MultiSelectFilter uses MultiSelect for input. It has the same options()
method like SelectFilter.
The $value
parameter inside apply()
method is in array form containing multi-selection of user.
Example:
<?php
use koolreport\dashboard\admin\filters\MultiSelectFilter;
class Countries extends MultiSelectFilter
{
protected function onCreated()
{
$this->title("Countries");
}
protected function apply($query, $value)
{
// The value is in array format so we use whereIn
return $query->whereIn("country", $value);
}
protected function options()
{
return AutoMaker::table("customers")
->select("country")
->distinct();
}
}
MultiSelect2Filter #
MultiSelect2Filter uses multiple Select2 for inputs. The usage is the same with MultiSelectFilter.
NumberRangeFilter #
NumberRangeFilter provides 2 textboxes to let user input min
and max
values. The $value
parameter in apply()
method is array form like this [$min,$max]
.
TextBoxFilter #
TextBoxFilter uses TextBox for inputs.
ToggleFilter #
ToggleFilter uses Toggle for inputs. The $value
parameter inside apply()
method will have value 1
or 0
.
DateTimeFilter #
DateTimeFilter use DateTimePicker for inputs. The $value
parameter inside apply()
method is in string form containing datetime.
DateRangeFilter #
DateRangeFilter use DateRangePicker for inputs. The $value
parameter inside apply()
method is in array format containing start date and end date [$startDate,$endDate]
.
Get started with KoolReport
KoolReport will help you to construct good php data report by gathering your data from multiple sources, transforming them into valuable insights, and finally visualizing them in stunning charts and graphs.