Custom filtering with yadcf
Range, select and multi-select filters
yadcf is a jQuery plugin that adds filter components to table columns.
The following example shows how to make the Datatables PHP library compatible with yadcf.
| ID | Track Name | Genre | MediaType |
|---|---|---|---|
| Loading… | |||
| ID | Track Name | Genre | MediaType |
<?php
use Ozdemir\Datatables\Datatables;
use Ozdemir\Datatables\DB\SQLite;
Route::get('/ajax/custom-filter2', function () {
$path = dirname(__DIR__).'/database/Chinook_Sqlite_AutoIncrementPKs.sqlite';
$dt = new Datatables(new SQLite($path));
$dt->query('Select TrackId, Track.Name, Genre.Name as Genre, MediaType.Name as MediaType
from Track
JOIN Genre ON Genre.GenreId = Track.GenreId
JOIN MediaType ON MediaType.MediaTypeId = Track.MediaTypeId');
$dt->filter('TrackId', function () {
if ($this->searchValue() === '-yadcf_delim-') return;
$val = explode('-yadcf_delim-', $this->searchValue());
return $this->between($val[0], $val[1] ?? null);
});
$dt->filter('MediaType', function () {
if ($this->searchValue() === '') return;
return $this->whereIn(explode('|', $this->searchValue()));
});
$dt->setDistinctResponseFrom('Genre');
$dt->setDistinctResponse([
'MediaType' => [
'AAC audio file',
'MPEG audio file',
'Protected AAC audio file',
'Protected MPEG-4 video file',
'Purchased AAC audio file',
],
]);
return $dt->generate();
});