Tip: Use the "+" links for multiple column sorting
Tip: Sorting has 3 states - ascending, descending and non-sorting state.
Name Surname Street address
Birthdate Weight (kg)
Okagbue Odinakachukwu 90 avenue Ferdinand de Lesseps 1996-09-30 37.4
Lidya Ambessa Trg revolucije 53 1996-02-09 38.3
Doris Novak Massbyntie 70 1995-04-25 40
Chiara Lorenzo 1731 Dry Pine Bay Rd 1995-09-06 41.8
Isobel Stevenson 444 Charter Street 1995-10-24 42.5
Petar Mesker Kaptensgränd 10 1996-11-08 43.5
Anneke Johansen Kimpling 17 1996-11-23 43.6
Sara Helminen 52 Ferny Avenue 1995-02-12 43.8
Temshe Hagos Maaningantie 88 1991-01-18 44
Enwelumokwu Chioke Seefeldstrasse 36 1991-08-17 44
Wiga Wojciechowska Escuadro 63 1994-02-15 44.1
Loredana Trentini Mannerheimintie 77 1996-07-10 44.2

Code

# SortingGrid.php


class SortingGrid extends BaseGrid
{

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

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

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

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

}