ListBox Field
Using below we can create the list box base on model attribute of yii2.0 framework. We added the following options like prompt, size, disabled, style etc
public static string activeListBox ( $model, $attribute, $items, $options = [] ) | ||
$model | yii\base\Model |
The model object
|
$attribute | string |
The attribute name or expression. See getAttributeName() for the format about attribute expression.
|
$items | array |
The option data items. The array keys are option values, and the array values are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). For each sub-array, an option group will be generated whose label is the key associated with the sub-array. If you have a list of data models, you may convert them into the format described above using yii\helpers\ArrayHelper::map().
Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in the labels will also be HTML-encoded.
|
$options | array |
The tag options in terms of name-value pairs. The following options are specially handled:
The rest of the options will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using encode(). If a value is null, the corresponding attribute will not be rendered. See renderTagAttributes() for details on how attributes are being rendered.
|
return | string |
The generated list box tag
|
---|
Listbox with prompt text
<?= $form->field($model, 'population')-> listBox( array('1'=>'1',2=>'2',3=>3,4=>4,5=>5), array('prompt'=>'Select') ); ?>
Listbox with size
<?= $form->field($model, 'population')-> listBox( array('1'=>'1',2=>'2',3=>3,4=>4,5=>5), array('prompt'=>'Select','size'=>3) ); ?>
Listbox with disabled, style properties
<?= $form->field($model, 'population')-> listBox( array('1'=>'1',2=>'2',3=>3,4=>4,5=>5), array('disabled' => true,'style'=>'background:gray;color:#fff;')) ->label('Gender'); ?>
Setting default values in List box
//$mymodels = array of models doesnt work of course $form->field($mymodels, 'post_types')->listBox($items);
data items call function from students modules
<?= echo $form->field($model, 'batch')->listBox(Students::getBatch(), [
'multiple' => true,
'size' => 7,])
->label(FALSE);
?>
Example Yii2 listbox multi-select field
Create related function as getWeekdaysList in Appointment.php modules.
public static function getWeekdaysList()
{
return [
'monday' => 'Monday',
'tuesday' => 'Tuesday',
'wednesday' => 'Wednesday',
'thursday' => 'Thursday',
'friday' => 'Friday',
'saturday' => 'Saturday',
'sunday' => 'Sunday',
];
}
Then in my _form.php
<?= $form->field($model, 'weekdays')->listBox(app\models\Appointment::getWeekdaysList(), [
'multiple' => true,
'size' => 7,
]) ?>
Access values in controller action
public function actionFeepaidcontacts()
{....
...
if ($model->load(Yii::$app->request->post())){
//your code pasted here.
echo $model->weekdays[0]; or
foreach ($model->weekdays as $key => $value) {
echo $value; //print selected weekdays
}
}
....
....
}
*Selected listbox items stored as form of one dimensional array.
No comments:
Post a Comment