Skip to content

Commit 17c77b3

Browse files
author
Eugene Tupikov
committed
try another approach to replace index placeholder in options
1 parent d17966f commit 17c77b3

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/components/BaseColumn.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use yii\helpers\ArrayHelper;
1919
use yii\helpers\Html;
2020
use yii\helpers\Inflector;
21-
use yii\helpers\Json;
2221
use unclead\multipleinput\renderers\BaseRenderer;
2322

2423
/**
@@ -332,15 +331,8 @@ public function renderInput($name, $options, $contextParams = [])
332331
$method = 'render' . Inflector::camelize($this->type);
333332

334333
// @see https://github.com/unclead/yii2-multiple-input/issues/261
335-
// we have to replace placeholder in all nested options and the best way to do it is:
336-
// 1) encode the options to a json string
337-
// 2) replace the placeholder
338-
// 3) decode the json string back to array
339-
// But we have to leave the placeholder in case of processing a row template
340334
if ($contextParams['index'] !== null) {
341-
$options_string = Json::encode($options);
342-
$options_string = str_replace('{' . $contextParams['indexPlaceholder'] . '}', $contextParams['index'], $options_string);
343-
$options = Json::decode($options_string);
335+
$options = $this->replaceIndexPlaceholderInOptions($options, $contextParams['indexPlaceholder'], $contextParams['index']);
344336
}
345337

346338
$value = null;
@@ -361,6 +353,19 @@ public function renderInput($name, $options, $contextParams = [])
361353
return strtr($this->inputTemplate, ['{input}' => $input]);
362354
}
363355

356+
private function replaceIndexPlaceholderInOptions($options, $indexPlaceholder, $index)
357+
{
358+
$result = [];
359+
foreach ($options as $key => $value) {
360+
if (is_array($value)) {
361+
$result[$key] = $this->replaceIndexPlaceholderInOptions($value, $indexPlaceholder, $index);
362+
} elseif (is_string($value)) {
363+
$result[$key] = str_replace('{' . $indexPlaceholder . '}', $index, $value);
364+
}
365+
}
366+
367+
return $result;
368+
}
364369

365370
/**
366371
* Renders drop down list.

0 commit comments

Comments
 (0)