Skip to content

Commit 8a42129

Browse files
author
Eugene Tupikov
committed
Fixes
1 parent be714e6 commit 8a42129

File tree

2 files changed

+51
-67
lines changed

2 files changed

+51
-67
lines changed

MultipleInput.php

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,26 @@ class MultipleInput extends InputWidget
2828
{
2929
const ACTION_ADD = 'plus';
3030
const ACTION_REMOVE = 'remove';
31-
3231
/**
3332
* @var ActiveRecord[]|array[] input data
3433
*/
3534
public $data = null;
36-
3735
/**
3836
* @var array columns configuration
3937
*/
4038
public $columns = [];
41-
4239
/**
4340
* @var integer inputs limit
4441
*/
4542
public $limit;
46-
4743
/**
4844
* @var string generated template, internal variable.
4945
*/
5046
protected $template;
51-
5247
/**
5348
* @var string
5449
*/
5550
protected $replacementKeys;
56-
5751
public function init()
5852
{
5953
parent::init();
@@ -64,14 +58,15 @@ public function init()
6458
}
6559
}
6660
}
67-
68-
6961
/**
7062
* Run widget.
7163
*/
7264
public function run()
7365
{
74-
echo Html::beginTag('div', ['id' => $this->getId(), 'class' => 'list-group']);
66+
echo Html::beginTag('div', [
67+
'id' => $this->getId(),
68+
'class' => 'multiple-input'
69+
]);
7570
echo Html::beginTag('table', [
7671
'class' => 'multiple-input-list table table-condensed'
7772
]);
@@ -89,10 +84,8 @@ public function run()
8984
echo Html::endTag('tbody');
9085
echo Html::endTag('table');
9186
echo Html::endTag('div');
92-
9387
$this->registerClientScript();
9488
}
95-
9689
/**
9790
* Render header.
9891
*
@@ -124,7 +117,6 @@ private function renderHeader()
124117
echo Html::endTag('tr');
125118
echo Html::endTag('thead');
126119
}
127-
128120
/**
129121
* Check that at least one column has a header.
130122
*
@@ -139,7 +131,6 @@ private function hasHeader()
139131
}
140132
return false;
141133
}
142-
143134
private function getRowTemplate()
144135
{
145136
if (empty($this->template)) {
@@ -150,16 +141,12 @@ private function getRowTemplate()
150141
foreach ($this->getColumns() as $columnIndex => $column) {
151142
$field = $column['name'];
152143
$name = $this->getElementName($field);
153-
154144
$value = $field . '_value';
155145
$this->replacementKeys[$value] = ArrayHelper::getValue($column, 'defaultValue', '');
156146
$value = '{' . $value . '}';
157-
158147
$options = ArrayHelper::getValue($column, 'options', []);
159148
$options['id'] = $this->getElementId($field);
160-
161149
$type = ArrayHelper::getValue($column, 'type', 'textInput');
162-
163150
if ($type == 'hiddenInput') {
164151
$hiddenFields .= Html::hiddenInput($name, $value, $options);
165152
} else {
@@ -207,9 +194,10 @@ private function getRowTemplate()
207194
'tagName' => 'div',
208195
'encodeLabel' => false,
209196
'label' => Html::tag('i', null, ['class' => 'glyphicon glyphicon-{btn_action}']),
197+
'type' => '{btn_type}',
210198
'options' => [
211199
'id' => $this->getElementId('button'),
212-
'class' => "{btn_type} multiple-input-list__btn btn js-{$this->getId()}-input-{btn_action}",
200+
'class' => "multiple-input-list__btn btn js-input-{btn_action}",
213201
]
214202
]
215203
);
@@ -219,7 +207,6 @@ private function getRowTemplate()
219207
}
220208
return $this->template;
221209
}
222-
223210
/**
224211
* Render row.
225212
*
@@ -230,7 +217,6 @@ private function renderRow($index, $data = null)
230217
{
231218
$btnAction = $index == 0 ? self::ACTION_ADD : self::ACTION_REMOVE;
232219
$btnType = $index == 0 ? 'btn-default' : 'btn-danger';
233-
234220
$search = ['{index}', '{btn_action}', '{btn_type}'];
235221
$replace = [$index, $btnAction, $btnType];
236222
foreach ($this->getColumns() as $column) {
@@ -239,7 +225,6 @@ private function renderRow($index, $data = null)
239225
}
240226
echo str_replace($search, $replace, $this->getRowTemplate());
241227
}
242-
243228
/**
244229
* @param $column
245230
* @param $data
@@ -263,7 +248,6 @@ private function prepareColumnValue($column, $data)
263248
}
264249
return $value;
265250
}
266-
267251
/**
268252
* @param $name
269253
* @param string $index
@@ -280,7 +264,6 @@ private function getElementName($name, $index = null)
280264
: '[' . $name . '][' . $index . ']';
281265
return $elementName;
282266
}
283-
284267
/**
285268
* @param $name
286269
* @return mixed
@@ -289,16 +272,14 @@ private function getElementId($name)
289272
{
290273
return $this->normalize($this->getElementName($name));
291274
}
292-
293275
/**
294276
* @param $name
295277
* @return mixed
296278
*/
297279
private function normalize($name) {
298280
return str_replace(['[]', '][', '[', ']', ' ', '.'], ['', '-', '-', '', '-', '-'], strtolower($name));
299281
}
300-
301-
/**
282+
/**
302283
* @return string
303284
*/
304285
private function getName()
@@ -308,7 +289,6 @@ private function getName()
308289
}
309290
return $this->name;
310291
}
311-
312292
/**
313293
* @return array
314294
*/
@@ -321,7 +301,6 @@ private function getColumns()
321301
}
322302
return $this->columns;
323303
}
324-
325304
/**
326305
* Регистрирует клиентский скрипт и опции.
327306
*/
@@ -343,4 +322,4 @@ public function registerClientScript()
343322
$js = "jQuery('#$id').multipleInput($options);";
344323
$this->getView()->registerJs($js);
345324
}
346-
}
325+
}

