How to set the default / initial value of CStarRating

hi everyone,

now i have successfully installed the CStarRating widget on my page. it worked and i can retrieve the values that users input. But i find it’s impossible to set the initial value of the widget (e.g to demonstrate the average score previously put by all users).For example, if the average score of the current user is 8.5, it’s natural to have the 1st - 9th star selected on load, that’s what i call the default/initial value.

but how to set this value to CStarRating?

Is there something i can do about the htmlOptions or attributes property of the widget?

Or i have to find a way to get the <input id=‘score_9’…> checked?

ps. $(“input#score_9”).attr(“checked”,“checked”); will never make the 9th input checked. i don’t know why. but i don’t think this is the right solution.




<?php $this->widget('CStarRating',array(

'name'=>'score',

'minRating'=>1,

'maxRating'=>10,

'cssFile'=>'/css/rating.css',

'ratingStepSize'=>1,

'callback'=>'function(value){$.ajax({

   type: "POST",

   url: "/user/rating/'.$user->id.'",

   data: "vallue="+value,                            

   success: function(msg){$(".notice").html(msg);}

})}'));

?>



thank you for your advice

sincere greetings,

In the ajaxLink success handler there is one "}" at the end !

hi Mdomba, thank you for that. i have changed the format of the ajax and they work fine now, but as you can see, the CStarRating control is not easy to control. i spent over 6 hours to figure how to set the initial value but failed.

What is the problem with setting the initial value?

There is the "value" attribute… or you can use the "model and attribute" attributes…

thanks! Mdomba.

yes, value, it works. you saved me again.

could you spare a minute to tell me what i can do with the model attribute to make the widget more powerful?

If you have an attribute like “rating” in your model, that will hold the value of CStarRating, instead of writing ‘value’=>$model->rating you can use something like:




'model'=>$model,

'attribute'=>'rating',



For me the value is not set for one reason which I detected it, but I don’t know if you have better solution. The thing is that rating in my MySql is defined as Decimal(2,1), and in my model, Gii defined it as string. I believe since this value is string, the star value cannot set. I put a constant integer and/or float value, manually in CStar ‘value’ it worked well. I figure out to use


'value'=> (int) $model->rate

, and it worked, but using only attribute it doesn’t work. I even changed the model rate variable to integer it didn’t work. Any thought on this?