Wildan Muhlis
Wildan Muhlis

Reputation: 1603

Composite CHtml::listData value

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.

Upvotes: 1

Views: 3016

Answers (1)

ippi
ippi

Reputation: 10167

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.

Upvotes: 7

Related Questions