Menus

Tuesday 18 August 2015

DropDownList



Steps in Use DropDwonList


In controller


          $model = new SomeModel();

          $items=ArrayHelper::map(ModelName::find()->all(),'id','name');

          return $this->render('view',['model'=>$model, 'items'=>$items])


Add in view

         <?php $form = ActiveForm::begin(); ?>

                 <?= $form->field($model, 'item_id')->dropDownList($items) ?>


         <?php ActiveForm::end(); ?>


-----------------




Method of Add in DropDwonList in activeform

<?=
          $form->field($model, 'permissions')->dropDownList($model->getPermissions(),
         ['prompt'=>'- Choose Your Permissions -']) 
?>



<?= 
         $form->field($model, 'company_status')->dropDownList([ 'ACTIVE' => 'ACTIVE',                                 'INACTIVE' => 'INACTIVE', ], ['prompt' => 'Status']) 
?>



<?= 
          $form->field($model, 'status')->dropDownList([ 'Active' => 'Active', 'Droped' =>                         'Droped', 'Completed' => 'Completed', ],['selected'=>true], ['prompt' => '']) 
?>



Click to view - Dependent Drop Down List Tutorials



Default Selection DropDownList Yii2

<?php
 
    $model->palce_or_subcenter = Yii::$app->user->identity->subcenter;   //default value set
    echo $form->field($model, 'palce_or_subcenter')->dropDownList(
                    ArrayHelper::map(Groupitems::find()->andwhere(['category'=>6])->andwhere(['!=','id',0])->orderBy('description')->all(),'slid','description'),
                    ['prompt'=>'Select One']
                    );
 ?>

Yii2 dropdownList select default option

<?= $form->field($model, 'ccid')->dropDownList(
                        getCenter(),['options' => [$ccid => ['Selected'=>'selected']]) ?>

'options'=>['72'=>['Selected'=>true]]


readonly dropdown


"disabled"=>"disabled" 
instead of "readonly"=>true





Set Dropdown list width in yii2


You can just add style in dropDownList show below code


'style' => 'width:90px !important'


<?= $form->field($model, 'experience_month')->dropDownList([ '0' => 0,'1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5 ,'6'=>6,'7'=>7,'8'=>8,'9'=>9,'10'=>10,'11'=>11], 
['style' => 'width:90px !important', 'prompt' => 'Month'])->label('Year & Month') ?>



Yii2 Display DropDownList Values Using funtion


make a function in your model to return an array of your list


public function getOptions()
{
   return array(
      'EASY',
      'MEDIUM',
      'HARD',
   );
}

then you could use it like this


echo $form->dropdownList($model , 'category' , $model->options); // this will use that function to get the array



yii2:drop-down list for multiple values concat in one line


use this code concat multiple fields in single line.


ArrayHelper::map(\app\models\Medicine::find()->asArray()->all(),
    'id',
    function($model, $defaultValue) {
        return $model['medicine_name'].'-'.$model['medicine_id'];
    }
)


Example

<?= $form->field($modelSalesitem, "[{$i}]stockitem_id")
                                    ->dropDownList(
                                    ArrayHelper::map(Stockcenter::find()->where(['center_ccid'=>1])->all(),       
                                    'stockitem_id' ,  function($model, $defaultValue) {
                                                    return $model['stockitem']['item_name'].'    -'.$model['stock'].'  -                                                     -'.$model['amount'];}),

                                            ['prompt'=>'Select Item']          
                                            )->label(FALSE); ?>  







No comments:

Post a Comment