Skip to content

Commit f351bbc

Browse files
author
Eugene Tupikov
committed
Added min option and prepare to release
1 parent 1192fa9 commit f351bbc

File tree

10 files changed

+217
-64
lines changed

10 files changed

+217
-64
lines changed

CHANGELOG.md

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

4-
1.2.6 in development
4+
1.2.7 in development
55
--------------------
66

7+
1.2.6
8+
-----
9+
710
- Bug #49: urlencoded field token replacement in js template (rolmonk)
11+
- Enh #48: Added option `min` for setting minimum number of rows
12+
- Enh: added option 'addButtonPosition'
813

914
1.2.5
1015
-----

README.md

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Yii2 widget for handle multiple inputs for an attribute of model
88
[![License](https://poser.pugx.org/unclead/yii2-multiple-input/license)](https://packagist.org/packages/unclead/yii2-multiple-input)
99

1010
##Latest release
11-
The latest version of the extension is v1.2.5. Follow the [instruction](./UPGRADE.md) for upgrading from previous versions
11+
The latest version of the extension is v1.2.6. Follow the [instruction](./UPGRADE.md) for upgrading from previous versions
1212

1313
Contents:
1414

@@ -38,28 +38,61 @@ to the require section of your `composer.json` file.
3838

3939
## Configuration
4040

41-
Widget support the following options that are additionally recognized over and above the configuration options in the InputWidget:
41+
Widget support the following options that are additionally recognized over and above the configuration options in the InputWidget.
4242

43-
- `limit` *integer*: rows limit. If not set will defaul to unlimited
44-
- `attributeOptions` *array*: client-side attribute options, e.g. enableAjaxValidation. You may use this property in case when
43+
### Base options
44+
45+
**limit** *integer*: rows limit. If not set will defaul to unlimited
46+
47+
**min** *integer*: minimum number of rows. Set to `0` if you need the empty list in case you don't have any data
48+
49+
**attributeOptions** *array*: client-side attribute options, e.g. enableAjaxValidation. You may use this property in case when
4550
you use widget without a model, since in this case widget is not able to detect client-side options automatically
46-
- `addButtonOptions` *array*: the HTML options for `add` button. Can contains `class` and `label` keys
47-
- `removeButtonOptions` *array*: the HTML options for `add` button. Can contains `class` and `label` keys
48-
- `data` *array*: array of values in case you use widget without model
49-
- `models` *array*: the list of models. Required in case you use `TabularInput` widget
50-
- `allowEmptyList` *boolean*: whether to allow the empty list
51-
- `columns` *array*: the row columns configuration where you can set the following properties:
52-
- `name` *string*: input name. *Required options*
53-
- `type` *string*: type of the input. If not set will default to `textInput`. Read more about the types described below
54-
- `title` *string*: the column title
55-
- `value` *Closure*: you can set it to an anonymous function with the following signature: ```function($data) {}```
56-
- `defaultValue` *string*: default value of input,
57-
- `items` *array*|*Closure*: the items for input with type dropDownList, listBox, checkboxList, radioList or anonymous function
58-
which return array of items and has the following signature: ```function($data) {}```
59-
- `options` *array*: the HTML attributes for the input
60-
- `headerOptions` *array*: the HTML attributes for the header cell
61-
- `enableError` *boolean*: whether to render inline error for the input. Default to `false`
62-
- `errorOptions` *array*: the HTMl attributes for the error tag
51+
52+
**addButtonPosition** *integer*: the position of `add` button. This can be MultipleInput::POS_HEADER or MultipleInput::POS_ROW.
53+
54+
**addButtonOptions** *array*: the HTML options for `add` button. Can contains `class` and `label` keys
55+
56+
**removeButtonOptions** *array*: the HTML options for `add` button. Can contains `class` and `label` keys
57+
58+
**data** *array*: array of values in case you use widget without model
59+
60+
**models** *array*: the list of models. Required in case you use `TabularInput` widget
61+
62+
**allowEmptyList** *boolean*: whether to allow the empty list
63+
64+
**columns** *array*: the row columns configuration where you can set the properties which is described below
65+
66+
### Column options
67+
68+
**name** *string*: input name. *Required options*
69+
70+
**type** *string*: type of the input. If not set will default to `textInput`. Read more about the types described below
71+
72+
**title** *string*: the column title
73+
74+
**value** *Closure*: you can set it to an anonymous function with the following signature:
75+
76+
```php
77+
function($data) {}
78+
```
79+
80+
**defaultValue** *string*: default value of input
81+
82+
**items** *array*|*Closure*: the items for input with type dropDownList, listBox, checkboxList, radioList
83+
or anonymous function which return array of items and has the following signature:
84+
85+
```php
86+
function($data) {}
87+
```
88+
89+
**options** *array*: the HTML attributes for the input
90+
91+
**headerOptions** *array*: the HTML attributes for the header cell
92+
93+
**enableError** *boolean*: whether to render inline error for the input. Default to `false`
94+
95+
**errorOptions** *array*: the HTMl attributes for the error tag
6396

6497
### Input types
6598

@@ -101,8 +134,13 @@ use unclead\widgets\MultipleInput;
101134

102135
...
103136

104-
<?= $form->field($model, 'emails')->widget(MultipleInput::className(), [
105-
'limit' => 5
137+
<?php
138+
echo $form->field($model, 'emails')->widget(MultipleInput::className(), [
139+
'limit' => 6,
140+
'allowEmptyList' => false,
141+
'enableGuessTitle' => true,
142+
'min' => 2, // should be at least 2 rows
143+
'addButtonPosition' => MultipleInput::POS_HEADER // show add button in the header
106144
])
107145
->label(false);
108146
?>
@@ -280,6 +318,8 @@ In some cases you need to have the ability to delete all rows in the list. For t
280318

281319
```
282320

321+
Also you can set `0` in `min` option if you don't need first blank row when data is empty.
322+
283323
### Guess column title
284324

285325
Sometimes you can use the widget without defining columns but you want to have the column header of the table.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"yii2 tabular input"
1010
],
1111
"type": "yii2-extension",
12-
"version": "1.2.5",
12+
"version": "1.2.6",
1313
"license": "BSD-3-Clause",
1414
"support": {
1515
"issues": "https://github.com/unclead/yii2-multiple-input/issues?state=open",

examples/views/multiple-input.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
<h3>Single column</h3>
2525
<?php
2626
echo $form->field($model, 'emails')->widget(MultipleInput::className(), [
27-
'limit' => 5,
28-
'allowEmptyList' => true,
29-
'enableGuessTitle' => true
27+
'limit' => 6,
28+
'allowEmptyList' => false,
29+
'enableGuessTitle' => true,
30+
'min' => 2, // should be at least 2 rows
31+
'addButtonPosition' => MultipleInput::POS_HEADER // show add button in the header
3032
])
3133
->label(false);
3234
?>

src/MultipleInput.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
class MultipleInput extends InputWidget
2424
{
25+
const POS_HEADER = 0;
26+
const POS_ROW = 1;
27+
2528
/**
2629
* @var ActiveRecord[]|array[] input data
2730
*/
@@ -64,6 +67,16 @@ class MultipleInput extends InputWidget
6467
*/
6568
public $enableGuessTitle = false;
6669

70+
/**
71+
* @var int minimum number of rows
72+
*/
73+
public $min;
74+
75+
/**
76+
* @var string position of add button. By default button is rendered in the row.
77+
*/
78+
public $addButtonPosition = self::POS_ROW;
79+
6780

6881
/**
6982
* Initialization.
@@ -129,6 +142,8 @@ private function createRenderer()
129142
'data' => $this->data,
130143
'columnClass' => MultipleInputColumn::className(),
131144
'allowEmptyList' => $this->allowEmptyList,
145+
'min' => $this->min,
146+
'addButtonPosition' => $this->addButtonPosition,
132147
'context' => $this
133148
];
134149

src/TabularInput.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
*/
2222
class TabularInput extends Widget
2323
{
24+
const POS_HEADER = 0;
25+
const POS_ROW = 1;
26+
2427
/**
2528
* @var array
2629
*/
@@ -31,6 +34,11 @@ class TabularInput extends Widget
3134
*/
3235
public $limit;
3336

37+
/**
38+
* @var int minimum number of rows
39+
*/
40+
public $min;
41+
3442
/**
3543
* @var array client-side attribute options, e.g. enableAjaxValidation. You may use this property in case when
3644
* you use widget without a model, since in this case widget is not able to detect client-side options
@@ -58,6 +66,11 @@ class TabularInput extends Widget
5866
*/
5967
public $models;
6068

69+
/**
70+
* @var string position of add button. By default button is rendered in the row.
71+
*/
72+
public $addButtonPosition = self::POS_ROW;
73+
6174

6275
/**
6376
* Initialization.
@@ -100,6 +113,8 @@ private function createRenderer()
100113
'data' => $this->models,
101114
'columnClass' => TabularColumn::className(),
102115
'allowEmptyList' => $this->allowEmptyList,
116+
'min' => $this->min,
117+
'addButtonPosition' => $this->addButtonPosition,
103118
'context' => $this
104119
];
105120

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@
5454
// string that collect js templates of widgets which uses in the columns
5555
jsTemplates: [],
5656
// how many row has to renders
57-
limit: 1
57+
limit: 1,
58+
// minimum number of rows
59+
min: 1
5860
};
5961

6062
var defaultAttributeOptions = {
@@ -142,7 +144,9 @@
142144

143145
var jsTemplate;
144146
for (i in settings.jsTemplates) {
145-
jsTemplate = settings.jsTemplates[i].replaceAll('{multiple_index}', data.currentIndex).replaceAll('%7Bmultiple_index%7D', data.currentIndex);
147+
jsTemplate = settings.jsTemplates[i]
148+
.replaceAll('{multiple_index}', data.currentIndex)
149+
.replaceAll('%7Bmultiple_index%7D', data.currentIndex);
146150
window.eval(jsTemplate);
147151
}
148152
$wrapper.data('multipleInput').currentIndex++;
@@ -153,23 +157,28 @@
153157

154158
removeInput: function () {
155159
var $wrapper = $(this).closest('.multiple-input').first(),
156-
$toDelete = $(this).closest('.multiple-input-list__item');
160+
$toDelete = $(this).closest('.multiple-input-list__item'),
161+
count = $('.multiple-input-list__item').length,
162+
data = $wrapper.data('multipleInput'),
163+
settings = data.settings;
157164

158-
var event = $.Event(events.beforeDeleteRow);
159-
$wrapper.trigger(event, [$toDelete]);
160-
if (event.result === false) {
161-
return;
162-
}
165+
if (count > settings.min) {
166+
var event = $.Event(events.beforeDeleteRow);
167+
$wrapper.trigger(event, [$toDelete]);
168+
if (event.result === false) {
169+
return;
170+
}
163171

164-
$toDelete.find('input, select, textarea').each(function () {
165-
methods.removeAttribute.apply(this);
166-
});
167-
$toDelete.fadeOut(300, function () {
168-
$(this).remove();
169-
});
172+
$toDelete.find('input, select, textarea').each(function () {
173+
methods.removeAttribute.apply(this);
174+
});
175+
$toDelete.fadeOut(300, function () {
176+
$(this).remove();
177+
});
170178

171-
event = $.Event(events.afterDeleteRow);
172-
$wrapper.trigger(event);
179+
event = $.Event(events.afterDeleteRow);
180+
$wrapper.trigger(event);
181+
}
173182
},
174183

175184
addAttribute: function () {

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.

0 commit comments

Comments
 (0)