Skip to content

Commit 9213df4

Browse files
authored
Merge pull request #259 from unclead/InsaneSkull-patch-1
Added support for ColumnOptions in BaseColumn
2 parents 579d5bb + 438ac8e commit 9213df4

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Yii2 multiple input change log
77
- #250 accept `\Traversable` in TableRenderer and ListRenderer for `yield` compatibility (bscheshirwork)
88
- #253 allow to omit a name for static column
99
- #257 added `jsPositions` property for the `BaseRenderer` to set right order js-code in `jsInit` and `jsTemplates` (Spell6inder)
10+
- #259 added `columnOptions` property in the `BaseColumn` for TableRenderer and ListRenderer to support HTML options of individual column (InsaneSkull)
1011

1112
2.17.0
1213
======

src/TabularColumn.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @property TabularInput $context
1919
*/
2020
class TabularColumn extends BaseColumn
21-
{
21+
{
2222
/**
2323
* Returns element's name.
2424
*
@@ -71,4 +71,4 @@ public function setModel($model)
7171

7272
parent::setModel($model);
7373
}
74-
}
74+
}

src/components/BaseColumn.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ abstract class BaseColumn extends BaseObject
131131
*/
132132
public $nameSuffix;
133133

134+
/**
135+
* @var array|\Closure the HTML attributes for the indivdual table body column. This can be either an array
136+
* specifying the common HTML attributes for indivdual body column, or an anonymous function that
137+
* returns an array of the HTML attributes. It should have the following signature:
138+
*
139+
* ```php
140+
* function ($model, $index, $context)
141+
* ```
142+
*
143+
* - `$model`: the current data model being rendered
144+
* - `$index`: the zero-based index of the data model in the model array
145+
* - `$context`: the widget object
146+
*
147+
* @since 2.18.0
148+
*/
149+
public $columnOptions = [];
150+
134151
/**
135152
* @var Model|ActiveRecordInterface|array
136153
*/

src/renderers/ListRenderer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ public function renderCellContent($column, $index)
233233
Html::addCssClass($options, 'form-group');
234234
}
235235

236+
if (is_callable($column->columnOptions)) {
237+
$columnOptions = call_user_func($column->columnOptions, $column->getModel(), $index, $this->context);
238+
} else {
239+
$columnOptions = $column->columnOptions;
240+
}
241+
242+
$options = array_merge_recursive($options, $columnOptions);
243+
236244
$content = Html::beginTag('div', $options);
237245

238246
if (empty($column->title)) {

src/renderers/TableRenderer.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,17 @@ public function renderCellContent($column, $index)
306306
Html::addCssClass($wrapperOptions, 'has-error');
307307
}
308308

309+
if (is_callable($column->columnOptions)) {
310+
$columnOptions = call_user_func($column->columnOptions, $column->getModel(), $index, $this->context);
311+
} else {
312+
$columnOptions = $column->columnOptions;
313+
}
314+
315+
Html::addCssClass($columnOptions, 'list-cell__' . $column->name);
316+
309317
$input = Html::tag('div', $input, $wrapperOptions);
310318

311-
return Html::tag('td', $input, [
312-
'class' => 'list-cell__' . $column->name,
313-
]);
319+
return Html::tag('td', $input, $columnOptions);
314320
}
315321

316322

0 commit comments

Comments
 (0)