Skip to content

Commit e4e94df

Browse files
committed
Fixed an incorrect behavior of widget in case of ajax loading (e.g. in modal window)
1 parent 5c51552 commit e4e94df

File tree

6 files changed

+102
-66
lines changed

6 files changed

+102
-66
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Yii2 multiple input change log
22
==============================
33

4+
2.7.0
5+
=====
6+
7+
- Fixed an incorrect behavior of widget in case of ajax loading (e.g. in modal window)
8+
49
2.6.1
510
=====
611

examples/views/multiple-input.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use unclead\multipleinput\examples\models\ExampleModel;
66
use yii\helpers\Html;
77
use unclead\multipleinput\MultipleInputColumn;
8+
use yii\widgets\MaskedInput;
89

910
// Note: You have to install https://github.com/kartik-v/yii2-widget-datepicker for correct work an example
1011
use kartik\date\DatePicker;
@@ -23,25 +24,25 @@
2324

2425
<h3>Single column</h3>
2526
<?php
26-
echo $form->field($model, 'emails')->widget(MultipleInput::className(), [
27-
'max' => 6,
28-
'allowEmptyList' => false,
29-
'columns' => [
30-
[
31-
'name' => 'emails',
32-
'options' => [
33-
'placeholder' => 'E-mail'
34-
]
35-
]
36-
],
37-
'min' => 2, // should be at least 2 rows
38-
'addButtonPosition' => [
39-
MultipleInput::POS_HEADER,
40-
MultipleInput::POS_FOOTER,
41-
MultipleInput::POS_ROW
42-
]
43-
])
44-
->label(false);
27+
// echo $form->field($model, 'emails')->widget(MultipleInput::className(), [
28+
// 'max' => 6,
29+
// 'allowEmptyList' => false,
30+
// 'columns' => [
31+
// [
32+
// 'name' => 'emails',
33+
// 'options' => [
34+
// 'placeholder' => 'E-mail'
35+
// ]
36+
// ]
37+
// ],
38+
// 'min' => 2, // should be at least 2 rows
39+
// 'addButtonPosition' => [
40+
// MultipleInput::POS_HEADER,
41+
// MultipleInput::POS_FOOTER,
42+
// MultipleInput::POS_ROW
43+
// ]
44+
// ])
45+
// ->label(false);
4546
?>
4647

