Skip to content

Commit 634c580

Browse files
author
Eugene Tupikov
committed
Added support of anonymous function for items attribute
1 parent 04d5c00 commit 634c580

File tree

4 files changed

+64
-22
lines changed

4 files changed

+64
-22
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ Widget support the following options that are additionally recognized over and a
4949
- `title` *string*: the column title
5050
- `value` *Closure*: you can set it to an anonymous function with the following signature: ```function($data) {}```
5151
- `defaultValue` *string*: default value of input,
52-
- `items` *array*: the items for input with type dropDownList, listBox, checkboxList, radioList
52+
- `items` *array*|*Closure*: the items for input with type dropDownList, listBox, checkboxList, radioList or anonymous function
53+
which return array of items and has thw following signature: ```function($data) {}```
5354
- `options` *array*: the HTML attributes for the input
5455
- `headerOptions` *array*: the HTML attributes for the header cell
5556
- `enableError` *boolean*: whether to render inline error for the input. Default to `false`

examples/views/multiple-input.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use unclead\widgets\MultipleInput;
55
use unclead\widgets\examples\models\ExampleModel;
66
use yii\helpers\Html;
7-
use nex\chosen\Chosen;
87
use unclead\widgets\MultipleInputColumn;
98

109
// Note: You have to install https://github.com/kartik-v/yii2-widget-datepicker for correct work an example
@@ -41,14 +40,26 @@
4140
'enableError' => true,
4241
'title' => 'User',
4342
'defaultValue' => 33,
44-
'items' => [
43+
/* it can be an anonymous function
44+
'items' => function($data) {
45+
return [
46+
31 => 'item 31',
47+
32 => 'item 32',
48+
33 => 'item 33',
49+
34 => 'item 34',
50+
35 => 'item 35',
51+
36 => 'item 36',
52+
];
53+
}
54+
*/
55+
'items' => [
4556
31 => 'item 31',
4657
32 => 'item 32',
4758
33 => 'item 33',
4859
34 => 'item 34',
4960
35 => 'item 35',
5061
36 => 'item 36',
51-
],
62+
]
5263
],
5364
[
5465
'name' => 'day',
@@ -83,7 +94,7 @@
8394
],
8495
[
8596
'name' => 'comment',
86-
'type' => 'static',
97+
'type' => MultipleInputColumn::TYPE_STATIC,
8798
'value' => function($data) {
8899
return Html::tag('span', 'static content', ['class' => 'label label-info']);
89100
},

src/components/BaseColumn.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ abstract class BaseColumn extends Object
5050
public $type;
5151

5252
/**
53-
* @var string|Closure
53+
* @var string|\Closure
5454
*/
5555
public $value;
5656

@@ -60,7 +60,23 @@ abstract class BaseColumn extends Object
6060
public $defaultValue;
6161

6262
/**
63-
* @var array
63+
* @var array|\Closure items which used for rendering input with multiple choice, e.g. dropDownList. It can be an array
64+
* or anonymous function with following signature:
65+
*
66+
* ```
67+
*
68+
* 'columns' => [
69+
* ...
70+
* [
71+
* 'name' => 'column',
72+
* 'items' => function($data) {
73+
* // do your magic
74+
* }
75+
* ....
76+
* ]
77+
* ...
78+
*
79+
* ```
6480
*/
6581
public $items;
6682

@@ -85,7 +101,7 @@ abstract class BaseColumn extends Object
85101
public $errorOptions = ['class' => 'help-block help-block-error'];
86102

87103
/**
88-
* @var BaseRenderer
104+
* @var BaseRenderer the renderer instance
89105
*/
90106
public $renderer;
91107

@@ -150,11 +166,11 @@ public function isHiddenInput()
150166
/**
151167
* Prepares the value of column.
152168
*
153-
* @param array|ActiveRecord $data
154169
* @return mixed
155170
*/
156-
public function prepareValue($data)
171+
protected function prepareValue()
157172
{
173+
$data = $this->getModel();
158174
if ($this->value !== null) {
159175
$value = $this->value;
160176
if ($value instanceof \Closure) {
@@ -208,15 +224,15 @@ private function normalize($name) {
208224
* Renders the input.
209225
*
210226
* @param string $name name of the input
211-
* @param mixed $value value of the input
212227
* @param array $options the input options
213228
* @return string
214229
*/
215-
public function renderInput($name, $value, $options)
230+
public function renderInput($name, $options)
216231
{
217232
$options = array_merge($this->options, $options);
218233
$method = 'render' . Inflector::camelize($this->type);
219234

235+
$value = $this->prepareValue();
220236
if (method_exists($this, $method)) {
221237
$input = call_user_func_array([$this, $method], [$name, $value, $options]);
222238
} else {
@@ -227,6 +243,8 @@ public function renderInput($name, $value, $options)
227243

228244

229245
/**
246+
* Renders drop down list.
247+
*
230248
* @param $name
231249
* @param $value
232250
* @param $options
@@ -235,7 +253,21 @@ public function renderInput($name, $value, $options)
235253
protected function renderDropDownList($name, $value, $options)
236254
{
237255
Html::addCssClass($options, 'form-control');
238-
return Html::dropDownList($name, $value, $this->items, $options);
256+
return Html::dropDownList($name, $value, $this->prepareItems(), $options);
257+
}
258+
259+
/**
260+
* Returns the items for list.
261+
*
262+
* @return array|Closure|mixed
263+
*/
264+
private function prepareItems()
265+
{
266+
if ($this->items instanceof \Closure) {
267+
return call_user_func($this->items, $this->getModel());
268+
} else {
269+
return $this->items;
270+
}
239271
}
240272

241273
/**
@@ -247,7 +279,7 @@ protected function renderDropDownList($name, $value, $options)
247279
protected function renderListBox($name, $value, $options)
248280
{
249281
Html::addCssClass($options, 'form-control');
250-
return Html::listBox($name, $value, $this->items, $options);
282+
return Html::listBox($name, $value, $this->prepareItems(), $options);
251283
}
252284

253285
/**
@@ -293,7 +325,7 @@ protected function renderRadioList($name, $value, $options)
293325
$options['item'] = function ($index, $label, $name, $checked, $value) {
294326
return '<div class="radio">' . Html::radio($name, $checked, ['label' => $label, 'value' => $value]) . '</div>';
295327
};
296-
$input = Html::radioList($name, $value, $this->items, $options);
328+
$input = Html::radioList($name, $value, $this->prepareItems(), $options);
297329
return Html::tag('div', $input, ['class' => 'radio-list']);
298330
}
299331

@@ -329,7 +361,7 @@ protected function renderCheckboxList($name, $value, $options)
329361
$options['item'] = function ($index, $label, $name, $checked, $value) {
330362
return '<div class="checkbox">' . Html::checkbox($name, $checked, ['label' => $label, 'value' => $value]) . '</div>';
331363
};
332-
$input = Html::checkboxList($name, $value, $this->items, $options);
364+
$input = Html::checkboxList($name, $value, $this->prepareItems(), $options);
333365
return Html::tag('div', $input, ['class' => 'checkbox-list']);
334366
}
335367

src/renderers/TableRenderer.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ private function renderRowContent($index = null, $item = null)
133133
/* @var $column BaseColumn */
134134
$column->setModel($item);
135135
if ($column->isHiddenInput()) {
136-
$hiddenInputs[] = $this->renderCellContent($column, $index, $item);
136+
$hiddenInputs[] = $this->renderCellContent($column, $index);
137137
} else {
138-
$cells[] = $this->renderCellContent($column, $index, $item);
138+
$cells[] = $this->renderCellContent($column, $index);
139139
}
140140
}
141141

@@ -160,15 +160,13 @@ private function renderRowContent($index = null, $item = null)
160160
*
161161
* @param BaseColumn $column
162162
* @param int|null $index
163-
* @param $data
164163
* @return string
165164
*/
166-
public function renderCellContent($column, $index, $data)
165+
public function renderCellContent($column, $index)
167166
{
168167
$id = $column->getElementId($index);
169-
$value = $column->prepareValue($data);
170168
$name = $column->getElementName($index);
171-
$input = $column->renderInput($name, $value, [
169+
$input = $column->renderInput($name, [
172170
'id' => $id
173171
]);
174172

0 commit comments

Comments
 (0)