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)
Katariina Aavikko Fagradalsbraut 57 1967-01-03 98.5
Semrawit Abaalom Dunajska 106 1990-11-01 64.7
Asmeret Abdullah 11 Rue Hubert de Lisle 1977-02-22 54.6
Elen Abdullah Brunnenstrasse 121 1977-10-03 80.5
Birikti Abel 227 Nutter Street 1961-09-10 52.7
Hana Abraham Agip u. 15. 1976-01-17 65.7
Shishay Abraham 1287 Fort Street 1973-09-24 87.8
Tekle Abrha Sahantie 5 1990-08-26 78.5
Jennifer Ackerman Slotsgade 69 1982-11-25 55.4
Huriyyah Adonay Rue des Tanneurs 167 1980-03-01 68.2
Meis Adriaans Fjölnisgata 49 1966-07-14 51
Faiz Adriaensen Þjónustusvæði 88 1968-07-13 88.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');
	}

}