Skip to content

Commit 2cfe212

Browse files
author
Eugene Tupikov
committed
Implemented ability to use widget as column type
1 parent 1f1a4ed commit 2cfe212

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

examples/models/ExampleModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public function init()
4242

4343
$this->schedule = [
4444
[
45-
'day' => 0,
45+
'day' => '27.02.2015',
4646
'user_id' => 1,
4747
'priority' => 1
4848
],
4949
[
50-
'day' => 0,
50+
'day' => '27.02.2015',
5151
'user_id' => 2,
5252
'priority' => 2
5353
],

examples/views/example.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use unclead\widgets\examples\models\ExampleModel;
66
use yii\helpers\Html;
77

8+
// Note: You have to install https://github.com/kartik-v/yii2-widget-datepicker for correct work an example
9+
use kartik\date\DatePicker;
10+
811
/* @var $this \yii\base\View */
912
/* @var $model ExampleModel */
1013
?>
@@ -18,7 +21,8 @@
1821
]);?>
1922

2023
<h3>Single column</h3>
21-
<?= $form->field($model, 'emails')->widget(MultipleInput::className(), [
24+
<?php
25+
echo $form->field($model, 'emails')->widget(MultipleInput::className(), [
2226
'limit' => 5
2327
])
2428
->label(false);
@@ -40,18 +44,20 @@
4044
],
4145
[
4246
'name' => 'day',
43-
'type' => 'dropDownList',
47+
'type' => DatePicker::className(),
4448
'title' => 'Day',
4549
'value' => function($data) {
4650
return $data['day'];
4751
},
48-
'defaultValue' => 1,
4952
'items' => [
5053
'0' => 'Saturday',
5154
'1' => 'Monday'
5255
],
5356
'options' => [
54-
57+
'pluginOptions' => [
58+
'format' => 'dd.mm.yyyy',
59+
'todayHighlight' => true
60+
]
5561
]
5662
],
5763
[

src/MultipleInput.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Yii;
1212
use yii\base\InvalidConfigException;
1313
use yii\base\Model;
14+
use yii\web\View;
1415
use yii\widgets\InputWidget;
1516
use yii\helpers\Json;
1617
use yii\db\ActiveRecord;
@@ -49,6 +50,11 @@ class MultipleInput extends InputWidget
4950
*/
5051
protected $template;
5152

53+
/**
54+
* @var array js templates collected from js which has been registered during rendering of widgets
55+
*/
56+
protected $jsTemplates = [];
57+
5258
/**
5359
* @var string
5460
*/
@@ -219,10 +225,26 @@ private function getRowTemplate()
219225
$this->template .= Html::tag('tr', implode("\n", $cells), [
220226
'class' => 'multiple-input-list__item',
221227
]);
228+
229+
if (is_array($this->getView()->js) && array_key_exists(View::POS_READY, $this->getView()->js)) {
230+
$this->collectJsTemplates();
231+
}
222232
}
233+
223234
return $this->template;
224235
}
225236

237+
private function collectJsTemplates()
238+
{
239+
$this->jsTemplates = [];
240+
foreach ($this->getView()->js[View::POS_READY] as $key => $js) {
241+
if (preg_match('/\(.#[^)]+{index}[^)]+\)/', $js) === 1) {
242+
$this->jsTemplates[] = $js;
243+
unset($this->getView()->js[View::POS_READY][$key]);
244+
}
245+
}
246+
}
247+
226248
/**
227249
* Renders the action column.
228250
*
@@ -268,7 +290,12 @@ private function renderRow($index, $data = null)
268290
$replace[] = $column->prepareValue($data);
269291
}
270292

271-
return str_replace($search, $replace, $this->getRowTemplate());
293+
$row = str_replace($search, $replace, $this->getRowTemplate());
294+
295+
foreach ($this->jsTemplates as $js) {
296+
$this->getView()->registerJs(strtr($js, ['{index}' => $index]), View::POS_READY);
297+
}
298+
return $row;
272299
}
273300

274301
/**
@@ -339,6 +366,7 @@ public function registerClientScript()
339366
[
340367
'id' => $this->getId(),
341368
'template' => $this->getRowTemplate(),
369+
'jsTemplates' => $this->jsTemplates,
342370
'btn_action' => self::ACTION_REMOVE,
343371
'btn_type' => 'btn-danger',
344372
'limit' => $this->limit,

src/MultipleInputColumn.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function init()
8181
$this->type = self::TYPE_TEXT_INPUT;
8282
}
8383

84-
if (!empty($this->options)) {
84+
if (empty($this->options)) {
8585
$this->options = [];
8686
}
8787
}
@@ -163,6 +163,11 @@ public function renderCellContent($value)
163163
default:
164164
if (method_exists('yii\helpers\Html', $type)) {
165165
$input = Html::$type($name, $value, $options);
166+
} elseif (class_exists($type) && method_exists($type, 'widget')) {
167+
$input = $type::widget(array_merge($options, [
168+
'name' => $name,
169+
'value' => $value,
170+
]));
166171
} else {
167172
throw new InvalidConfigException("Invalid column type '$type'");
168173
}

src/assets/src/js/jquery.multipleInput.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
var defaultOptions = {
1414
id: null,
1515
template: null,
16+
jsTemplates: [],
1617
btn_action: null,
1718
btn_type: null,
1819
limit: 1,
@@ -92,6 +93,11 @@
9293
methods.addAttribute.apply(this);
9394
});
9495

96+
var jsTemplate;
97+
for (i in settings.jsTemplates) {
98+
jsTemplate = settings.jsTemplates[i].replaceAll('{index}', data.currentIndex);
99+
window.eval(jsTemplate);
100+
}
95101
wrapper.data('multipleInput').currentIndex++;
96102
},
97103

0 commit comments

Comments
 (0)