clonevn
(Clonevn)
May 16, 2013, 1:35pm
1
Im trying to make a dropdownlist as: <?php echo CHtml::dropDownList(‘product’, $id, $list) with $list is:
$models = Category::model()->findAll(array('order' => 'date'));
$list = CHtml::listData($models, 'name', 'date');
The thing is the date column is not yet formatted. I want to format it but dont know to do stuff with the listData(). It should be something like: $list = CHtml::listData($models, ‘name’, myformat($model->date));. I tried to format it and put it into an array but could not add it in the CHtml::listData. Please help .
Keith
(Kburton)
May 16, 2013, 1:47pm
2
Create a function in your model called something like getFormattedDate():
public function getFormattedDate()
{
// return the date formatted as required
}
Then update CHtml::listData() to use this property:
$list = CHtml::listData($models, 'name', 'formattedDate');
Dear Friend
You can pass anonymous functions as 2nd, 3rd or 4th parameters in CHtml::listData().
$list = CHtml::listData($models, 'name',function($model) {
return //do something with $model->date;
});
Regards.
clonevn
(Clonevn)
May 16, 2013, 1:51pm
4
Love your answer Keith .
Thank you too, seenivasan.
clonevn:
Im trying to make a dropdownlist as: <?php echo CHtml::dropDownList(‘product’, $id, $list) with $list is:
$models = Category::model()->findAll(array('order' => 'date'));
$list = CHtml::listData($models, 'name', 'date');
The thing is the date column is not yet formatted. I want to format it but dont know to do stuff with the listData(). It should be something like: $list = CHtml::listData($models, ‘name’, myformat($model->date));. I tried to format it and put it into an array but could not add it in the CHtml::listData. Please help .
Create a function in model and return the whole List(what u need here) and just pass it to
CHtml::dropDownList(),
<?php echo CHtml::dropDownList($model,‘name’,MODELNAME::MyList(‘param’)); ?>