-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionColumn.php
105 lines (93 loc) · 2.6 KB
/
ActionColumn.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* @author Anton Ivanov (https://github.com/yapi68/)
* @link https://github.com/yapi68/yii2-action-column#readme
* @license https://github.com/yapi68/yii2-action-column/blob/master/LICENSE
*/
namespace yapi68\actioncolumn;
use yii\grid\Column;
use yii\helpers\Url;
use yii\widgets\Menu;
/**
* Class ActionColumn
* @package yapi68\actioncolumn
*/
class ActionColumn extends Column
{
/**
* @var array|\Closure dropdown menu items
*/
public $items;
/**
* @var string widget container class
*/
public $containerClass = 'dropdown action-column';
/**
* @var string dropdown menu container class
*/
public $itemsContainerClass = 'dropdown-menu';
/**
* @var string column header
*/
public $header = '<i class="fa fa-bars"></i>';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
\Yii::setAlias('actioncolumn', __DIR__);
ActionColumnAsset::register(\Yii::$app->controller->view);
}
/**
* Set default dropdown menu items
* @param mixed $key the key associated with the data model
* @return array dropdown menu items array
*/
protected function initDefaultItems($key)
{
return [
[
'label' => 'View',
'url' => Url::to(['view', 'id' => $key]),
],
[
'label' => 'Edit',
'url' => Url::to(['edit', 'id' => $key]),
],
[
'label' => 'Copy',
'url' => Url::to(['copy', 'id' => $key]),
],
[
'label' => '',
'url' => null,
'options' => ['class' => 'divider']
],
[
'label' => 'Delete',
'url' => Url::to(['delete', 'id' => $key]),
]
];
}
/**
* @inheritdoc
*/
protected function renderDataCellContent($model, $key, $index)
{
$toggle = '<span data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">'
. '<i class="fa fa-bars text-muted"></i></span>';
$items = $this->items;
if ($this->items instanceof \Closure) {
$items = call_user_func($this->items, $model, $key, $index);
}
$nav = Menu::widget([
'items' => $items ?: $this->initDefaultItems($key),
'options' => [
'class' => $this->itemsContainerClass,
'role' => 'menu'
]
]);
return "<div class=\"$this->containerClass\">$toggle$nav</div>";
}
}