Default select one item in the dropDownList Yii - yii

I have a dynamic dropdown, data coming from the database.
<?php $sel_id = $selected_id_array[0]->UPR_RelationType;?>
My dropdowm looks like this
<?php echo CHtml::dropDownList('RelationType_'.$pat_id[0]->PAT_ID,'U2U_RelationType',CHtml::listData(MasterTypeItems::model()->findAllByAttributes(array('MSTT_MST_ID'=>$relationship_type_array[0]->MST_ID),array('order' => 'MSTT_Name')), 'MSTT_ID', 'MSTT_Name'),array('id'=>'select','class'=>'relation_type','style'=>'width:50px'));
In this dropdown i have to select defaultly $sel_id;
for example am getting $sel_id=5; In the drop down i have to select 5th option as selected in yii. please give me any suggestion what i have to write in dropDown to select $sel_id;

If I understand your question correctly, what you want to achieve is a dropdownlist which already has a pre-selected option. If so, then this piece of code should work.
echo dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))
where $select would be your default selected item.
More information can be found here: http://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detail

Related

How to set items per page dropdown in grid view for yii

I want to set items per page dropdown in yii gridview.
What changes i need to do this? Has anyone had implemented this type of functionality before?
You have to add summaryText entry to the gridview as below.
$pageSize=Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider_countries,
'summaryText'=>'Displaying {start}-{end} of {count} result(s). ' .
CHtml::dropDownList(
'pageSize',
$pageSize,
Yii::app()->params['pageSizeOptions'],
array('class'=>'change-pageSize')) .' rows per page',
Result is similar to following.

Yii, set radio button value fetch from database

In Yii, I listed my table which is fetch from database in grid view.
'value'=> 'CHtml::radioButton("set_default",false,array(
"value"=>"$data->id",
"set"=>"1",
"disable"=>"disable",
"uncheckValue"=>null
))',
the above code I entered in normal radio button view, how do I fetch from database? Anybody help me?
CGridView lets you write any value inside row's column instead of normal database values. For modify column values, you need to add a function for value attribute. Inside of this function, you can access each data's attributes. In your case, You need to echo a radio button like this:
//$data refres to each data row in the CGridView
'value' => function ($data, $row) {
echo
CHtml::radioButton("set_default",false,array(
"value"=>$data->id,
"set"=>"1",
"disable"=>"disable",
"uncheckValue"=>null
)),
},

Yii populate a dropdown on select of another

I have a dropdown that I want to populate when an item in another dropdown is selected. Both the dropdown are tied to data/model passed on from controller. and the first dropdown is populated from DB by calling a function in the model. Heres' the form,
echo $form->dropDownListRow($modelunit,
'superunit',
$model->getSunits(),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('user/getunits'),
'update'=>'#unit_id',
)));
echo CHtml::dropDownList('unit_id','', array());
Here's the action user/getunits called by Ajax.
$data=Unit::model()->findAll('sid=:sid',
array(':sid'=>(int) $_POST['superunit']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
I keep getting an error "Undefined index: superunit" when first dropdown is selected. Also, you may notice I am using form->dropDownListRow for the first dropdown while using CHtml::dropDownList for the second. That's cause I am clueless on the syntax of how exactly to make sure the dropdown is populated correctly with ajax and at also properly bind to the model.
You use $form->dropDownListRow that's why you will get $_POST['MyModelName']['superunit'] on your server side
Change you code like
$data=Unit::model()->findAll('sid=:sid',
array(':sid'=>(int) $_POST['MyModelName']['superunit']));
Where MyModelName is a model that you use)
Or like
echo CHtml::dropDownList('superunit'.....
For others - this wiki may help.

Updating the dropdown list field value which is created using drop down list using create

i got a problem while updating a record for a model in yii. i have 2 models. im working on one model now. I'm creating a record for one model using create controller. in the form i've 1 fields which is the name field(im getting this name from other model(table). im getting the names from this second table and showing them in dropdown list. and storing them.
when it comes to update its again coming up with the same dropdown what i've shown using the _form.php for creating a record. can anyone pls tell me how can i show the name instead of dropdown list again??
thanks in advance.
Add a simple check in your view to see if the value has already been added or not. Something like this:
<?php if ($model->attribute && $model->attribute != ''): ?>
// Code to display a normal textfield here
<?php else: ?>
// Code to display dropdown
<?php endif; ?>

Change the Row Color Based on the Column value in CGridView

In Yii, CGridView has it's own background color in the row. But what I want to do is highlight particular row based on the value of one of the column.
For Instance, I have three column, id, name, status. Now, If the Value of status is Inactive or 0, I should highlight the row with some color.
I read the class reference briefly and searched this site as well. But could not find the relevant solution. If some example or some direction toward the right solution, that would be much appreciated.
Thanks,
Ujjwal
CGridView 'rowCssClassExpression' is the way to get what you want.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'rowCssClassExpression'=>'($data->myFlag==0)?"normal":"especial"',
'columns'=>array(
...
),
));
You can also call a custom php function, and pass the $data variable to it. That function should return the class name for the given row :)
Use rowCssClass and rowCssClassExpression for your functionality. I did not tested this code but the trick you can use to get your solution.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'rowCssClass'=>array('odd','even'),
'rowCssClassExpression'=>($data->status==0)?even:odd,
'columns'=>array(
),
));