Generate select without optgroup on dropDownList in Yii - php

I am using dropDownListRow of bootstrap Yii.
<?php echo $form->dropDownListRow($model, 'roles', array('' => "--Select Roles--", 'Trekking Agency' => CHtml::listData(Roles::model()->findAll(), 'idRole', 'name'))); ?>
It renders my select within the optgroup. I do not want the options in optgroup. What is the way to remove optgroup from the select in Yii?

It looks like you are using ListData within an array and that array has another element, optgroups will be created if you have an array within array structure. For creating empty options use empty htmlOptions attribute as below (see here for details)
<?php echo $form->dropDownListRow($model, 'roles',
CHtml::listData(Roles::model()->findAll(), 'idRole', 'name'),
array('empty'=>'--Select Roles---')); ?>

If you use Yii 2.0++
And items = array(arra(item01=>1), array(item02=>2))
you need this:
use yii\helpers\ArrayHelper;
<?= Html::dropDownList('Name', 'SelectedItem', ArrayHelper::getColumn($listItems, 'key_name')); ?>

Related

disable encode html in activeform field dropdown yii2

How can disable encode html in ActiveForm::Dropdown active form Yii2?
I want to create a select html tag that shows multilevel data so that children make fixed padding than its parents. So, I create an array like this:
$items = [
'Computer'
' Hardware'
' Software',
' Programming'
'&nbps; C#'
];
But space is removed and &nbps; encoded and both not worked. We can use pure html tag, but how can create it using Yii2::ActiveField?
Note that we can encode items before calling widget based on our conditions.
There is any idea?!
To retain the spaces,
echo $form->field($model, 'attribute')->dropDownList($data, [
'encodeSpaces' => true,
]);

Yii - checkboxlist with different labels

I'm creating a checkboxlist in Yii ($form->checkboxlist($model, 'name', $cbOptions) - $cbOptions is a key=>value array).
Now I need to change the labels of the checkboxes (translation in a different file than the regular translation).
How do I change the labels?
I tried
$form->checkboxlist($model, 'name', $cbOptions, array('options'=>array('val1'=>array('label'=>'label1', 'val2'=>...)
But this doesn't work...
Anyone having an idea?
Try this:
<?php echo $form->checkBoxList($model, 'name', array('label1'=>'value1','label2'=>'value2', ...)); ?>
I tested and it worked for me. You can use Yii::t() for "label1" and "label2".

Yii active dropdownlist with optgroup

I try to create dropdown-list with Yii, using listData and activeDropDownList.
I use the examples found on the web, but it refuses to create the optgroups for me.
$data = CHtml::listData(MyModel::model()->getEntries(0), 'id', 'text', 'group');
Generates an array as expected:
Array([group1] => Array([10]=>FirstEntry, [20]=>SecondEntry),
[group2]=>Array([30]=>firstEntryGroup2, [40]=>firstEntryGroup2))
And so on. So it's an associative array filled with sub-arrays...
But when I use
echo CHtml::activeDropDownList($model, 'dropdownName', $data);
All I get is a flat dropdown without the optgroups. Just the entries from the sub-arrays...
Yii 1.1.6 (I read something about safe-attributes and tried to implement it, but with no success...
The old answer below is incorrect. Sorry. You create optgroups using a 'group' attribute in your drop down data array.
array(
array('id'=>256,'text'=>'TV','group'=>'Electrical'),
array('id'=>257,'text'=>'Radio','group'=>'Electrical'),
);
http://www.yiiframework.com/forum/index.php/topic/6903-how-can-i-generate-a-select-list-with-optgroup/
Old answer:
There is a great gist here that shows how do create what you're after: https://gist.github.com/StandardNerd/2909111
<?php echo CHtml::dropDownList('Cars', 'car_id', array(
'Mazda'=>array(
'mazda-rx7'=>'RX7',
'mazda-rx5'=>'RX5',
),
'Volvo'=>array(
'volvo-b9tl'=>'B9TL',
'volvo-l90e-radlader'=>'L90E Radlader',
),
)); ?>
You're currently using an activeDropDownList which should only be different because you add your $model variable instead of 'Cars' and tweak the function
One of solutions is anonymous function.
$data = CHtml::listData(
MyModel::model()->getEntries(0),
'id',
'text',
function(MyModel $aMyModelInstance){
return $aMyModelInstance->getLocalizedGroupNameForThisInstance();
});

Select option dropdownlist yii

When i select value from drop down list,
and I submit form, i want my drop down list to have selected value,
not to be on default value again.
I try like this, but not working:
<?php echo $form->dropDownList($model, 'code', $countriesIssuedList, array('name'=>'countriesIssued'), $select = array($_POST['countriesIssued']));?>
Also i would like to add a first value to be default, not from db, i want to do it in the code like this, array('empty'=>'--Select country--')
but not working.
Thanks
If you must change the name of your dropdownList you must manually set the value of code to $_POST['countriesIssued'] in your controller/view. As for the default, prompt is used to set this.
<?php if(!$model->code) $model->code=$_POST['countriesIssued'];?>
<?php echo $form->dropDownList($model, 'code', $countriesIssuedList, array(
'name'=>'countriesIssued','prompt'=>'--Select country--'
));?>
The second parameter (currently 'code') must be a key in this array: $countriesIssuedList
See for example here.
"Also i would like to add a first value to be default" ... perhaps you can use array_merge()?

Select or Dropdown list from CActiveRecord in Yii

I have table types and i want to build selectbox with all values from this table
In my controller i wrote this code
$allRegistrationTypes = RegistrationType::model()->findAll();
$this->render('index', array('allRegistrationTypes' => $allRegistrationTypes))
How build selectbox in view file ?
Well then its pretty simple all you need to do is first create List Data like
CHtml::ListData(allRegistrationTypes,'value you want to pass when item is selected','value you have to display');
for ex
typeList = CHtml::ListData(allRegistrationTypes,'id','type');
now remember both id and type are fields in table
now all you have to do is if you are using form then
<?php echo $form->dropDownList($model, 'type_id', $typeList, array('empty'=>'Select a tyoe')); ?>
and if you need multiple you can pass multiple => multiple in the array as htmlOptions
You would use CHtml::dropDownList, or activeDropDownList if there is a "parent" model and you want to leverage its validation rules.
If you want to make the <select> element multiple-selection-capable, pass in 'multiple' => 'multiple' and 'size' => X as part of the $htmlOptions parameter.
Simplest Method to get "Select Box" in YII Framework:
<div class="row">
<?php
echo $form->labelEx($model,'county');
$data = CHtml::listData(County::model()->findAll(), 'id', 'county');
$htmlOptions = array('size' => '1', 'prompt'=>'-- select county --', );
echo $form->listBox($model,'county', $data, $htmlOptions);
echo $form->error($model,'county');
?>
</div>
Good Luck..

Categories