4748
<h3>Multiple columns</h3>
@@ -88,7 +89,7 @@
8889
],
8990
[
9091
'name' => 'day',
91-
'type' => DatePicker::className(),
92+
'type' => MaskedInput::className(),
9293
'title' => 'Day',
9394
'value' => function($data) {
9495
return $data['day'];
@@ -98,11 +99,15 @@
9899
'1' => 'Monday'
99100
],
100101
'options' => [
101-
'pluginOptions' => [
102-
'format' => 'dd.mm.yyyy',
103-
'todayHighlight' => true
104-
]
102+
'mask' => '99.99.9999'
103+
105104
],
105+
// 'options' => [
106+
// 'pluginOptions' => [
107+
// 'format' => 'dd.mm.yyyy',
108+
// 'todayHighlight' => true
109+
// ]
110+
// ],
106111
'headerOptions' => [
107112
'style' => 'width: 250px;',
108113
'class' => 'day-css-class'

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@
6161
*/
6262
template: null,
6363
/**
64-
* string that collect js templates of widgets which uses in the columns
64+
* array that collect js templates of widgets which uses in the columns
6565
*/
6666
jsTemplates: [],
67+
/**
68+
* array of scripts which need to execute before initialization
69+
*/
70+
jsInit: [],
6771
/**
6872
* how many row are allowed to render
6973
*/
@@ -96,12 +100,17 @@
96100
form = $wrapper.closest('form'),
97101
inputId = settings.inputId;
98102

103+
for (i in settings.jsInit) {
104+
var script = document.createElement("script");
105+
script.innerHTML = settings.jsInit[i];
106+
document.body.appendChild(script);
107+
}
108+
99109
$wrapper.data('multipleInput', {
100110
settings: settings,
101111
currentIndex: 0
102112
});
103113

104-
105114
$wrapper.on('click.multipleInput', '.js-input-remove', function (e) {
106115
e.stopPropagation();
107116
removeInput($(this));

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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/BaseColumn.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,11 @@ protected function renderCheckboxList($name, $value, $options)
439439
}
440440

441441
/**
442-
* @param $name
443-
* @param $value
444-
* @param $options
442+
* Renders a text.
443+
*
444+
* @param string $name the name of input
445+
* @param mixed $value the value of input
446+
* @param array $options the HTMl options of input
445447
* @return string
446448
*/
447449
protected function renderStatic($name, $value, $options)
@@ -450,22 +452,24 @@ protected function renderStatic($name, $value, $options)
450452
}
451453

452454
/**
453-
* @param $name
454-
* @param $value
455-
* @param $options
455+
* Renders a drag&drop column.
456+
*
457+
* @param string $name the name of input
458+
* @param mixed $value the value of input
459+
* @param array $options the HTMl options of input
456460
* @return string
457461
*/
458-
protected function renderDragColumn($name, $options)
462+
protected function renderDragColumn($name, $value, $options)
459463
{
460-
return Html::tag('span', $value, ['class' => 'glyphicon glyphicon-menu-hamburger drag-handle']);
464+
return Html::tag('span', null, ['class' => 'glyphicon glyphicon-menu-hamburger drag-handle']);
461465
}
462466

463467
/**
464468
* Renders an input.
465469
*
466-
* @param $name
467-
* @param $value
468-
* @param $options
470+
* @param string $name the name of input
471+
* @param mixed $value the value of input
472+
* @param array $options the HTMl options of input
469473
* @return string
470474
* @throws InvalidConfigException
471475
*/
@@ -488,10 +492,10 @@ protected function renderDefault($name, $value, $options)
488492
/**
489493
* Renders a widget.
490494
*
491-
* @param $type
492-
* @param $name
493-
* @param $value
494-
* @param $options
495+
* @param string $type
496+
* @param string $name the name of input
497+
* @param mixed $value the value of input
498+
* @param array $options the HTMl options of input
495499
* @return mixed
496500
*/
497501
protected function renderWidget($type, $name, $value, $options)

src/renderers/BaseRenderer.php

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -250,42 +250,46 @@ protected function initColumns()
250250
public function render()
251251
{
252252
$this->initColumns();
253-
$content = $this->internalRender();
254-
$this->registerAssets();
255-
return $content;
256-
}
257-
258-
/**
259-
* @return mixed
260-
* @throws NotSupportedException
261-
*/
262-
abstract protected function internalRender();
263253

264-
/**
265-
* Register script.
266-
*
267-
* @throws \yii\base\InvalidParamException
268-
*/
269-
protected function registerAssets()
270-
{
271254
$view = $this->context->getView();
272255
MultipleInputAsset::register($view);
273256

274-
$view = $this->context->getView();
275-
257+
// Collect all js scripts which were added before rendering of our widget
276258
$jsBefore= [];
277-
if (is_array($view->js) && array_key_exists(View::POS_READY, $view->js)) {
278-
foreach ($view->js[View::POS_READY] as $key => $js) {
279-
$jsBefore[$key] = $js;
259+
if (is_array($view->js)) {
260+
foreach ($view->js as $position => $scripts) {
261+
foreach ($scripts as $key => $js) {
262+
if (!isset($jsBefore[$position])) {
263+
$jsBefore[$position] = [];
264+
}
265+
$jsBefore[$position][$key] = $js;
266+
}
267+
}
268+
}
269+
270+
$content = $this->internalRender();
271+
272+
// Collect all js scripts which has to be appended to page before initialization widget
273+
$jsInit = [];
274+
if (is_array($view->js)) {
275+
foreach ($view->js as $position => $scripts) {
276+
foreach ($scripts as $key => $js) {
277+
if (isset($jsBefore[$position][$key])) {
278+
continue;
279+
}
280+
$jsInit[$key] = $js;
281+
$jsBefore[$position][$key] = $js;
282+
unset($view->js[$position][$key]);
283+
}
280284
}
281285
}
282286

283287
$template = $this->prepareTemplate();
284288

285289
$jsTemplates = [];
286-
if (is_array($view->js) && array_key_exists(View::POS_READY, $view->js)) {
290+
if (is_array($view->js) && isset($view->js[View::POS_READY])) {
287291
foreach ($view->js[View::POS_READY] as $key => $js) {
288-
if (array_key_exists($key, $jsBefore)) {
292+
if (isset($jsBefore[View::POS_READY][$key])) {
289293
continue;
290294
}
291295

@@ -298,6 +302,7 @@ protected function registerAssets()
298302
'id' => $this->id,
299303
'inputId' => $this->context->options['id'],
300304
'template' => $template,
305+
'jsInit' => $jsInit,
301306
'jsTemplates' => $jsTemplates,
302307
'max' => $this->max,
303308
'min' => $this->min,
@@ -313,8 +318,16 @@ protected function registerAssets()
313318
}
314319

315320
$view->registerJs($js);
321+
322+
return $content;
316323
}
317324

325+
/**
326+
* @return mixed
327+
* @throws NotSupportedException
328+
*/
329+
abstract protected function internalRender();
330+
318331
/**
319332
* @return string
320333
*/

0 commit comments

Comments
 (0)