$orders = $this->sort->getAttributeOrders();
<?= DropDownSorter::widget(['sort' => $dataProvider['sort'], 'selectOptions' => ['id' => list_form_sort', 'class' => 'js-dropdown-box']]); ?>
$attributes = empty($this->attributes) ? $this->sort->attributes : $this->attributes;
<?php
namespace common\components;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\data\Sort;
use yii\helpers\Html;
class DropDownSorter extends Widget
{
/**
* @var Sort the sort definition
*/
public $sort;
/**
* @var array list of the attributes that support sorting. If not set, it will be determined
* using [[Sort::attributes]].
*/
public $attributes;
/**
* @var array HTML attributes for the sorter container tag.
* @see \yii\helpers\Html::ul() for special attributes.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = ['class' => 'sorter'];
/**
* @var array HTML attributes for the dropDown in a sorter container tag
* @since 2.0.6
*/
public $selectOptions = [];
/**
* Initializes the sorter.
*/
public function init()
{
if ($this->sort === null) {
throw new InvalidConfigException('The "sort" property must be set.');
}
}
/**
* Executes the widget.
* This method renders the sort links.
*/
public function run()
{
echo $this->renderSortDropDown();
}
/**
* Renders the sort links.
*
* @return string the rendering result
*/
protected function renderSortDropDown()
{
$attributes = empty($this->attributes) ? $this->sort->attributes : $this->attributes;
$items = [];
$selected = null;
foreach ($attributes as $key => $name) {
$items[$key] = $name['label']['asc'];
$items['-' . $key] = $name['label']['desc'];
}
$orders = $this->sort->getAttributeOrders();
if (count($orders) == 1) {
$key = key($orders);
$value = $orders[$key];
$selected = ($value == SORT_DESC ? '-' : '') . $key;
}
return Html::dropDownList($this->sort->sortParam, $selected, $items, $this->selectOptions);
}
}