forked from kartik-v/yii2-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionColumn.php
executable file
·377 lines (344 loc) · 14.9 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/**
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2017
* @package yii2-grid
* @version 3.1.6
*/
namespace kartik\grid;
use Closure;
use Yii;
use yii\grid\ActionColumn as YiiActionColumn;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* The ActionColumn is a column that displays buttons for viewing and manipulating the items and extends the
* [[YiiActionColumn]] with various enhancements.
*
* To add an ActionColumn to the gridview, add it to the [[GridView::columns|columns]] configuration as follows:
*
* ```php
* 'columns' => [
* // ...
* [
* 'class' => ActionColumn::className(),
* // you may configure additional properties here
* ],
* ]
* ```
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
*/
class ActionColumn extends YiiActionColumn
{
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 boolean 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. This configuration will be applicable only if [[dropdown]] is `true`.
* The following special options are recognized:
*
* - `label`: _string_', the button label to be displayed. Defaults to `Actions`.
* - `caret`: _string_', 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 [[GridView::ALIGN_LEFT]],
* [[GridView::ALIGN_RIGHT]], or [[GridView::ALIGN_CENTER]].
*/
public $hAlign = GridView::ALIGN_CENTER;
/**
* @var string the vertical alignment of each column. Should be one of [[GridView::ALIGN_TOP]],
* [[GridView::ALIGN_BOTTOM]], or [[GridView::ALIGN_MIDDLE]].
*/
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 options are recognized:
* - `label`: _string_, the label for the view action button. This is not html encoded. Defaults to `View`.
* - `icon`: _null_|_array_|_string_ the icon HTML attributes as an _array_ or the raw icon markup as _string_
* or _false_ to disable the icon and just use text label instead. When set as a string, this is not HTML
* encoded. If null or not set, the default icon with CSS `glyphicon glyphicon-eye-open` will be displayed
* as the icon for the default button.
*/
public $viewOptions = [];
/**
* @var array HTML attributes for the update action button. The following additional options are recognized:
* - `label`: _string_, the label for the update action button. This is not html encoded. Defaults to `Update`.
* - `icon`: _null_|_array_|_string_ the icon HTML attributes as an _array_ or the raw icon markup as _string_
* or _false_ to disable the icon and just use text label instead. When set as a string, this is not HTML
* encoded. If null or not set, the default icon with CSS `glyphicon glyphicon-pencil` will be displayed
* as the icon for the default button.
*/
public $updateOptions = [];
/**
* @var array HTML attributes for the delete action button. The following additional options are recognized:
* - `label`: _string_, the label for the delete action button. This is not html encoded. Defaults to `Delete`.
* - `message`: _string_, the delete confirmation message to display when the delete button is clicked.
* Defaults to `Are you sure to delete this item?`.
* - `icon`: _null_|_array_|_string_ the icon HTML attributes as an _array_ or the raw icon markup as _string_
* or _false_ to disable the icon and just use text label instead. When set as a string, this is not HTML
* encoded. If null or not set, the default icon with CSS `glyphicon glyphicon-trash` will be displayed
* as the icon for the default button.
*/
public $deleteOptions = [];
/**
* @var bool whether to trigger delete via PJAX request. If set to `false` the default yii2 action column delete
* behavior using POST method will be triggered. Setting this to `true` will trigger a pjax delete with confirmation
* message dialog using Krajee dialog.
*/
public $pjaxDelete = true;
/**
* @var boolean|string|Closure the page summary that is displayed above the footer. You can set it to one of the
* following:
* - `false`: the summary will not be displayed.
* - `true`: the page summary for the column will be calculated and displayed using the
* [[pageSummaryFunc]] setting.
* - `string`: will be displayed as is.
* - `Closure`: you can set it to an anonymous function with the following signature:
*
* ```php
* // example 1
* function ($summary, $data, $widget) { return 'Count is ' . $summary; }
* // example 2
* function ($summary, $data, $widget) { return 'Range ' . min($data) . ' to ' . max($data); }
* ```
*
* where:
*
* - the `$summary` variable will be replaced with the calculated summary using the [[pageSummaryFunc]] setting.
* - the `$data` variable will contain array of the selected page rows for the column.
*/
public $pageSummary = false;
/**
* @var string the summary function that will be used to calculate the page summary for the column.
*/
public $pageSummaryFunc = GridView::F_SUM;
/**
* @var array HTML attributes for the page summary cell. The following special attributes are available:
* - `prepend`: _string_, a prefix string that will be prepended before the pageSummary content
* - `append`: _string_, a suffix string that will be appended after the pageSummary content
*/
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
* [[GridView::filterPosition]] for the grid is set to [[GridView::FILTER_POS_BODY]].
*/
public $mergeHeader = true;
/**
* @var array the HTML attributes for the header cell tag.
* @see 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 Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $contentOptions = [];
/**
* @var boolean is the dropdown menu to be rendered?
*/
protected $_isDropdown = false;
/**
* @inheritdoc
*/
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();
}
/**
* @inheritdoc
*/
public function renderDataCell($model, $key, $index)
{
$options = $this->fetchContentOptions($model, $key, $index);
return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
}
/**
* Renders button icon
*
* @param array $options HTML attributes for the action button element
* @param array $iconOptions HTML attributes for the icon element. The following additional options are recognized:
* - `tag`: _string_, the HTML tag to render the icon. Defaults to `span`.
*
* @return string
*/
protected function renderIcon(&$options, $iconOptions = [])
{
$icon = ArrayHelper::remove($options, 'icon');
if ($icon === false) {
$icon = '';
} elseif (!is_string($icon)) {
if (is_array($icon)) {
$iconOptions = array_replace_recursive($iconOptions, $icon);
}
$tag = ArrayHelper::remove($iconOptions, 'tag', 'span');
$icon = Html::tag($tag, '', $iconOptions);
}
return $icon;
}
/**
* Renders button label
*
* @param array $options HTML attributes for the action button element
* @param string $title the action button title
* @param array $iconOptions HTML attributes for the icon element (see [[renderIcon]])
*
* @return string
*/
protected function renderLabel(&$options, $title, $iconOptions = [])
{
$label = ArrayHelper::remove($options, 'label');
if (is_null($label)) {
$icon = $this->renderIcon($options, $iconOptions);
if (strlen($icon) > 0) {
$label = $this->_isDropdown ? ($icon . ' ' . $title) : $icon;
} else {
$label = $title;
}
}
return $label;
}
/**
* Sets a default button configuration based on the type
*
* @param string $type the type of the button
* @param string $title the title of the button
* @param string $icon the meaningful glyphicon suffix name for the button
*/
protected function setDefaultButton($type, $title, $icon)
{
if (isset($this->buttons[$type])) {
return;
}
$this->buttons[$type] = function ($url) use ($type, $title, $icon) {
$opts = "{$type}Options";
$options = ['title' => $title, 'data-pjax' => '0'];
if ($type === 'delete') {
$msg = ArrayHelper::remove($options, 'message', Yii::t(
'kvgrid',
'Are you sure to delete this {item}?',
['item' => isset($this->grid->itemLabelSingle) ? $this->grid->itemLabelSingle : Yii::t('kvgrid', 'item')]
));
if ($this->pjaxDelete) {
$pjax = $this->grid->pjax ? true : false;
$pjaxContainer = $pjax ? $this->grid->pjaxSettings['options']['id'] : '';
if ($pjax) {
$options['data-pjax-container'] = $pjaxContainer;
}
$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' => $msg,
]
);
ActionColumnAsset::register($view);
$js = "kvActionDelete({$delOpts});";
$view->registerJs($js);
$this->initPjax($js);
} else {
$options['data-method'] = 'post';
$options['data-confirm'] = $msg;
}
}
$options = array_replace_recursive($options, $this->buttonOptions, $this->$opts);
$label = $this->renderLabel($options, $title, ['class' => "glyphicon glyphicon-{$icon}"]);
$link = Html::a($label, $url, $options);
if ($this->_isDropdown) {
$options['tabindex'] = '-1';
return "<li>{$link}</li>\n";
} else {
return $link;
}
};
}
/**
* @inheritdoc
*/
protected function initDefaultButtons()
{
$this->setDefaultButton('view', Yii::t('kvgrid', 'View'), 'eye-open');
$this->setDefaultButton('update', Yii::t('kvgrid', 'Update'), 'pencil');
$this->setDefaultButton('delete', Yii::t('kvgrid', 'Delete'), 'trash');
}
/**
* @inheritdoc
*/
protected function renderDataCellContent($model, $key, $index)
{
$content = parent::renderDataCellContent($model, $key, $index);
$options = $this->dropdownButton;
$trimmed = trim($content);
if ($this->_isDropdown && !empty($trimmed)) {
$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;
}
}