Star

Custom filtering

Hook your own logic into a column's filter

Datatables PHP library has the ability to perform custom filtering via the filter() function. The function provides a helper class to simplify the process.
The following example shows how to apply a between() filter on the TrackId column.
IDTrack NameAlbumMediaType
Loading…
IDTrack NameAlbumMediaType
<?php
use Ozdemir\Datatables\Datatables;
use Ozdemir\Datatables\DB\SQLite;

Route::get('/ajax/custom-filter', function () {
    $path = dirname(__DIR__).'/database/Chinook_Sqlite_AutoIncrementPKs.sqlite';

    $dt = new Datatables(new SQLite($path));
    $dt->query('Select TrackId, Track.Name, Title as Album, MediaType.Name as MediaType
                from Track
                JOIN Album ON Album.AlbumId = Track.AlbumId
                JOIN MediaType ON MediaType.MediaTypeId = Track.MediaTypeId');

    $dt->filter('TrackId', function () {
        return $this->between(5, 11);
    });

    return $dt->generate();
});