Menus

Friday 16 October 2015

Yii2 ListBox Field


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 = [] )
$modelyii\base\Model
The model object
$attributestring
The attribute name or expression. See getAttributeName() for the format about attribute expression.
$itemsarray
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.
$optionsarray
The tag options in terms of name-value pairs. The following options are specially handled:
  • prompt: string, a prompt text to be displayed as the first option;
  • options: array, the attributes for the select option tags. The array keys must be valid option values, and the array values are the extra attributes for the corresponding option tags. For example,
    [
        'value1' => ['disabled' => true],
        'value2' => ['label' => 'value 2'],
    ];
    
  • groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', except that the array keys represent the optgroup labels specified in $items.
  • unselect: string, the value that will be submitted when no option is selected. When this attribute is set, a hidden field will be generated so that if no option is selected in multiple mode, we can still obtain the posted unselect value.
  • encodeSpaces: bool, whether to encode spaces in option prompt and option value with  character. Defaults to false.
  • encode: bool, whether to encode option prompt and option value characters. Defaults totrue. This option is available since 2.0.3.
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.
returnstring
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

<?php $model->offerCategory = ['monday', 'tuesday'] ?>
<?=$form->field($model, 'offerCategory')->listBox([
        'monday' => 'Monday',
        'tuesday' => 'Tuesday',
        'wednesday' => 'Wednesday',
        'thursday' => 'Thursday',
        'friday' => 'Friday',
        'saturday' => 'Saturday',
        'sunday' => 'Sunday'
        ],['multiple' => 'true',
    ]) ?>
//$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.



Related links:


            





No comments:

Post a Comment