Filtering
The data tables can be filtered, with use of the filters.
Toggling the feature
By default, the filtration feature is enabled for every data table. This can be configured with the filtration_enabled
option:
kreyu_data_table:
defaults:
filtration:
enabled: true
use Symfony\Config\KreyuDataTableConfig;
return static function (KreyuDataTableConfig $config) {
$defaults = $config->defaults();
$defaults->filtration()->enabled(true);
};
use Kreyu\Bundle\DataTableBundle\Type\AbstractDataTableType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ProductDataTableType extends AbstractDataTableType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'filtration_enabled' => true,
]);
}
}
use App\DataTable\Type\ProductDataTableType;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ProductController extends AbstractController
{
use DataTableFactoryAwareTrait;
public function index()
{
$dataTable = $this->createDataTable(
type: ProductDataTableType::class,
query: $query,
options: [
'filtration_enabled' => true,
],
);
}
}
Filtering is enabled, but submitting a filtration form does nothing?
Ensure that the handleRequest()
method of the data table is called:
class ProductController
{
public function index(Request $request)
{
$dataTable = $this->createDataTable(...);
$dataTable->handleRequest($request);
}
}
Saving applied filters
By default, the filtration feature persistence is disabled for every data table.
You can configure the persistence globally using the package configuration file, or its related options:
kreyu_data_table:
defaults:
filtration:
persistence_enabled: true
# if persistence is enabled and symfony/cache is installed, null otherwise
persistence_adapter: kreyu_data_table.filtration.persistence.adapter.cache
# if persistence is enabled and symfony/security-bundle is installed, null otherwise
persistence_subject_provider: kreyu_data_table.persistence.subject_provider.token_storage
use Symfony\Config\KreyuDataTableConfig;
return static function (KreyuDataTableConfig $config) {
$defaults = $config->defaults();
$defaults->filtration()
->persistenceEnabled(true)
// if persistence is enabled and symfony/cache is installed, null otherwise
->persistenceAdapter('kreyu_data_table.filtration.persistence.adapter.cache')
// if persistence is enabled and symfony/security-bundle is installed, null otherwise
->persistenceSubjectProvider('kreyu_data_table.persistence.subject_provider.token_storage')
;
};
use Kreyu\Bundle\DataTableBundle\Persistence\PersistenceAdapterInterface;
use Kreyu\Bundle\DataTableBundle\Persistence\PersistenceSubjectProviderInterface;
use Kreyu\Bundle\DataTableBundle\Type\AbstractDataTableType;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ProductDataTableType extends AbstractDataTableType
{
public function __construct(
#[Autowire(service: 'kreyu_data_table.filtration.persistence.adapter.cache')]
private PersistenceAdapterInterface $persistenceAdapter,
#[Autowire(service: 'kreyu_data_table.persistence.subject_provider.token_storage')]
private PersistenceSubjectProviderInterface $persistenceSubjectProvider,
) {
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'filtration_persistence_enabled' => true,
'filtration_persistence_adapter' => $this->persistenceAdapter,
'filtration_persistence_subject_provider' => $this->persistenceSubjectProvider,
]);
}
}
use App\DataTable\Type\ProductDataTableType;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait;
use Kreyu\Bundle\DataTableBundle\Persistence\PersistenceAdapterInterface;
use Kreyu\Bundle\DataTableBundle\Persistence\PersistenceSubjectProviderInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ProductController extends AbstractController
{
use DataTableFactoryAwareTrait;
public function __construct(
#[Autowire(service: 'kreyu_data_table.filtration.persistence.adapter.cache')]
private PersistenceAdapterInterface $persistenceAdapter,
#[Autowire(service: 'kreyu_data_table.persistence.subject_provider.token_storage')]
private PersistenceSubjectProviderInterface $persistenceSubjectProvider,
) {
}
public function index()
{
$dataTable = $this->createDataTable(
type: ProductDataTableType::class,
query: $query,
options: [
'filtration_persistence_enabled' => true,
'filtration_persistence_adapter' => $this->persistenceAdapter,
'filtration_persistence_subject_provider' => $this->persistenceSubjectProvider,
],
);
}
}
Default filtration
The default filtration data can be overridden using the data table builder's setDefaultFiltrationData()
method:
use Kreyu\Bundle\DataTableBundle\DataTableBuilderInterface;
use Kreyu\Bundle\DataTableBundle\Type\AbstractDataTableType;
use Kreyu\Bundle\DataTableBundle\Filter\FiltrationData;
use Kreyu\Bundle\DataTableBundle\Filter\FilterData;
use Kreyu\Bundle\DataTableBundle\Filter\Operator;
class ProductDataTableType extends AbstractDataTableType
{
public function buildDataTable(DataTableBuilderInterface $builder, array $options): void
{
$builder->setDefaultFiltrationData(new FiltrationData([
'name' => new FilterData(value: 'John', operator: Operator::Contains),
]));
// or by creating the filtration data from an array:
$builder->setDefaultFiltrationData(FiltrationData::fromArray([
'name' => ['value' => 'John', 'operator' => 'contains'],
]));
}
}
Events
The following events are dispatched when filter()
method of the DataTableInterface
is called:
PRE_FILTER
Dispatched before the filtration data is applied to the query. Can be used to modify the filtration data, e.g. to force application of some filters.
POST_FILTER
Dispatched after the filtration data is applied to the query and saved if the filtration persistence is enabled; Can be used to execute additional logic after the filters are applied.
The dispatched events are instance of the DataTableFiltrationEvent
:
use Kreyu\Bundle\DataTableBundle\Event\DataTableFiltrationEvent;
class DataTableFiltrationListener
{
public function __invoke(DataTableFiltrationEvent $event): void
{
$dataTable = $event->getDataTable();
$filtrationData = $event->getFiltrationData();
// for example, modify the filtration data, then save it in the event
$event->setFiltrationData($filtrationData);
}
}