Hello great community of Yii:
I have a special situation where I have a create/update form in Model A I need to show a DropDownList from Model B. What I need to do is: show the dropdownlist including the inactive records in Model B, but show these inactive records as "disabled". Why? Because if a record in Model A has already picked a record from Model B and then after some time that record in Model B is de-activated, then I still want Model A to show that it contains a related record from Model B which is disabled at this time. I hope it makes sense.
This is what I have right now:
$dataContents = CHtml::listData($modelContents->findAll(), 'content_id', 'name'); // where `content_id` is from Model B
This generates the dropdown items that I feed to my dropDownList afterwards.
So, how do I tell listData to assign "disabled" attribute to items that have the "active" column set to 0? Do I have to extend listData in order to provide this functionality or is there an easier way to do it?
Thank you in advance for your help.
Allright, I found my own solution, it is below. If someone has any tips how to make this better, please share!
_form.php
$dataContents=$modelContents->findAll(); // Querying ALL of Model A
$dataContentArr = Array(); // Declare array to store all items from Model A
$dataDisabledContentArr = Array(); // Declare array to store the disabled items from Model B
foreach($dataContents as $value)
{
if ($value->active==0) { // We found an inactive item on Model B
$dataDisabledContentArr[$value->content_id] = array('disabled'=>true); // Store it here
}
$dataContentArr[$value->content_id] = $value->name; // Now storing item from Model A
}
// ----
// I'm using EchMultiSelect widget, so here is how I implement:
// ----
$this->widget('ext.widgets.EchMultiSelect', array(
'model' => $modelContents, // This is declared above: $modelContents = Contents::model();
'dropDownAttribute' => 'content_id',
'data' => $dataContentArr, // This is where we use the data from Model B, stored in that array above
'options' => array(
'filter'=>true,
'selectedList'=>4,
),
'dropDownHtmlOptions'=> array(
'class'=>'input_full',
'style'=>'width:352px;',
'options'=>$dataDisabledContentArr, // This is where we tell the widget to disable Model B's inactive rows/items
),
'filterOptions'=> array(
'width'=>150,
)
));
I hope this helps someone because I just wasted 2 hours looking for a way to disable inactive items from a database. Also, if you have any tips please let me know!
Thank you for the great piece of software and awesome community!