burnbright / silverstripe-listsorter
3.0.0
2023-08-23 05:07 UTC
Requires
- silverstripe/framework: ^4.0 | ^5.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.0
Replaces
README
这是一个用于轻松排序 SilverStripe 列表的前端控件。该模块的目的是使排序列表变得像使用 PaginatedList 一样简单。
要求
- SilverStripe 4+ 或 5+
使用方法
您可以在数组中定义几种排序选项。
在你的控制器上创建一个公开函数
public function getSorter(){ $sorts = [ 'Title', //DB field name only 'Popularity' => 'Popularity DESC', //map title to sort sql 'Price' => ['BasePrice' => 'ASC'], //map title to data list sort ListSorter_Option::create('Age', ['Created' => 'DESC'], //object ListSorter_Option::create('Age', ['Created' => 'ASC']) //reverse ) ; return ListSorter::create($this->request,$sorts); }
当更新列表时调用该函数
public function getSortableChildren() { $list = $this->Children(); $list = $this->getSorter()->sortList($list); return $list; }
使用我的模板或自行开发。
<% include Sorter %>
<ul>
<% loop SortableChildren %>
<li>$Title</li>
<% end_loop %>
</ul>
与 Silvershop 一起使用
Silvershop 的 PageCategoryController 预定义了一些排序。如果您想定义自己的排序选项,可以向 ProductCategory 添加一个扩展,如下所示
<?php namespace MyNamespace\SilverShop\Extensions; use SilverShop\ListSorter\ListSorter; use SilverShop\ListSorter\ListSorterOption; use SilverStripe\Core\Extension; use SilverStripe\Security\Security; class ProductCategorySorting extends Extension { public function updateSorter(ListSorter $sorter) { $basePriceOptionDESC = ListSorterOption::create('BasePrice highest first', ['BasePrice' => 'DESC']); $basePriceOptionASC = ListSorterOption::create('BasePrice lowest first', ['BasePrice' => 'ASC']); $titleOptionASC = ListSorterOption::create('Title a-z', ['Title' => 'ASC']); $titleOptionDESC = ListSorterOption::create('Title z-a', ['Title' => 'DESC']); $newestOption = ListSorterOption::create('Newest first', ['Created' => 'DESC']); $popularityOption = ListSorterOption::create('Most Popular', ['Popularity' => 'DESC']); //overwrite all settings //you can use $sorter->addSortOption($option) if you want to add a sort option $sorter->setSortOptions([ $basePriceOptionASC, $basePriceOptionDESC, $titleOptionASC, $titleOptionDESC, $newestOption, $popularityOption ]); } }
然后,将此扩展添加到您的配置中的 ProductCategoryController
SilverShop\Page\ProductCategoryController: extensions: - MyNamespace\Silvershop\Extensions\ProductCategorySorting