system system.base system.caching system.caching.dependencies system.collections system.console system.db system.db.ar system.db.schema system.db.schema.cubrid system.db.schema.mssql system.db.schema.mysql system.db.schema.oci system.db.schema.pgsql system.db.schema.sqlite system.gii system.i18n system.i18n.gettext system.logging system.test system.utils system.validators system.web system.web.actions system.web.auth system.web.filters system.web.form system.web.helpers system.web.renderers system.web.services system.web.widgets system.web.widgets.captcha system.web.widgets.pagers zii.behaviors zii.widgets zii.widgets.grid zii.widgets.jui

CStarRating

system.web.widgets
继承 class CStarRating » CInputWidget » CWidget » CBaseController » CComponent
可用自 1.0
源码 framework/web/widgets/CStarRating.php
CStarRating displays a star rating control that can collect user rating input.

CStarRating is based on jQuery Star Rating Plugin. It displays a list of stars indicating the rating values. Users can toggle these stars to indicate their rating input. On the server side, when the rating input is submitted, the value can be retrieved in the same way as working with a normal HTML input. For example, using
$this->widget('CStarRating',array('name'=>'rating'));
we can retrieve the rating value via $_POST['rating'].

CStarRating allows customization of its appearance. It also supports empty rating as well as read-only rating.

公共属性

隐藏继承的属性

属性类型描述被定义在
actionPrefix string the prefix to the IDs of the actions. CWidget
allowEmpty boolean whether the rating value can be empty (not set). CStarRating
attribute string the attribute associated with this widget. CInputWidget
blur string Callback when the stars are not focused. CStarRating
callback string Callback when the stars are clicked. CStarRating
controller CController Returns the controller that this widget belongs to. CWidget
cssFile mixed the CSS file used for the widget. CStarRating
focus string Callback when the stars are focused. CStarRating
htmlOptions array additional HTML options to be rendered in the input tag CInputWidget
id string Returns the ID of the widget or generates a new one if requested. CWidget
maxRating mixed the maximum rating allowed. CStarRating
minRating mixed the minimum rating allowed. CStarRating
model CModel the data model associated with this widget. CInputWidget
name string the input name. CInputWidget
owner CBaseController Returns the owner/creator of this widget. CWidget
ratingStepSize mixed the step size of rating. CStarRating
readOnly boolean whether the rating value is read-only or not. CStarRating
resetText string the hint text for the reset button. CStarRating
resetValue string the value taken when the rating is cleared. CStarRating
skin mixed the name of the skin to be used by this widget. CWidget
starCount integer the number of stars. CStarRating
starWidth integer the width of star image. CStarRating
titles array the titles associated with the rating options. CStarRating
value string the input value CInputWidget
viewPath string Returns the directory containing the view files for this widget. CWidget

受保护的属性

隐藏继承的属性

属性类型描述被定义在
clientOptions array the javascript options for the star rating CStarRating

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CWidget
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
actions() Returns a list of actions that are used by this widget. CWidget
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
beginCache() Begins fragment caching. CBaseController
beginClip() Begins recording a clip. CBaseController
beginContent() Begins the rendering of content that is to be decorated by the specified view. CBaseController
beginWidget() Creates a widget and executes it. CBaseController
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
createWidget() Creates a widget and initializes it. CBaseController
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
endCache() Ends fragment caching. CBaseController
endClip() Ends recording a clip. CBaseController
endContent() Ends the rendering of content. CBaseController
endWidget() Ends the execution of the named widget. CBaseController
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
getController() Returns the controller that this widget belongs to. CWidget
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getId() Returns the ID of the widget or generates a new one if requested. CWidget
getOwner() Returns the owner/creator of this widget. CWidget
getViewFile() Looks for the view script file according to the view name. CWidget
getViewPath() Returns the directory containing the view files for this widget. CWidget
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the widget. CWidget
raiseEvent() Raises an event. CComponent
registerClientScript() Registers the necessary javascript and css scripts. CStarRating
registerCssFile() Registers the needed CSS file. CStarRating
render() Renders a view. CWidget
renderFile() Renders a view file. CBaseController
renderInternal() Renders a view file. CBaseController
run() Executes the widget. CStarRating
setId() Sets the ID of the widget. CWidget
widget() Creates a widget and executes it. CBaseController

受保护的方法

隐藏继承的方法

方法描述被定义在
getClientOptions() Returns the javascript options for the star rating CStarRating
hasModel() Determines whether this widget is associated with a data model. CInputWidget
renderStars() Renders the stars. CStarRating
resolveNameID() CInputWidget

属性详情

allowEmpty 属性
public boolean $allowEmpty;

whether the rating value can be empty (not set). Defaults to true. When this is true, a reset button will be displayed in front of stars.

blur 属性
public string $blur;

Callback when the stars are not focused.

callback 属性
public string $callback;

Callback when the stars are clicked.

clientOptions 属性 只读
protected array getClientOptions()

the javascript options for the star rating

cssFile 属性
public mixed $cssFile;

the CSS file used for the widget. Defaults to null, meaning using the default CSS file included together with the widget. If false, no CSS file will be used. Otherwise, the specified CSS file will be included when using this widget.

focus 属性
public string $focus;

