CStarRating 主要用於評分,顯示一組星星(5顆)供使用者評分。
<center class="form">
<?php $form = $this->beginWidget('CActiveForm'); ?>
<?php $this->widget('CStarRating', array(
'model' => $model,
'attribute' => 'rating',
'name' => 'rating',
'value' => 3,
)); ?>
<center class="row submit">
<?php echo CHtml::submitButton('Submit'); ?>
</center>
<?php $this->endWidget(); ?>
</center><!-- form -->
在控制器中可透過 $_POST['rating'] 存取星級值:
public function actionIndex() {
$model = new DataModel();
$model->rating = 3;
if (!empty($_POST['rating'])) {
$model->rating = $_POST['rating'];
if ($model->validate()) {
$this->render('response', array('model' => $model));
return;
}
}
$this->render('index', array('model' => $model));
}
CStarRating 可設為唯讀,僅顯示評分不允許修改,透過 'readOnly' => true 實現。
