silvershop/silverstripe-listsorter

There is no license information available for the latest version (2.1.1) of this package.

Easily provide front-end sorting controls for SilverStripe lists

Installs: 24 397

Dependents: 3

Suggesters: 0

Security: 0

Stars: 15

Watchers: 6

Forks: 8

Open Issues: 1

Type:silverstripe-vendormodule

2.1.1 2018-01-29 14:17 UTC

This package is auto-updated.

Last update: 2023-05-24 16:57:02 UTC


README

Build Status Version License

A front-end control for sorting SilverStripe lists easily. The aim of this module is to make sorting lists as simple as it is to use PaginatedList.

Requirements

  • SilverStripe 4+

Usage

There are a few ways you can define sort options within an array.

Make a public function on your controller:

function getSorter(){
	$sorts = [
		'Title', //DB field name only
		'Popularity' => 'Popularity DESC', //map title to sort sql
		'Price' => array('BasePrice' => 'ASC'), //map title to data list sort
		ListSorter_Option::create('Age', 'Created DESC', //object
			new ListSorter_Option('Age', array('Created' => 'ASC')) //reverse
		)
	;
	return new ListSorter($this->request,$sorts);
}

Call that function when updating your list:

public function getSortableChildren() {
	$list = $this->Children();
	$list = $this->getSorter()->sortList($list);
	return $list;
}

Use my template or roll your own.

<% include Sorter %>
<ul>
<% loop SortableChildren %>
	<li>$Title</li>
<% end_loop %>
</ul>