Skip to content

Commit b0a35b0

Browse files
author
Eugene Tupikov
committed
Improvements
1 parent ae7bcd8 commit b0a35b0

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

MultipleInput.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,12 @@ public function registerClientScript()
330330
MultipleInputAsset::register($view);
331331
$options = Json::encode(
332332
[
333-
'id' => $this->getId(),
334-
'template' => $this->getRowTemplate(),
335-
'btn_action' => self::ACTION_REMOVE,
336-
'btn_type' => Button::TYPE_DANGER,
337-
'limit' => $this->limit,
338-
'replacement' => $this->replacementKeys,
333+
'id' => $this->getId(),
334+
'template' => $this->getRowTemplate(),
335+
'btn_action' => self::ACTION_REMOVE,
336+
'btn_type' => Button::TYPE_DANGER,
337+
'limit' => $this->limit,
338+
'replacement' => $this->replacementKeys,
339339
]
340340
);
341341
$id = $this->options['id'];

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1-
# yii2-multiple-field
1+
Yii2 Multiple input widget.
2+
==================
3+
Yii2 widget for handle multiple inputs for an attribute of model
24

5+
Installation
6+
------------
37

4-
In progress
8+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
9+
10+
Either run
11+
12+
```
13+
php composer.phar require unclead/yii2-multiple-input "*"
14+
```
15+
16+
or add
17+
18+
```
19+
"unclead/yii2-multiple-input": "*"
20+
```
21+
22+
to the require section of your `composer.json` file.
23+
24+
Usage:
25+
------
26+
Look at examples for getting more details
27+
28+
Full documentation in progress.

assets/src/js/jquery.multipleInput.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
btn_action: null,
1717
btn_type: null,
1818
limit: 1,
19-
replacement: []
19+
replacement: [],
20+
currentIndex: 0
2021
};
2122

2223
var attributeDefaults = {};
2324

25+
var currentIndex = 0;
26+
27+
2428
var methods = {
2529
init: function (options) {
2630
var settings = $.extend(defaultOptions, options || {});
@@ -41,29 +45,32 @@
4145
setTimeout(function() {
4246
var attributes = form.data('yiiActiveForm').attributes;
4347
$.each(attributes[0], function(key, value) {
44-
attributeDefaults[key] = value;
48+
if (['id', 'input', 'container'].indexOf(key) == -1) {
49+
attributeDefaults[key] = value;
50+
}
4551
});
4652
form.data('yiiActiveForm').attributes = [];
4753

4854
wrapper.find('.multiple-input-list').find('input, select, textarea').each(function () {
4955
methods.addAttribute.apply(this);
5056
});
57+
58+
currentIndex = $('#' + settings.id).find('.multiple-input-list__item').length;
5159
}, 100);
5260
},
5361

5462
addInput: function (settings) {
5563
var template = settings.template,
56-
$wrapper = $(this).parents('.multiple-input-list').first(),
57-
index = $wrapper.find('.multiple-input-list__item').length,
58-
btn_action = settings.btn_action,
59-
btn_type = settings.btn_type,
64+
parent = $('#' + settings.id),
65+
inputList = parent.find('.multiple-input-list').first(),
66+
count = parent.find('.multiple-input-list__item').length,
6067
replacement = settings.replacement || [];
6168

62-
if (settings.limit != null && index >= settings.limit) {
69+
if (settings.limit != null && count >= settings.limit) {
6370
return;
6471
}
6572
var search = ['{index}', '{btn_action}', '{btn_type}', '{value}'],
66-
replace = [index, btn_action, btn_type, ''];
73+
replace = [currentIndex, settings.btn_action, settings.btn_type, ''];
6774

6875
for (var i in search) {
6976
template = template.replaceAll(search[i], replace[i]);
@@ -73,11 +80,12 @@
7380
template = template.replaceAll('{' + j + '}', replacement[j]);
7481
}
7582

76-
77-
$(template).hide().appendTo($wrapper).fadeIn(300);
83+
$(template).hide().appendTo(inputList).fadeIn(300);
7884
$(template).find('input, select, textarea').each(function () {
7985
methods.addAttribute.apply(this);
8086
});
87+
88+
currentIndex++;
8189
},
8290

8391
removeInput: function () {

0 commit comments

Comments
 (0)