Name Surname Weight (kg)  
Okagbue Odinakachukwu 37.4  
Lidya Ambessa 38.3  
Doris Novak 40  
Chiara Lorenzo 41.8  
Isobel Stevenson 42.5  
Petar Mesker 43.5  
Anneke Johansen 43.6  
Sara Helminen 43.8  
Temshe Hagos 44  
Enwelumokwu Chioke 44  
Wiga Wojciechowska 44.1  
Loredana Trentini 44.2  

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');
	}

}