rulight
(Andres Rulight)
January 12, 2011, 4:31pm
1
Hello, this is my first topic on Yii…
I will appreciate very much if someone can help me with this problem:
Is there a chance to concatenate 2 table columns (first name and last name) in a dropdown list so that the result displays as an option? The dropdown list takes the data from another model which is related to it.
<div class="row">
<?php echo $form->labelEx($model,'table2_id'); ?>
<?php echo $form->dropDownList($model, 'table2_id', CHtml::listData(Table2::model()->findAll(), 'id', 'lastname'),array('prompt' => 'Select')); ?>
<?php echo $form->error($model,'table2_id'); ?>
</div>
I have tried to pass multiple parameters but it doesn´t work.
Any suggesions?
Thanks.
recreate the array yourself:
$models = Table2::model()->findAll();
$data = array();
foreach ($models as $model)
$data[$model->id] = $model->lastname . ' '. $model->secondAttribute;
echo $form->dropDownList($model, 'table2_id', $data ,array('prompt' => 'Select'));
jayrulez
(Waprave)
January 12, 2011, 11:22pm
5
rulight:
Hello, this is my first topic on Yii…
I will appreciate very much if someone can help me with this problem:
Is there a chance to concatenate 2 table columns (first name and last name) in a dropdown list so that the result displays as an option? The dropdown list takes the data from another model which is related to it.
<div class="row">
<?php echo $form->labelEx($model,'table2_id'); ?>
<?php echo $form->dropDownList($model, 'table2_id', CHtml::listData(Table2::model()->findAll(), 'id', 'lastname'),array('prompt' => 'Select')); ?>
<?php echo $form->error($model,'table2_id'); ?>
</div>
I have tried to pass multiple parameters but it doesn´t work.
Any suggesions?
Thanks.
Table2::getFullName()
{
return $this->first_name.' '.$this->last_name;
}
CHtml::listData(Table2::model()->findAll(),'id','fullName');
noorr_swati
(Nooralamswati91)
January 12, 2015, 11:25am
7
recreate the array yourself:
$models = Table2::model()->findAll();
$data = array();
foreach ($models as $model)
$data[$model->id] = $model->lastname . ' '. $model->secondAttribute;
echo $form->dropDownList($model, 'table2_id', $data ,array('prompt' => 'Select'));
i am using yii2 i followed your instructions but this is giving error
here is my code
<?=
$models = Size::model()->findAll();
$data = array();
foreach ($models as $model)
$data[$model->fld_id] = $model->fld_width . ' '. $model->fld_height;
echo $form->dropDownList($model, 'fld_size_id', $data ,array('prompt' => 'Select'));
?>
wakeupnow
(Wakeupnow)
September 21, 2015, 11:20am
8
Here’s the approach I took. I think the latest api supports a custom function.
<?php
$myList = CHtml::listData(MyTable::model()->findAll(), 'id', function($data){
return "({$data->start_date}-{$data->end_date}) {$data->name}";
});
echo $form->dropDownList($model,'foreign_id', $myList); ?>
Here’s the approach I took. I think the latest api supports a custom function.
<?php
$myList = CHtml::listData(MyTable::model()->findAll(), 'id', function($data){
return "({$data->start_date}-{$data->end_date}) {$data->name}";
});
echo $form->dropDownList($model,'foreign_id', $myList); ?>
Gracias me ha servido mucho tu aporte, Thanks