Yii Framework Forum: Modify The Chtml::listdata ? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Modify The Chtml::listdata ? Rate Topic: -----

#1 User is offline   clonevn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 25-March 13

Posted 16 May 2013 - 08:35 AM

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 :).
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,481
  • Joined: 04-March 10
  • Location:UK

Posted 16 May 2013 - 08:47 AM

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');

0

#3 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 16 May 2013 - 08:51 AM

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.
0

#4 User is offline   clonevn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 25-March 13

Posted 16 May 2013 - 08:51 AM

Love your answer Keith :).
Thank you too, seenivasan.
0

#5 User is offline   sefburhan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 136
  • Joined: 08-February 12
  • Location:islamabad

Posted 17 May 2013 - 12:11 AM

View Postclonevn, on 16 May 2013 - 08:35 AM, said:

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')); ?>
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users