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)
Aaliyah Storey 2483 Toy Avenue 1963-12-17 71.7
Aaya Weerdenburg Via San Domenico Soriano 147 1970-05-04 67.7
Abbey Tunn Kajaaninkatu 39 1963-12-10 94.2
Abe Sneep Δηληγιάννη 206 1980-03-01 69.5
Abigail McKay Σουνίου 143 1980-09-19 80.6
Adaora Eluemuno 84 Bath Rd 1971-06-18 52.2
Adaora Ikechukwu Ctra. Beas-Cortijos Nuevos 41 1985-12-29 49
Adelbert Dávid Dalmatinova 112 1984-06-23 73.5
Adelheid Sørensen Liljerum Grenadjärtorpet 43 1994-03-15 75.8
Adina Wallin 90 Sea Road 1970-08-28 76.2
Admir Sagadin Kimpling 76 1964-07-30 92.1
Agi Miklós Idvägen 51 1984-05-23 86

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

}