Skip to content

Commit 6f2e302

Browse files
committed
Pass more params to a prepareValue closure
1 parent a474ced commit 6f2e302

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

src/components/BaseColumn.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,19 @@ public function isHiddenInput()
214214

215215
/**
216216
* Prepares the value of column.
217-
*
217+
* @param array $contextParams the params who passed to closure:
218+
* string $id the id of input element
219+
* string $name the name of input element
220+
* string $indexPlaceholder The index placeholder of multiple input. The {$indexPlaceholder} template will be replace by $index
221+
* int $index The index of multiple input
222+
* int $columnIndex The index of current model attributes
218223
* @return mixed
219224
*/
220-
protected function prepareValue()
225+
protected function prepareValue($contextParams = [])
221226
{
222227
$data = $this->getModel();
223228
if ($this->value instanceof \Closure) {
224-
$value = call_user_func($this->value, $data);
229+
$value = call_user_func($this->value, $data, $contextParams);
225230
} else {
226231
$value = null;
227232
if ($data instanceof ActiveRecordInterface ) {
@@ -284,11 +289,18 @@ private function normalize($name) {
284289
/**
285290
* Renders the input.
286291
*
287-
* @param string $name the name of the input
288-
* @param array $options the HTML options of input
292+
* @param string $name the name of the input
293+
* @param array $options the HTML options of input
294+
* @param array $contextParams the params who passed to closure:
295+
* string $id the id of input element
296+
* string $name the name of input element
297+
* string $indexPlaceholder The index placeholder of multiple input. The {$indexPlaceholder} template will be replace by $index
298+
* int $index The index of multiple input
299+
* int $columnIndex The index of current model attributes
289300
* @return string
301+
* @throws InvalidConfigException
290302
*/
291-
public function renderInput($name, $options)
303+
public function renderInput($name, $options, $contextParams = [])
292304
{
293305
if ($this->options instanceof \Closure) {
294306
$optionsExt = call_user_func($this->options, $this->getModel());
@@ -301,7 +313,7 @@ public function renderInput($name, $options)
301313

302314
$value = null;
303315
if ($this->type !== self::TYPE_DRAGCOLUMN) {
304-
$value = $this->prepareValue();
316+
$value = $this->prepareValue($contextParams);
305317
}
306318

307319
if (isset($options['items'])) {

src/renderers/TableRenderer.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ private function hasHeader()
117117
if ($this->min === 0 || $this->isAddButtonPositionHeader()) {
118118
return true;
119119
}
120-
120+
121121
foreach ($this->columns as $column) {
122122
/* @var $column BaseColumn */
123123
if ($column->title) {
124124
return true;
125125
}
126126
}
127-
127+
128128
return false;
129129
}
130130

@@ -141,7 +141,7 @@ private function renderHeaderCell($column)
141141

142142
$options = $column->headerOptions;
143143
Html::addCssClass($options, 'list-cell__' . $column->name);
144-
144+
145145
return Html::tag('th', $column->title, $options);
146146
}
147147

@@ -186,7 +186,7 @@ protected function renderBody()
186186
$rows[] = $this->renderRowContent($i);
187187
}
188188
}
189-
189+
190190
return Html::tag('tbody', implode("\n", $rows));
191191
}
192192

@@ -207,13 +207,14 @@ private function renderRowContent($index = null, $item = null)
207207
$cells[] = $this->renderActionColumn($index, $item, true);
208208
}
209209

210+
$columnIndex = 0;
210211
foreach ($this->columns as $column) {
211212
/* @var $column BaseColumn */
212213
$column->setModel($item);
213214
if ($column->isHiddenInput()) {
214-
$hiddenInputs[] = $this->renderCellContent($column, $index);
215+
$hiddenInputs[] = $this->renderCellContent($column, $index, $columnIndex++);
215216
} else {
216-
$cells[] = $this->renderCellContent($column, $index);
217+
$cells[] = $this->renderCellContent($column, $index, $columnIndex++);
217218
}
218219
}
219220
if ($this->cloneButton) {
@@ -228,7 +229,7 @@ private function renderRowContent($index = null, $item = null)
228229
$hiddenInputs = implode("\n", $hiddenInputs);
229230
$cells[0] = preg_replace('/^(<td[^>]+>)(.*)(<\/td>)$/s', '${1}' . $hiddenInputs . '$2$3', $cells[0]);
230231
}
231-
232+
232233
$content = Html::tag('tr', implode("\n", $cells), $this->prepareRowOptions($index, $item));
233234

234235
if ($index !== null) {
@@ -263,9 +264,10 @@ protected function prepareRowOptions($index, $item)
263264
*
264265
* @param BaseColumn $column
265266
* @param int|null $index
267+
* @param int|null $columnIndex
266268
* @return string
267269
*/
268-
public function renderCellContent($column, $index)
270+
public function renderCellContent($column, $index, $columnIndex = null)
269271
{
270272
$id = $column->getElementId($index);
271273
$name = $column->getElementName($index);
@@ -279,7 +281,14 @@ public function renderCellContent($column, $index)
279281
if (substr($id, -4) === 'drag') {
280282
$options = ArrayHelper::merge($options, ['class' => $this->iconMap['drag-handle']]);
281283
}
282-
$input = $column->renderInput($name, $options);
284+
$input = $column->renderInput($name, $options, [
285+
'id' => $id,
286+
'name' => $name,
287+
'indexPlaceholder' => $this->getIndexPlaceholder(),
288+
'index' => $index,
289+
'columnIndex' => $columnIndex,
290+
'context' => $this->context,
291+
]);
283292

284293
if ($column->isHiddenInput()) {
285294
return $input;
@@ -305,15 +314,15 @@ public function renderCellContent($column, $index)
305314
if ($hasError) {
306315
Html::addCssClass($wrapperOptions, 'has-error');
307316
}
308-
317+
309318
if (is_callable($column->columnOptions)) {
310319
$columnOptions = call_user_func($column->columnOptions, $column->getModel(), $index, $this->context);
311320
} else {
312321
$columnOptions = $column->columnOptions;
313322
}
314323

315324
Html::addCssClass($columnOptions, 'list-cell__' . $column->name);
316-
325+
317326
$input = Html::tag('div', $input, $wrapperOptions);
318327

319328
return Html::tag('td', $input, $columnOptions);
@@ -386,7 +395,7 @@ private function renderAddButton()
386395
];
387396

388397
Html::addCssClass($options, $this->addButtonOptions['class']);
389-
398+
390399
return Html::tag('div', $this->addButtonOptions['label'], $options);
391400
}
392401

@@ -402,7 +411,7 @@ private function renderRemoveButton()
402411
];
403412

404413
Html::addCssClass($options, $this->removeButtonOptions['class']);
405-
414+
406415
return Html::tag('div', $this->removeButtonOptions['label'], $options);
407416
}
408417

0 commit comments

Comments
 (0)