forked from kartik-v/yii2-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionColumn.php
277 lines (249 loc) · 10.7 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2016
* @package yii2-grid
* @version 3.1.2
*/
namespace kartik\grid;
use Yii;
use yii\base\Model;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* Extends the Yii's ActionColumn for the Grid widget [[\kartik\widgets\GridView]] with various enhancements.
* ActionColumn is a column for the [[GridView]] widget that displays buttons for viewing and manipulating the items.
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
*/
class ActionColumn extends \yii\grid\ActionColumn
{
use ColumnTrait;
/**
* @var boolean whether the column is hidden from display. This is different than the `visible` property, in the
* sense, that the column is rendered, but hidden from display. This will allow you to still export the column
* using the export function.
*/
public $hidden;
/**
* @var boolean|array whether the column is hidden in export output. If set to boolean `true`, it will hide the
* column for all export formats. If set as an array, it will accept the list of GridView export `formats` and
* hide output only for them.
*/
public $hiddenFromExport = true;
/**
* @var bool whether the action buttons are to be displayed as a dropdown
*/
public $dropdown = false;
/**
* @var array the HTML attributes for the Dropdown container. The class `dropdown` will be added. To align a
* dropdown at the right edge of the page container, you set the class to `pull-right`.
*/
public $dropdownOptions = [];
/**
* @var array the HTML attributes for the Dropdown menu. Applicable if `dropdown` is `true`.
*/
public $dropdownMenu = ['class' => 'text-left'];
/**
* @var array the dropdown button options. Applicable if `dropdown` is `true`. The following special options are
* recognized:
* `label`: the button label to be displayed. Defaults to `Actions`.
* `caret`: the caret symbol to be appended to the dropdown button.
* Defaults to `<span class="caret"></span>`
*/
public $dropdownButton = ['class' => 'btn btn-default'];
/**
* @var string the horizontal alignment of each column. Should be one of
* 'left', 'right', or 'center'.
*/
public $hAlign = GridView::ALIGN_CENTER;
/**
* @var string the vertical alignment of each column. Should be one of
* 'top', 'middle', or 'bottom'.
*/
public $vAlign = GridView::ALIGN_MIDDLE;
/**
* @var boolean whether to force no wrapping on all table cells in the column
* @see http://www.w3schools.com/cssref/pr_text_white-space.asp
*/
public $noWrap = false;
/**
* @var string the width of each column (matches the CSS width property).
* @see http://www.w3schools.com/cssref/pr_dim_width.asp
*/
public $width = '80px';
/**
* @var array HTML attributes for the view action button. The following additional option is recognized:
* `label`: string, the label for the view action button. This is not html encoded.
*/
public $viewOptions = [];
/**
* @var array HTML attributes for the update action button. The following additional option is recognized:
* `label`: string, the label for the update action button. This is not html encoded.
*/
public $updateOptions = [];
/**
* @var array HTML attributes for the delete action button. The following additional option is recognized:
* `label`: string, the label for the delete action button. This is not html encoded.
*/
public $deleteOptions = [];
/**
* @var boolean|string whether the page summary is displayed above the footer for this column. If this is set to a
* string, it will be displayed as is. If it is set to `false` the summary will not be displayed.
*/
public $pageSummary = false;
/**
* @var array HTML attributes for the page summary cell
*/
public $pageSummaryOptions = [];
/**
* @var boolean whether to just hide the page summary display but still calculate the summary based on
* `pageSummary` settings
*/
public $hidePageSummary = false;
/**
* @var boolean whether to merge the header title row and the filter row This will not render the filter for the
* column and can be used when `filter` is set to `false`. Defaults to `false`. This is only applicable when
* `filterPosition` for the grid is set to FILTER_POS_BODY.
*/
public $mergeHeader = true;
/**
* @var array the HTML attributes for the header cell tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $headerOptions = [];
/**
* @var array|\Closure the HTML attributes for the data cell tag. This can either be an array of attributes or an
* anonymous function ([[Closure]]) that returns such an array. The signature of the function should be the
* following: `function ($model, $key, $index, $column)`. A function may be used to assign different attributes
* to different rows based on the data in that row.
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $contentOptions = [];
/**
* @var bool is the dropdown menu to be rendered?
*/
protected $_isDropdown = false;
public function init()
{
/** @noinspection PhpUndefinedFieldInspection */
$this->_isDropdown = ($this->grid->bootstrap && $this->dropdown);
if (!isset($this->header)) {
$this->header = Yii::t('kvgrid', 'Actions');
}
$this->parseFormat();
$this->parseVisibility();
parent::init();
$this->initDefaultButtons();
$this->setPageRows();
}
/**
* Render default action buttons
*
* @return string
*/
protected function initDefaultButtons()
{
if (!isset($this->buttons['view'])) {
$this->buttons['view'] = function ($url) {
$options = $this->viewOptions;
$title = Yii::t('kvgrid', 'View');
$icon = '<span class="glyphicon glyphicon-eye-open"></span>';
$label = ArrayHelper::remove($options, 'label', ($this->_isDropdown ? $icon . ' ' . $title : $icon));
$options = array_replace_recursive(['title' => $title, 'data-pjax' => '0'], $options);
if ($this->_isDropdown) {
$options['tabindex'] = '-1';
return '<li>' . Html::a($label, $url, $options) . '</li>' . PHP_EOL;
} else {
return Html::a($label, $url, $options);
}
};
}
if (!isset($this->buttons['update'])) {
$this->buttons['update'] = function ($url) {
$options = $this->updateOptions;
$title = Yii::t('kvgrid', 'Update');
$icon = '<span class="glyphicon glyphicon-pencil"></span>';
$label = ArrayHelper::remove($options, 'label', ($this->_isDropdown ? $icon . ' ' . $title : $icon));
$options = array_replace_recursive(['title' => $title, 'data-pjax' => '0'], $options);
if ($this->_isDropdown) {
$options['tabindex'] = '-1';
return '<li>' . Html::a($label, $url, $options) . '</li>' . PHP_EOL;
} else {
return Html::a($label, $url, $options);
}
};
}
if (!isset($this->buttons['delete'])) {
$this->buttons['delete'] = function ($url) {
$options = $this->deleteOptions;
$title = Yii::t('kvgrid', 'Delete');
$icon = '<span class="glyphicon glyphicon-trash"></span>';
$label = ArrayHelper::remove($options, 'label', ($this->_isDropdown ? $icon . ' ' . $title : $icon));
$defaults = ['title' => $title, 'data-pjax' => 'false'];
$pjax = $this->grid->pjax ? true : false;
$pjaxContainer = $pjax ? $this->grid->pjaxSettings['options']['id'] : '';
if ($pjax) {
$defaults['data-pjax-container'] = $pjaxContainer;
}
$options = array_replace_recursive($defaults, $options);
$css = $this->grid->options['id'] . '-action-del';
Html::addCssClass($options, $css);
$view = $this->grid->getView();
$delOpts = Json::encode([
'css' => $css,
'pjax' => $pjax,
'pjaxContainer' => $pjaxContainer,
'lib' => ArrayHelper::getValue($this->grid->krajeeDialogSettings, 'libName', 'krajeeDialog'),
'msg' => Yii::t('kvgrid', 'Are you sure to delete this item?')
]);
ActionColumnAsset::register($view);
$js = "kvActionDelete({$delOpts});";
$view->registerJs($js);
$this->initPjax($js);
if ($this->_isDropdown) {
$options['tabindex'] = '-1';
return '<li>' . Html::a($label, $url, $options) . '</li>' . PHP_EOL;
} else {
return Html::a($label, $url, $options);
}
};
}
}
/**
* @inheritdoc
*/
public function renderDataCell($model, $key, $index)
{
$options = $this->fetchContentOptions($model, $key, $index);
return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
}
/**
* Renders the data cell.
*
* @param Model $model
* @param mixed $key
* @param int $index
*
* @return mixed|string
*/
protected function renderDataCellContent($model, $key, $index)
{
$content = parent::renderDataCellContent($model, $key, $index);
$options = $this->dropdownButton;
if ($this->_isDropdown) {
$label = ArrayHelper::remove($options, 'label', Yii::t('kvgrid', 'Actions'));
$caret = ArrayHelper::remove($options, 'caret', ' <span class="caret"></span>');
$options = array_replace_recursive($options, ['type' => 'button', 'data-toggle' => 'dropdown']);
Html::addCssClass($options, 'dropdown-toggle');
$button = Html::button($label . $caret, $options);
Html::addCssClass($this->dropdownMenu, 'dropdown-menu');
$dropdown = $button . PHP_EOL . Html::tag('ul', $content, $this->dropdownMenu);
Html::addCssClass($this->dropdownOptions, 'dropdown');
return Html::tag('div', $dropdown, $this->dropdownOptions);
}
return $content;
}
}