1

I want to post group value in a drop down. How do i concate id and group (type) in value field?

CHtml::listData($eventLocationByUser, 'id', 'caption', 'type');

I have tried:

CHtml::listData($eventLocationByUser, 'id'.'type', 'caption', 'type');

but returning empty value.

Wildan Muhlis
  • 1,553
  • 2
  • 22
  • 43

1 Answers1

7

Since the value field accepts an anonymous function you can do it like this:

CHtml::listData( 
    $eventLocationByUser, 
    'id', 
    function($loc){ return $loc->id . " " . $loc->type; }
);

And here is the example from the docs.


One alternative way could be to add another field to your model (lets say $idtype for example), and every time you create or save a record you update this field as well (using a behavior perhaps?) And then you can use:

CHtml::listData( $eventLocationByUser, 'id', 'idtype' });

It could potentially move business logic from your view to your model, but only you can decide if it's worth the hassle.

ippi
  • 9,857
  • 2
  • 39
  • 50
  • 1
    The anonymous function only works from version **1.1.13**. For anything before that you can use the model's `afterFind()` to set a new variable to the combination of `id` and `type` – topher May 31 '13 at 08:32