assets/src/js/jquery.multipleInput.js

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,61 +16,64 @@
1616
btn_action: null,
1717
btn_type: null,
1818
limit: 1,
19-
replacement: [],
20-
currentIndex: 0
19+
replacement: []
2120
};
2221

23-
var attributeDefaults = {};
24-
25-
var currentIndex = 0;
26-
27-
2822
var methods = {
2923
init: function (options) {
30-
var settings = $.extend(defaultOptions, options || {});
24+
var settings = $.extend({}, defaultOptions, options || {}),
25+
wrapper = $('#' + settings.id),
26+
form = wrapper.closest('form'),
27+
id = this.selector.replace('#', '');
28+
29+
wrapper.data('multipleInput', {
30+
settings: settings,
31+
currentIndex: 0,
32+
attributeDefaults: {}
33+
});
3134

32-
$(document).on('click.multipleinput', '.js-' + settings.id + '-input-remove', function (e) {
35+
$(document).on('click.multipleInput', '#' + settings.id + ' .js-input-remove', function (e) {
3336
e.preventDefault();
3437
methods.removeInput.apply(this);
3538
});
3639

37-
$(document).on('click.multipleinput', '.js-'+ settings.id + '-input-plus', function (e) {
40+
$(document).on('click.multipleInput', '#' + settings.id + ' .js-input-plus', function (e) {
3841
e.preventDefault();
39-
methods.addInput.apply(this,[settings]);
42+
methods.addInput.apply(this);
4043
});
4144

42-
var wrapper = $('#' + settings.id),
43-
form = wrapper.closest('form');
44-
45-
setTimeout(function() {
46-
var attributes = form.data('yiiActiveForm').attributes;
47-
$.each(attributes[0], function(key, value) {
48-
if (['id', 'input', 'container'].indexOf(key) == -1) {
49-
attributeDefaults[key] = value;
50-
}
51-
});
52-
form.data('yiiActiveForm').attributes = [];
53-
54-
wrapper.find('.multiple-input-list').find('input, select, textarea').each(function () {
55-
methods.addAttribute.apply(this);
56-
});
57-
58-
currentIndex = $('#' + settings.id).find('.multiple-input-list__item').length;
45+
var intervalID = setInterval(function(){
46+
if (typeof form.data('yiiActiveForm') === 'object') {
47+
$.each(form.yiiActiveForm('find', id), function (key, value) {
48+
if (['id', 'input', 'container'].indexOf(key) == -1) {
49+
wrapper.data('multipleInput').attributeDefaults[key] = value;
50+
}
51+
});
52+
form.yiiActiveForm('remove', id);
53+
wrapper.find('.multiple-input-list').find('input, select, textarea').each(function () {
54+
methods.addAttribute.apply(this);
55+
});
56+
wrapper.data('multipleInput').currentIndex = wrapper.find('.multiple-input-list__item').length;
57+
clearInterval(intervalID);
58+
}
5959
}, 100);
60+
6061
},
6162

62-
addInput: function (settings) {
63-
var template = settings.template,
64-
parent = $('#' + settings.id),
65-
inputList = parent.find('.multiple-input-list').first(),
66-
count = parent.find('.multiple-input-list__item').length,
63+
addInput: function () {
64+
var wrapper = $(this).closest('.multiple-input').first(),
65+
data = wrapper.data('multipleInput'),
66+
settings = data.settings,
67+
template = settings.template,
68+
inputList = wrapper.find('.multiple-input-list').first(),
69+
count = wrapper.find('.multiple-input-list__item').length,
6770
replacement = settings.replacement || [];
6871

6972
if (settings.limit != null && count >= settings.limit) {
7073
return;
7174
}
7275
var search = ['{index}', '{btn_action}', '{btn_type}', '{value}'],
73-
replace = [currentIndex, settings.btn_action, settings.btn_type, ''];
76+
replace = [data.currentIndex, settings.btn_action, settings.btn_type, ''];
7477

7578
for (var i in search) {
7679
template = template.replaceAll(search[i], replace[i]);
@@ -85,7 +88,7 @@
8588
methods.addAttribute.apply(this);
8689
});
8790

88-
currentIndex++;
91+
wrapper.data('multipleInput').currentIndex++;
8992
},
9093

9194
removeInput: function () {
@@ -100,9 +103,11 @@
100103

101104
addAttribute: function () {
102105
var id = $(this).attr('id'),
103-
form = $('#' + $(this).attr('id')).closest('form');
106+
ele = $('#' + $(this).attr('id')),
107+
wrapper = ele.closest('.multiple-input').first(),
108+
form = ele.closest('form');
104109

105-
form.yiiActiveForm('add', $.extend(attributeDefaults, {
110+
form.yiiActiveForm('add', $.extend(wrapper.data('multipleInput').attributeDefaults, {
106111
'id': id,
107112
'input': '#' + id,
108113
'container': '.field-' + id
@@ -120,4 +125,4 @@
120125
String.prototype.replaceAll = function(search, replace){
121126
return this.split(search).join(replace);
122127
};
123-
})(window.jQuery);
128+
})(window.jQuery);

0 commit comments

Comments
 (0)