@@ -50,7 +50,7 @@ abstract class BaseColumn extends Object
50
50
public $ type ;
51
51
52
52
/**
53
- * @var string|Closure
53
+ * @var string|\ Closure
54
54
*/
55
55
public $ value ;
56
56
@@ -60,7 +60,23 @@ abstract class BaseColumn extends Object
60
60
public $ defaultValue ;
61
61
62
62
/**
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
+ * ```
64
80
*/
65
81
public $ items ;
66
82
@@ -85,7 +101,7 @@ abstract class BaseColumn extends Object
85
101
public $ errorOptions = ['class ' => 'help-block help-block-error ' ];
86
102
87
103
/**
88
- * @var BaseRenderer
104
+ * @var BaseRenderer the renderer instance
89
105
*/
90
106
public $ renderer ;
91
107
@@ -150,11 +166,11 @@ public function isHiddenInput()
150
166
/**
151
167
* Prepares the value of column.
152
168
*
153
- * @param array|ActiveRecord $data
154
169
* @return mixed
155
170
*/
156
- public function prepareValue ($ data )
171
+ protected function prepareValue ()
157
172
{
173
+ $ data = $ this ->getModel ();
158
174
if ($ this ->value !== null ) {
159
175
$ value = $ this ->value ;
160
176
if ($ value instanceof \Closure) {
@@ -208,15 +224,15 @@ private function normalize($name) {
208
224
* Renders the input.
209
225
*
210
226
* @param string $name name of the input
211
- * @param mixed $value value of the input
212
227
* @param array $options the input options
213
228
* @return string
214
229
*/
215
- public function renderInput ($ name , $ value , $ options )
230
+ public function renderInput ($ name , $ options )
216
231
{
217
232
$ options = array_merge ($ this ->options , $ options );
218
233
$ method = 'render ' . Inflector::camelize ($ this ->type );
219
234
235
+ $ value = $ this ->prepareValue ();
220
236
if (method_exists ($ this , $ method )) {
221
237
$ input = call_user_func_array ([$ this , $ method ], [$ name , $ value , $ options ]);
222
238
} else {
@@ -227,6 +243,8 @@ public function renderInput($name, $value, $options)
227
243
228
244
229
245
/**
246
+ * Renders drop down list.
247
+ *
230
248
* @param $name
231
249
* @param $value
232
250
* @param $options
@@ -235,7 +253,21 @@ public function renderInput($name, $value, $options)
235
253
protected function renderDropDownList ($ name , $ value , $ options )
236
254
{
237
255
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
+ }
239
271
}
240
272
241
273
/**
@@ -247,7 +279,7 @@ protected function renderDropDownList($name, $value, $options)
247
279
protected function renderListBox ($ name , $ value , $ options )
248
280
{
249
281
Html::addCssClass ($ options , 'form-control ' );
250
- return Html::listBox ($ name , $ value , $ this ->items , $ options );
282
+ return Html::listBox ($ name , $ value , $ this ->prepareItems () , $ options );
251
283
}
252
284
253
285
/**
@@ -293,7 +325,7 @@ protected function renderRadioList($name, $value, $options)
293
325
$ options ['item ' ] = function ($ index , $ label , $ name , $ checked , $ value ) {
294
326
return '<div class="radio"> ' . Html::radio ($ name , $ checked , ['label ' => $ label , 'value ' => $ value ]) . '</div> ' ;
295
327
};
296
- $ input = Html::radioList ($ name , $ value , $ this ->items , $ options );
328
+ $ input = Html::radioList ($ name , $ value , $ this ->prepareItems () , $ options );
297
329
return Html::tag ('div ' , $ input , ['class ' => 'radio-list ' ]);
298
330
}
299
331
@@ -329,7 +361,7 @@ protected function renderCheckboxList($name, $value, $options)
329
361
$ options ['item ' ] = function ($ index , $ label , $ name , $ checked , $ value ) {
330
362
return '<div class="checkbox"> ' . Html::checkbox ($ name , $ checked , ['label ' => $ label , 'value ' => $ value ]) . '</div> ' ;
331
363
};
332
- $ input = Html::checkboxList ($ name , $ value , $ this ->items , $ options );
364
+ $ input = Html::checkboxList ($ name , $ value , $ this ->prepareItems () , $ options );
333
365
return Html::tag ('div ' , $ input , ['class ' => 'checkbox-list ' ]);
334
366
}
335
367
0 commit comments