Skip to content

Commit c4587f9

Browse files
author
Eugene Tupikov
committed
Implemented ability to customize buttons
1 parent a0414fd commit c4587f9

File tree

7 files changed

+150
-36
lines changed

7 files changed

+150
-36
lines changed

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
Yii2 widget for handle multiple inputs for an attribute of model
33

44
[![Latest Stable Version](https://poser.pugx.org/unclead/yii2-multiple-input/v/stable)](https://packagist.org/packages/unclead/yii2-multiple-input)
5-
[![Total Downloads](https://poser.pugx.org/unclead/yii2-multiple-input/downloads)](https://packagist.org/packages/unclead/yii2-multiple-input)
5+
[![Total Downloads](https://poser.pugx.org/unclead/yii2-multiple-input/downloads)](https://packagist.org/packages/unclead/yii2-multiple-input)
6+
[![Daily Downloads](https://poser.pugx.org/unclead/yii2-multiple-input/d/daily)](https://packagist.org/packages/unclead/yii2-multiple-input)
67
[![Latest Unstable Version](https://poser.pugx.org/unclead/yii2-multiple-input/v/unstable)](https://packagist.org/packages/unclead/yii2-multiple-input)
78
[![License](https://poser.pugx.org/unclead/yii2-multiple-input/license)](https://packagist.org/packages/unclead/yii2-multiple-input)
89

@@ -14,6 +15,7 @@ Contents:
1415
- [Installation](#installation)
1516
- [Configuration](#configuration)
1617
- [Usage](#usage)
18+
- [How to customize buttons](#customize-buttons)
1719
- [Javascript Events](#javascript-events)
1820
- [Renderers](#renderers)
1921

@@ -38,10 +40,12 @@ to the require section of your `composer.json` file.
3840

3941
Widget support the following options that are additionally recognized over and above the configuration options in the InputWidget:
4042

41-
- `limit`: *integer*: rows limit. If not set will defaul to unlimited
42-
- `attributeOptions` *array*: client-side attribute options, e.g. enableAjaxValidation. You may use this property in case when
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
4345
you use widget without a model, since in this case widget is not able to detect client-side options automatically
44-
- `date` *array*: array of values in case you use widget without model
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
4549
- `models` *array*: the list of models. Required in case you use `TabularInput` widget
4650
- `columns` *array*: the row columns configuration where you can set the following properties:
4751
- `name` *string*: input name. *Required options*
@@ -236,6 +240,27 @@ You can find more detail about this use case [here](docs/tabular_input.md)
236240

237241
> Also you can find source code of examples [here](./docs/examples/)
238242
243+
## How to customize buttons
244+
245+
You can customize `add` and `remove` buttons via `addButtonOptions` and `removeButtonOptions`. Here is the simple example
246+
how you can use those options:
247+
248+
```php
249+
250+
echo $form->field($model, 'emails')->widget(MultipleInput::className(), [
251+
'limit' => 5,
252+
'addButtonOptions' => [
253+
'class' => 'btn btn-success',
254+
'label' => 'add' // also you can use html code
255+
],
256+
'removeButtonOptions' => [
257+
'label' => 'remove'
258+
]
259+
])
260+
->label(false);
261+
262+
```
263+
239264
## JavaScript events
240265
This widget has following events:
241266
- `afterInit`: triggered after initialization

src/MultipleInput.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ class MultipleInput extends InputWidget
4444
*/
4545
public $attributeOptions = [];
4646

47+
/**
48+
* @var array the HTML options for the `remove` button
49+
*/
50+
public $removeButtonOptions;
51+
52+
/**
53+
* @var array the HTML options for the `add` button
54+
*/
55+
public $addButtonOptions;
56+
4757

4858
/**
4959
* Initialization.
@@ -90,23 +100,32 @@ protected function guessColumns()
90100
*/
91101
public function run()
92102
{
93-
$renderer = $this->createRenderer();
94-
return $renderer->render();
103+
return $this->createRenderer()->render();
95104
}
96105

97106
/**
98107
* @return TableRenderer
99108
*/
100109
private function createRenderer()
101110
{
102-
return new TableRenderer([
111+
$config = [
103112
'id' => $this->options['id'],
104113
'columns' => $this->columns,
105114
'limit' => $this->limit,
106115
'attributeOptions' => $this->attributeOptions,
107116
'data' => $this->data,
108117
'columnClass' => MultipleInputColumn::className(),
109118
'context' => $this
110-
]);
119+
];
120+
121+
if (!is_null($this->removeButtonOptions)) {
122+
$config['removeButtonOptions'] = $this->removeButtonOptions;
123+
}
124+
125+
if (!is_null($this->addButtonOptions)) {
126+
$config['addButtonOptions'] = $this->addButtonOptions;
127+
}
128+
129+
return new TableRenderer($config);
111130
}
112131
}

src/TabularInput.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ class TabularInput extends Widget
3838
*/
3939
public $attributeOptions = [];
4040

41+
/**
42+
* @var array the HTML options for the `remove` button
43+
*/
44+
public $removeButtonOptions;
45+
46+
/**
47+
* @var array the HTML options for the `add` button
48+
*/
49+
public $addButtonOptions;
50+
4151
/**
4252
* @var Model[]|ActiveRecord[]
4353
*/
@@ -64,29 +74,37 @@ public function init()
6474
parent::init();
6575
}
6676

67-
6877
/**
6978
* Run widget.
7079
*/
7180
public function run()
7281
{
73-
$renderer = $this->createRenderer();
74-
return $renderer->render();
82+
return $this->createRenderer()->render();
7583
}
7684

7785
/**
7886
* @return TableRenderer
7987
*/
8088
private function createRenderer()
8189
{
82-
return new TableRenderer([
90+
$config = [
8391
'id' => $this->options['id'],
8492
'columns' => $this->columns,
8593
'limit' => $this->limit,
8694
'attributeOptions' => $this->attributeOptions,
8795
'data' => $this->models,
8896
'columnClass' => TabularColumn::className(),
8997
'context' => $this
90-
]);
98+
];
99+
100+
if (!is_null($this->removeButtonOptions)) {
101+
$config['removeButtonOptions'] = $this->removeButtonOptions;
102+
}
103+
104+
if (!is_null($this->addButtonOptions)) {
105+
$config['addButtonOptions'] = $this->addButtonOptions;
106+
}
107+
108+
return new TableRenderer($config);
91109
}
92110
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353
template: null,
5454
// string that collect js templates of widgets which uses in the columns
5555
jsTemplates: [],
56-
// remove button css class
57-
btnType: null,
5856
// how many row has to renders
5957
limit: 1
6058
};
@@ -133,8 +131,8 @@
133131
if (settings.limit != null && count >= settings.limit) {
134132
return;
135133
}
136-
var search = ['{multiple-index}', '{multiple-btn-type}', '{multiple-value}'],
137-
replace = [data.currentIndex, settings.btnType, ''];
134+
var search = ['{multiple-index}', '{multiple-value}'],
135+
replace = [data.currentIndex, ''];
138136

139137
for (var i in search) {
140138
template = template.replaceAll(search[i], replace[i]);

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/BaseRenderer.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace unclead\widgets\components;
1010

1111
use Yii;
12+
use yii\helpers\Html;
1213
use yii\base\InvalidConfigException;
1314
use yii\base\Model;
1415
use yii\base\NotSupportedException;
@@ -53,6 +54,16 @@ abstract class BaseRenderer extends Object
5354
*/
5455
public $attributeOptions = [];
5556

57+
/**
58+
* @var array the HTML options for the `remove` button
59+
*/
60+
public $removeButtonOptions;
61+
62+
/**
63+
* @var array the HTML options for the `add` button
64+
*/
65+
public $addButtonOptions;
66+
5667
/**
5768
* @var string
5869
*/
@@ -82,6 +93,27 @@ public function init()
8293
if (!class_exists($this->columnClass)) {
8394
throw new InvalidConfigException('Column class "' . $this->columnClass. '" does not exist');
8495
}
96+
97+
$this->prepareButtonsOptions();
98+
}
99+
100+
private function prepareButtonsOptions()
101+
{
102+
if (!isset($this->removeButtonOptions['class'])) {
103+
$this->removeButtonOptions['class'] = 'btn btn-danger';
104+
}
105+
106+
if (!isset($this->removeButtonOptions['label'])) {
107+
$this->removeButtonOptions['label'] = Html::tag('i', null, ['class' => 'glyphicon glyphicon-remove']);
108+
}
109+
110+
if (!isset($this->addButtonOptions['class'])) {
111+
$this->addButtonOptions['class'] = 'btn btn-default';
112+
}
113+
114+
if (!isset($this->addButtonOptions['label'])) {
115+
$this->addButtonOptions['label'] = Html::tag('i', null, ['class' => 'glyphicon glyphicon-plus']);
116+
}
85117
}
86118

87119

@@ -136,7 +168,6 @@ protected function registerClientScript()
136168
'id' => $this->id,
137169
'template' => $template,
138170
'jsTemplates' => $jsTemplates,
139-
'btnType' => 'btn-danger',
140171
'limit' => $this->limit,
141172
'attributeOptions' => $this->attributeOptions,
142173
]

src/renderers/TableRenderer.php

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
*/
2222
class TableRenderer extends BaseRenderer
2323
{
24-
const ACTION_ADD = 'plus';
25-
const ACTION_REMOVE = 'remove';
26-
2724
/**
2825
* @return mixed
2926
*/
@@ -206,28 +203,54 @@ public function renderCellContent($column, $index)
206203
private function renderActionColumn($index = null)
207204
{
208205
if (is_null($index)) {
209-
$action = self::ACTION_REMOVE;
210-
$type = '{multiple-btn-type}';
206+
$button = $this->renderRemoveButton();
211207
} else {
212-
$action = $index == 0 ? self::ACTION_ADD : self::ACTION_REMOVE;
213-
$type = $index == 0 ? 'btn-default' : 'btn-danger';
208+
$button = $index == 0 ? $this->renderAddButton() : $this->renderRemoveButton();
214209
}
215210

216-
$button = Button::widget(
217-
[
218-
'tagName' => 'div',
219-
'encodeLabel' => false,
220-
'label' => Html::tag('i', null, ['class' => 'glyphicon glyphicon-' . $action]),
221-
'options' => [
222-
'class' => $type . ' multiple-input-list__btn btn js-input-' . $action,
223-
]
224-
]
225-
);
226211
return Html::tag('td', $button, [
227212
'class' => 'list-cell__button',
228213
]);
229214
}
230215

216+
private function renderAddButton()
217+
{
218+
$options = [
219+
'class' => 'multiple-input-list__btn js-input-plus',
220+
];
221+
Html::addCssClass($options, $this->addButtonOptions['class']);
222+
return Button::widget(
223+
[
224+
'tagName' => 'div',
225+
'encodeLabel' => false,
226+
'label' => $this->addButtonOptions['label'],
227+
'options' => $options
228+
]
229+
);
230+
}
231+
232+
/**
233+
* Renders remove button.
234+
*
235+
* @return string
236+
* @throws \Exception
237+
*/
238+
private function renderRemoveButton()
239+
{
240+
$options = [
241+
'class' => 'multiple-input-list__btn js-input-remove',
242+
];
243+
Html::addCssClass($options, $this->removeButtonOptions['class']);
244+
return Button::widget(
245+
[
246+
'tagName' => 'div',
247+
'encodeLabel' => false,
248+
'label' => $this->removeButtonOptions['label'],
249+
'options' => $options
250+
]
251+
);
252+
}
253+
231254
/**
232255
* Returns template for using in js.
233256
*

0 commit comments

Comments
 (0)