Name Surname Weight (kg)  
Martina Trommler 80.3  
Marij Rašpolić 75.7  
Lyyli Vaara 102.2  
Dýrleif Björnsdóttir 91.5  
Martha Yohannes 74.8  
Amadi Onyekachukwu 96.9  
Satu Tukio 59.4  
Mary Black 49.2  
Consuela Takens 81.3  
Aaya Weerdenburg 67.7  
Ormos Széll 66.4  
Hrvojka Petković 82.9  

Code

# FilterGrid.php

<?php

declare(strict_types = 1);

use Nette\Forms\Form;
use TwiGrid\DataGrid;
use Nette\Forms\Container;
use Nette\Database\Explorer;
use Nette\Database\Table\Selection;


final class FilterGrid extends DataGrid
{

	private Explorer $database;


	public function __construct(Explorer $database)
	{
		parent::__construct();

		$this->database = $database;
	}


	protected function build(): void
	{
		$this->setPrimaryKey('id');
		$this->addColumn('firstname', 'Name')->setSortable();
		$this->addColumn('surname', 'Surname')->setSortable();
		$this->addColumn('kilograms', 'Weight (kg)')->setSortable();

		$this->setFilterFactory(static function (Container $container): void {
			$container->addText('firstname');
			$container->addText('surname');

			$container->addText('kilograms')
				->addCondition(Form::FILLED)
				->addRule(Form::FLOAT);
		});

		$this->setDataLoader(function (array $filters, array $order): Selection {
			$users = $this->database->table('user');

			// filtering
			foreach ($filters as $column => $value) {
				if ($column === 'kilograms') {
					$users->where("$column <= ?", $value);

				} else {
					$users->where("$column LIKE ?", "$value%");
				}
			}

			// sorting
			foreach ($order as $column => $dir) {
				$users->order($column . ($dir === TwiGrid\Components\Column::DESC ? ' DESC' : ''));
			}

			return $users->limit(12);
		});

		$this->setRecordVariable('user');
	}

}