Callback when the stars are focused.

maxRating 属性
public mixed $maxRating;

the maximum rating allowed. This can be either an integer or a float value. Defaults to 10.

minRating 属性
public mixed $minRating;

the minimum rating allowed. This can be either an integer or a float value. Defaults to 1.

ratingStepSize 属性
public mixed $ratingStepSize;

the step size of rating. This is the minimum difference between two rating values. Defaults to 1.

readOnly 属性
public boolean $readOnly;

whether the rating value is read-only or not. Defaults to false. When this is true, the rating cannot be changed.

resetText 属性
public string $resetText;

the hint text for the reset button. Defaults to null, meaning using the system-defined text (which is 'Cancel Rating').

resetValue 属性
public string $resetValue;

the value taken when the rating is cleared. Defaults to null, meaning using the system-defined value (which is '').

starCount 属性
public integer $starCount;

the number of stars. Defaults to 5.

starWidth 属性
public integer $starWidth;

the width of star image. Defaults to null, meaning using the system-defined value (which is 16).

titles 属性
public array $titles;

the titles associated with the rating options. The keys are ratings and the values are the corresponding titles. Defaults to null, meaning using the rating value as the title.

方法详情

getClientOptions() 方法
protected array getClientOptions()
{return} array the javascript options for the star rating
源码: framework/web/widgets/CStarRating.php#180 (显示)
protected function getClientOptions()
{
    
$options=array();
    if(
$this->resetText!==null)
        
$options['cancel']=$this->resetText;
    if(
$this->resetValue!==null)
        
$options['cancelValue']=$this->resetValue;
    if(
$this->allowEmpty===false)
        
$options['required']=true;
    if(
$this->starWidth!==null)
        
$options['starWidth']=$this->starWidth;
    if(
$this->readOnly===true)
        
$options['readOnly']=true;
    foreach(array(
'focus''blur''callback') as $event)
    {
        if(
$this->$event!==null)
        {
            if(
$this->$event instanceof CJavaScriptExpression)
                
$options[$event]=$this->$event;
            else
                
$options[$event]=new CJavaScriptExpression($this->$event);
        }
    }
    return 
$options;
}

registerClientScript() 方法
public void registerClientScript(string $id)
$id string the ID of the container
源码: framework/web/widgets/CStarRating.php#122 (显示)
public function registerClientScript($id)
{
    
$jsOptions=$this->getClientOptions();
    
$jsOptions=empty($jsOptions) ? '' CJavaScript::encode($jsOptions);
    
$js="jQuery('#{$id} > input').rating({$jsOptions});";
    
$cs=Yii::app()->getClientScript();
    
$cs->registerCoreScript('rating');
    
$cs->registerScript('Yii.CStarRating#'.$id,$js);

    if(
$this->cssFile!==false)
        
self::registerCssFile($this->cssFile);
}

Registers the necessary javascript and css scripts.

registerCssFile() 方法
public static void registerCssFile(string $url=NULL)
$url string the CSS URL. If null, a default CSS URL will be used.
源码: framework/web/widgets/CStarRating.php#139 (显示)
public static function registerCssFile($url=null)
{
    
$cs=Yii::app()->getClientScript();
    if(
$url===null)
        
$url=$cs->getCoreScriptUrl().'/rating/jquery.rating.css';
    
$cs->registerCssFile($url);
}

Registers the needed CSS file.

renderStars() 方法
protected void renderStars(string $id, string $name)
$id string the ID of the container
$name string the name of the input
源码: framework/web/widgets/CStarRating.php#152 (显示)
protected function renderStars($id,$name)
{
    
$inputCount=(int)(($this->maxRating-$this->minRating)/$this->ratingStepSize+1);
    
$starSplit=(int)($inputCount/$this->starCount);
    if(
$this->hasModel())
    {
        
$attr=$this->attribute;
        
CHtml::resolveName($this->model,$attr);
        
$selection=$this->model->$attr;
    }
    else
        
$selection=$this->value;
    
$options=$starSplit>? array('class'=>"{split:{$starSplit}}") : array();
    for(
$value=$this->minRating$i=0;$i<$inputCount; ++$i$value+=$this->ratingStepSize)
    {
        
$options['id']=$id.'_'.$i;
        
$options['value']=$value;
        if(isset(
$this->titles[$value]))
            
$options['title']=$this->titles[$value];
        else
            unset(
$options['title']);
        echo 
CHtml::radioButton($name,!strcmp($value,$selection),$options) . "\n";
    }
}

Renders the stars.

run() 方法
public void run()
源码: framework/web/widgets/CStarRating.php#101 (显示)
public function run()
{
    list(
$name,$id)=$this->resolveNameID();
    if(isset(
$this->htmlOptions['id']))
        
$id=$this->htmlOptions['id'];
    else
        
$this->htmlOptions['id']=$id;
    if(isset(
$this->htmlOptions['name']))
        
$name=$this->htmlOptions['name'];

    
$this->registerClientScript($id);

    echo 
CHtml::openTag('span',$this->htmlOptions)."\n";
    
$this->renderStars($id,$name);
    echo 
"</span>";
}

Executes the widget. This method registers all needed client scripts and renders the text field.