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)
Corinna Pisani Östbygatan 42 1961-01-15 92.8
Klara Walczak 29 rue Isambard 1961-02-21 100.8
Corinne L'Hiver Οδός Υγείας 22 1961-02-25 58
Archie Fowler 81 Caradon Hill 1961-03-08 83.7
Peppin Lemaître Via San Cosmo fuori Porta Nolana 32 1961-03-25 114.3
Łucja Pawlak Parmova 108 1961-04-02 78
Madeleine Hopkins Büsingstrasse 1 1961-04-22 61.6
Leevi Tiisanoja Rudower Strasse 98 1961-04-27 93.3
Samuel Sundström Dunajska 20 1961-05-21 94.8
Victorine Doucet 1719 Skinner Hollow Road 1961-05-24 83.8
Anke Mathiesen Ilichova 3 1961-07-10 70.3
Rita Habte Rue du Manoir 254 1961-07-17 77.8

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

}