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


use Nette\Forms\Form;


class FilterGrid extends BaseGrid
{

	/** @return void */
	protected function build()
	{
		parent::build();

		$this->setPrimaryKey('id');
		$this->addColumn('firstname', 'Name')->setSortable();
		$this->addColumn('surname', 'Surname')->setSortable();
		$this->addColumn('kilograms', 'Weight (kg)')->setSortable();

		$this->setFilterFactory([$this, 'filterFactory']);
		$this->setDataLoader([$this, 'dataLoader']);
	}


	/** @return Nette\Forms\Container */
	public function filterFactory()
	{
		$c = new Nette\Forms\Container;

		$c->addText('firstname');
		$c->addText('surname');

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

		return $c;
	}


	/**
	 * @param  FilterGrid $grid
	 * @param  array $filters
	 * @param  array $order
	 * @return Nette\Database\Table\Selection
	 */
	public function dataLoader(FilterGrid $grid, array $filters, array $order)
	{
		$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);
	}

}
	

# BaseGrid.php


use Nette\Http\Session as NSession;
use Nette\Database\Context as NdbContext;


abstract class BaseGrid extends TwiGrid\DataGrid
{

	/** @var NdbContext */
	protected $database;


	/**
	 * @param  NSession $session
	 * @param  NdbContext $database
	 */
	public function __construct(NSession $session, NdbContext $database)
	{
		parent::__construct($session);
		$this->database = $database;
	}


	/** @return void */
	protected function build()
	{
		parent::build();
		$this->setRecordVariable('user');
	}

}