forked from kartik-v/yii2-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpandRowColumn.php
executable file
·415 lines (379 loc) · 15.7 KB
/
ExpandRowColumn.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
/**
* @package yii2-grid
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2017
* @version 3.1.7
*/
namespace kartik\grid;
use Closure;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\JsExpression;
use yii\web\View;
/**
* An ExpandRowColumn can be used to expand a row and add content in a new row below it either directly or via ajax.
*
* To add an ExpandRowColumn to the gridview, add it to the [[GridView::columns|columns]] configuration as follows:
*
* ```php
* 'columns' => [
* // ...
* [
* 'class' => ExpandRowColumn::className(),
* // you may configure additional properties here
* ],
* ]
* ```
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @since 1.0
*/
class ExpandRowColumn extends DataColumn
{
/**
* @var integer|Closure the value of this attribute will identify the state of the current row. The following values
* are supported:
* - [[GridView::ROW_EXPANDED]] or 0: the row will be expanded by default and will display the collapse indicator.
* - [[GridView::ROW_COLLAPSED]] or 1: the row will be collapsed by default and will display the expand indicator.
* - [[GridView::ROW_NONE]] or -1: no indicator will be displayed for the row.
*
* If this is not set, `$model[$attribute]` will be used to obtain the value. If this value is evaluated as empty
* or null, it is treated as [[GridView::ROW_NONE]]. This can also be an anonymous function that returns one of
* the values above. The anonymous function should have the signature `function ($model, $key, $index, $column)`,
* where:
* - `$model`: _\yii\base\Model_, is the data model.
* - `$key`: _string|object_, is the primary key value associated with the data model.
* - `$index`: _integer_, is the zero-based index of the data model among the model array returned by [[dataProvider]].
* - `$column`: _ExpandRowColumn_, is the column object instance.
*/
public $value = GridView::ROW_NONE;
/**
* @var boolean whether to toggle the expansion/collapse by clicking on the table row. To disable row click for
* specific elements within the row you can add the CSS class `kv-disable-click` to tags/elements to disable
* the toggle functionality.
*/
public $enableRowClick = false;
/**
* @var array list of tags in the row on which row click will be disabled.
*/
public $rowClickExcludedTags = ['a', 'button', 'input'];
/**
* @var array additional data that will be passed to the ajax load function as key value pairs
*/
public $extraData = [];
/**
* @var string icon for the expand indicator. If this is not set, it will derive values automatically using the
* following rules:
* - If GridView `bootstrap` property is set to `true`, it will default to [[GridView::ICON_EXPAND]]
* or `<span class="glyphicon glyphicon-expand"></span>`
* - If GridView `bootstrap` property is set to `false`, then it will default to `+`.
*/
public $expandIcon;
/**
* @var string icon for the collapse indicator. If this is not set, it will derive values automatically using the
* following rules:
* - If GridView `bootstrap` property is set to `true`, it will default to [[GridView::ICON_COLLAPSE]]
* or `<span class="glyphicon glyphicon-collapse-down"></span>`
* - If GridView `bootstrap` property is set to `false`, then it will default to `-`.
*/
public $collapseIcon;
/**
* @var string title to display on hover of expand indicator for each row.
*/
public $expandTitle;
/**
* @var string title to display on hover of collapse indicator for each row.
*/
public $collapseTitle;
/**
* @var string title to display on hover of expand indicator at header.
*/
public $expandAllTitle;
/**
* @var string title to display on hover of collapse indicator at header.
*/
public $collapseAllTitle;
/**
* @var integer default state of the header. The following values can be set:
* - [[GridView::ROW_COLLAPSED]]: Will set all rows to collapsed and display the [[expandIcon]].
* - [[GridView::ROW_EXPANDED]]: Will set all rows to expanded and display the [[collapseIcon]].
*/
public $defaultHeaderState = GridView::ROW_COLLAPSED;
/**
* @var boolean whether to enable caching of expanded row content while expanding the row using ajax triggered
* action (applicable when `detailUrl` is set). Defaults to `true`.
*/
public $enableCache = true;
/**
* @var boolean whether to allow only one row to be expanded at a time and auto collapse other expanded rows
* whenever a row is expanded. Defaults to `false`.
*/
public $expandOneOnly = false;
/**
* @var boolean allow batch expansion or batch collapse of all rows by clicking the header indicator. Defaults to
* `true`.
*/
public $allowBatchToggle = true;
/**
* @var boolean|Closure whether the expand icon indicator is disabled. Defaults to `false`. If set to `true`, one
* cannot collapse or expand the sections. This can be setup as an anonymous function having the signature:
* `function ($model, $key, $index, $column)`, where:
* - `$model`: _\yii\base\Model_, is the data model.
* - `$key`: _string|object_, is the primary key value associated with the data model.
* - `$index`: _integer_, is the zero-based index of the data model among the model array returned by [[dataProvider]].
* - `$column`: _ExpandRowColumn_, is the column object instance.
*/
public $disabled = false;
/**
* @var string|Closure the detail content (html markup) to be displayed in the expanded row. Either [[detail]]
* or [[detailUrl]] must be entered. This can be a normal html markup or an anonymous function that returns the
* markup. The anonymous function should have the signature:
* `function ($model, $key, $index, $column)`, where:
* - `$model`: _\yii\base\Model_, is the data model.
* - `$key`: _string|object_, is the primary key value associated with the data model.
* - `$index`: _integer_, is the zero-based index of the data model among the model array returned by [[dataProvider]].
* - `$column`: _ExpandRowColumn_, is the column object instance.
*/
public $detail = '';
/**
* @var string the url/action that would render the detail content via ajax. Either `detail` OR `detailUrl` must be
* entered. The ajax response must return the content/markup to render. The extension automatically passes the
* following data parameters to the server URL as POST data:
* - `expandRowKey` the key associated with the data model
* - `expandRowIndex` the zero-based index of the data model among the models array returned by
* [[GridView::dataProvider]].
* @see http://api.jquery.com/jquery.load/
*/
public $detailUrl;
/**
* @var string|JsExpression the javascript callback to execute after loading the content via ajax. Only applicable
* when detailUrl is provided.
*/
public $onDetailLoaded = '';
/**
* @var array|Closure the HTML attributes for the expanded table row. This can be an array or an anonymous function
* of the signature:
* `function ($model, $key, $index, $column)`, where:
* - `$model`: _\yii\base\Model_, is the data model.
* - `$key`: _string|object_, is the primary key value associated with the data model.
* - `$index`: _integer_, is the zero-based index of the data model among the model array returned by [[dataProvider]].
* - `$column`: _ExpandRowColumn_, is the column object instance.
*/
public $detailOptions = [];
/**
* @var string the CSS class for the detail content table row.
*/
public $detailRowCssClass = GridView::TYPE_INFO;
/**
* @var string|integer the animation duration to slide up/down the detail row.
* @see http://api.jquery.com/slidedown/
*/
public $detailAnimationDuration = 'slow';
/**
* @inheritdoc
*/
public $hiddenFromExport = true;
/**
* @inheritdoc
*/
public $hAlign = GridView::ALIGN_CENTER;
/**
* @inheritdoc
*/
public $width = '50px';
/**
* @inheritdoc
*/
public $mergeHeader = true;
/**
* @var string hashed javascript variable to store grid expand row options
*/
protected $_hashVar;
/**
* Parses data for Closure and returns accordingly
*
* @param string|Closure $data the data to parse.
* @param Model $model the data model.
* @param string|object $key the key associated with the data model.
* @param integer $index the zero-based index of the data model among the models array returned by
* [[GridView::dataProvider]].
* @param ExpandRowColumn $column the column object instance.
*
* @return mixed
*/
protected static function parseData($data, $model, $key, $index, $column)
{
if ($data instanceof Closure) {
$data = call_user_func($data, $model, $key, $index, $column);
}
return $data;
}
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (empty($this->detail) && empty($this->detailUrl)) {
throw new InvalidConfigException("Either the 'detail' or 'detailUrl' must be entered");
}
$this->format = 'raw';
$this->expandIcon = $this->getIcon('expand');
$this->collapseIcon = $this->getIcon('collapse');
$this->setProp('expandTitle', Yii::t('kvgrid', 'Expand'));
$this->setProp('collapseTitle', Yii::t('kvgrid', 'Collapse'));
$this->setProp('expandAllTitle', Yii::t('kvgrid', 'Expand All'));
$this->setProp('collapseAllTitle', Yii::t('kvgrid', 'Collapse All'));
$onDetailLoaded = $this->onDetailLoaded;
if (!empty($onDetailLoaded) && !$onDetailLoaded instanceof JsExpression) {
$onDetailLoaded = new JsExpression($onDetailLoaded);
}
if ($this->allowBatchToggle) {
$this->headerOptions['title'] = $this->expandAllTitle;
}
if ($this->allowBatchToggle && $this->defaultHeaderState === GridView::ROW_EXPANDED) {
$this->headerOptions['title'] = $this->collapseTitle;
}
$class = 'kv-expand-header-cell';
$class .= $this->allowBatchToggle ? ' kv-batch-toggle' : ' text-muted';
Html::addCssClass($this->headerOptions, $class);
$view = $this->grid->getView();
ExpandRowColumnAsset::register($view);
$clientOptions = Json::encode(
[
'gridId' => $this->grid->options['id'],
'hiddenFromExport' => $this->hiddenFromExport,
'detailUrl' => empty($this->detailUrl) ? '' : $this->detailUrl,
'onDetailLoaded' => $onDetailLoaded,
'expandIcon' => $this->expandIcon,
'collapseIcon' => $this->collapseIcon,
'expandTitle' => $this->expandTitle,
'collapseTitle' => $this->collapseTitle,
'expandAllTitle' => $this->expandAllTitle,
'collapseAllTitle' => $this->collapseAllTitle,
'rowCssClass' => $this->detailRowCssClass,
'animationDuration' => $this->detailAnimationDuration,
'expandOneOnly' => $this->expandOneOnly,
'enableRowClick' => $this->enableRowClick,
'enableCache' => $this->enableCache,
'rowClickExcludedTags' => array_map('strtoupper', $this->rowClickExcludedTags),
'collapseAll' => false,
'expandAll' => false,
'extraData' => $this->extraData,
]
);
$this->_hashVar = 'kvExpandRow_' . hash('crc32', $clientOptions);
$view->registerJs("var {$this->_hashVar} = {$clientOptions};\n", View::POS_HEAD);
$view->registerJs("kvExpandRow({$this->_hashVar});");
}
/**
* @inheritdoc
*/
public function getDataCellValue($model, $key, $index)
{
$value = parent::getDataCellValue($model, $key, $index);
/** @noinspection PhpUnusedLocalVariableInspection */
$icon = '';
if ($value === GridView::ROW_EXPANDED) {
$type = 'collapsed';
$icon = $this->collapseIcon;
} elseif ($value === GridView::ROW_COLLAPSED) {
$type = 'expanded';
$icon = $this->expandIcon;
} else {
return $value;
}
$detail = static::parseData($this->detail, $model, $key, $index, $this);
$detailOptions = static::parseData($this->detailOptions, $model, $key, $index, $this);
$disabled = static::parseData($this->disabled, $model, $key, $index, $this) ? ' kv-state-disabled' : '';
if ($this->hiddenFromExport) {
Html::addCssClass($detailOptions, 'skip-export');
}
$detailOptions['data-index'] = $index;
$detailOptions['data-key'] = !is_string($key) && !is_numeric($key) ?
(is_array($key) ? Json::encode($key) : (string)$key) : $key;
$id = $this->grid->options['id'];
Html::addCssClass($detailOptions, ['kv-expanded-row', $id]);
$content = Html::tag('div', $detail, $detailOptions);
return <<< HTML
<div class="kv-expand-row {$id}{$disabled}">
<div class="kv-expand-icon kv-state-{$type}{$disabled} {$id}">{$icon}</div>
<div class="kv-expand-detail skip-export {$id}" style="display:none;">
{$content}
</div>
</div>
HTML;
}
/**
* @inheritdoc
*/
public function renderDataCell($model, $key, $index)
{
$options = $this->fetchContentOptions($model, $key, $index);
$css = 'kv-expand-icon-cell';
$options['title'] = $this->expandTitle;
if ($this->value === GridView::ROW_EXPANDED) {
$options['title'] = $this->collapseTitle;
}
if (static::parseData($this->disabled, $model, $key, $index, $this)) {
$css .= ' kv-state-disabled';
}
Html::addCssClass($options, $css);
$this->initPjax("kvExpandRow({$this->_hashVar});");
return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
}
/**
* Get icon indicator
*
* @param string $type one of `expand` or `collapse`
*
* @return string the icon indicator markup
*/
protected function getIcon($type)
{
$setting = "{$type}Icon";
if (!empty($this->$setting)) {
return $this->$setting;
}
$bs = $this->grid->bootstrap;
if ($type === 'expand') {
return $bs ? GridView::ICON_EXPAND : '+';
}
if ($type === 'collapse') {
return $bs ? GridView::ICON_COLLAPSE : '-';
}
return null;
}
/**
* Sets property for the object instance if not set
*
* @param string $prop the property name
* @param string $val the property value
*/
protected function setProp($prop, $val)
{
if (!isset($this->$prop)) {
$this->$prop = $val;
}
}
/**
* @inheritdoc
*/
protected function renderHeaderCellContent()
{
if ($this->header !== null) {
return parent::renderHeaderCellContent();
}
$icon = $this->expandIcon;
$css = 'kv-expand-header-icon kv-state-collapsed';
if ($this->defaultHeaderState === GridView::ROW_EXPANDED) {
$icon = $this->collapseIcon;
$css = 'kv-expand-header-icon kv-state-expanded';
}
return "<div class='{$css}'>{$icon}</div>";
}
}