-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathButtonDropdownSorter.php
90 lines (78 loc) · 2.3 KB
/
ButtonDropdownSorter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* @see http://www.yiiframework.com/doc-2.0/yii-widgets-linksorter.html
*/
namespace rsr\yii2;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\Html;
/**
* ButtonDropdownSorter renders bootstrap ButtonDropdown of sort links for the given sort definition.
* ButtonDropdownSorter will generate a bootstrap ButtonDropdown which contains every attribute declared in [[sort]].
*
* @author Reza Saberi Rad <[email protected]>
*/
class ButtonDropdownSorter extends Widget
{
/**
* @var string lablel of dropdown button
*/
public $label;
/**
* @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 https://github.com/yiisoft/yii2-bootstrap/blob/master/ButtonDropdown.php for special attributes and details on how attributes are being rendered.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [
'class' => 'sorter'
];
/**
* 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->renderSortButtonDropdown();
}
/**
* Renders the sort ButtonDropdown
*
* @return string the rendering result
*/
protected function renderSortButtonDropdown()
{
$attributes = empty($this->attributes) ? array_keys($this->sort->attributes) : $this->attributes;
$links = [];
foreach ($attributes as $name) {
$links[] = Html::tag('li', $this->sort->link($name, [
'tabindex' => '-1'
]));
}
if (empty($this->label))
$this->label = 'Sort';
return \yii\bootstrap\ButtonDropdown::widget([
'label' => $this->label,
'dropdown' => [
'items' => $links
]
]);
}
}