Reputation: 143
HI guys I have a app that needs to sum the products values ,I have a multi select dropdown in the form , but the problem is that I need to add the attribute price in the options to be able sum the values selected with javascript.
my code is this
<?php echo $form->dropDownList($modelpp,'product_id',CHtml::listData(Product::model()->findAll(), 'id', 'name'),array('multiple'=>"multiple", 'size'=>"3" )); ?>
how can i add a attribute price in the options ?
Upvotes: 0
Views: 634
Reputation: 6356
I'm not sure there's a good built-in way to handle this. One hacky method would be to use an anonymous function when setting the value for listData to a multi-value, pipe-delimited value, something like this:
<?php echo $form->dropDownList($modelpp,
'product_id',
CHtml::listData(Product::model()->findAll(),
function($model) {
return $model->id . '|' . $model->value;
},
'name'),
array('multiple'=>"multiple", 'size'=>"3" ));
?>
This would result in the value of each of the options having something like "123|456", where 123 would be the id, and 456 would be the value. In your javascript, you'd need to split this on the | to separate the ID and value. You would also need to handle splitting this when submitting to your controller, and separating the id and the value.
You could obviously use a different delimiter, and you might even be able to have the anonymous function return JSON, and use that for the value, which could result in slightly easier parsing in both javascript and PHP.
Upvotes: 1