CStarRating

优质
小牛编辑
124浏览
2023-12-01
所有包 | 属性 | 方法
system.web.widgets
继承class CStarRating » CInputWidget » CWidget » CBaseController » CComponent
源自1.0
版本$Id: CStarRating.php 3515 2011-12-28 12:29:24Z mdomba $
源码framework/web/widgets/CStarRating.php
CStarRating显示一个星级评价控件来收集用户的评价值。

CStarRating的基础是jQuery Star Rating Plugin. 它用一系列的星星来表示评价的高低, 用户可以触动这些星星 来输入他们的评价。当评价值被提交时,返回的值在服务器端可 象其他普通的HTML输入元素的值一样被使用。 例如,使用
$this->widget('CStarRating',array('name'=>'rating'));
我们可以通过$_POST['rating']检索到评价的值。

CStarRating允许对它的外观进行定制,也支持空白评价和只读评价。

公共属性

隐藏继承属性

属性类型描述定义在
actionPrefixstringactions的ID的前缀。 当微件在CController::actions中声明了 动作提供者,可以为其动作的ID指定前缀以区别 于别的微件或控制器。当微件用于控制器 的视图中时,必须配置同样的前缀。CWidget
allowEmptyboolean评价值是否可以为空(不设置)。默认值是true。 当此属性设置为true,在星星的前面将显示一个重置按钮。CStarRating
attributestringthe 与此小物件相关联的属性。 这个名字可以包含方括号 (e.CInputWidget
blurstring星星失去焦点时触发的回调函数。CStarRating
callbackstring星星被点击时触发的回调函数。CStarRating
controllerCController返回此微件所属的控制器。CWidget
cssFilemixed此微件使用的CSS文件。默认值是null,表示 使用与此微件捆绑的默认CSS文件。 如果设置为false,将不使用CSS文件。其他情况下,当使用此微件 时指定的CSS文件将被使用。CStarRating
focusstring星星获得焦点时触发的回调函数。CStarRating
htmlOptionsarray渲染输入标签时附加的HTML选项CInputWidget
idstring返回此微件的ID。如果需要的话,将生产一个新的ID并将其返回。CWidget
maxRatingmixed允许的最高评价值。可以是整数或浮点数,默认值是10。CStarRating
minRatingmixed允许的最低评价值。可以是整数或浮点数,默认值是1。CStarRating
modelCModel与此小物件相关联的数据模型。CInputWidget
namestring输入控件的名字。如果model没有设置,则必须设置此属性。CInputWidget
ownerCBaseController返回此微件的所有者或创建者。CWidget
ratingStepSizemixed评价的分差。这是两颗星之前的最小分差,默认值是1。CStarRating
readOnlyboolean评价值是否只读。默认值是false。 如果此属性设置为true,评价控件将不可改动。CStarRating
resetTextstring重置按钮的提示文本。默认值是null,表示使用系统预定义的文本("Cancel Rating")。CStarRating
resetValuestring星级评价控件被清空时被设置的值。默认值是null,表示使用系统预定义的值("")。CStarRating
skinmixed微件使用的皮肤的名称。默认为“default”。 如果此属性设置为false,微件将不会有皮肤被使用。CWidget
starCountinteger星星的数量。默认值是5。CStarRating
starWidthinteger星星图标的宽度。默认值是null,表示使用系统定义的值(16)。CStarRating
titlesarray与评价选项相关联的标题。键是评价选项,值是评价的标题。 默认值是null,表示使用评价值作为标题。CStarRating
valuestring输入控件的值CInputWidget
viewPathstring返回包含此微件所需的视图文件的路径。CWidget

受保护属性

隐藏继承属性

属性类型描述定义在
clientOptionsarray星级评价控件的JavaScript选项CStarRating

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__construct()构造器。CWidget
__get()返回一个属性值、一个事件处理程序列表或一个行为名称。CComponent
__isset()检查一个属性是否为null。CComponent
__set()设置一个组件的属性值。CComponent
__unset()设置一个组件的属性为null。CComponent
actions()返回此widget使用的动作的列表。CWidget
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。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()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
createWidget()Creates a widget and initializes it.CBaseController
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。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()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
getController()返回此微件所属的控制器。CWidget
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getId()返回此微件的ID。如果需要的话,将生产一个新的ID并将其返回。CWidget
getOwner()返回此微件的所有者或创建者。CWidget
getViewFile()根据视图名查找视图文件。CWidget
getViewPath()返回包含此微件所需的视图文件的路径。CWidget
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasProperty()确定属性是否被定义。CComponent
init()初始化此微件。CWidget
raiseEvent()发起一个事件。CComponent
registerClientScript()注册必须的JavaScript和CSS脚本。CStarRating
registerCssFile()注册必须的CSS文件。CStarRating
render()渲染一个视图。CWidget
renderFile()Renders a view file.CBaseController
renderInternal()Renders a view file.CBaseController
run()运行此微件。CStarRating
setId()设置此微件的ID。CWidget
widget()Creates a widget and executes it.CBaseController

受保护方法

隐藏继承方法

方法描述定义在
getClientOptions()返回星级评价控件的JavaScript选项CStarRating
hasModel()确定此小物件是否与一个数据模型相关联。CInputWidget
renderStars()渲染出星星。CStarRating
resolveNameID()CInputWidget

属性详细

allowEmpty 属性 public boolean $allowEmpty;

评价值是否可以为空(不设置)。默认值是true。 当此属性设置为true,在星星的前面将显示一个重置按钮。

blur 属性 public string $blur;

星星失去焦点时触发的回调函数。

callback 属性 public string $callback;

星星被点击时触发的回调函数。

clientOptions 属性 只读 protected array getClientOptions()

星级评价控件的JavaScript选项

cssFile 属性 public mixed $cssFile;

此微件使用的CSS文件。默认值是null,表示 使用与此微件捆绑的默认CSS文件。 如果设置为false,将不使用CSS文件。其他情况下,当使用此微件 时指定的CSS文件将被使用。

focus 属性 public string $focus;

星星获得焦点时触发的回调函数。

maxRating 属性 public mixed $maxRating;

允许的最高评价值。可以是整数或浮点数,默认值是10。

minRating 属性 public mixed $minRating;

允许的最低评价值。可以是整数或浮点数,默认值是1。

ratingStepSize 属性 public mixed $ratingStepSize;

评价的分差。这是两颗星之前的最小分差,默认值是1。

readOnly 属性 public boolean $readOnly;

评价值是否只读。默认值是false。 如果此属性设置为true,评价控件将不可改动。

resetText 属性 public string $resetText;

重置按钮的提示文本。默认值是null,表示使用系统预定义的文本("Cancel Rating")。

resetValue 属性 public string $resetValue;

星级评价控件被清空时被设置的值。默认值是null,表示使用系统预定义的值("")。

starCount 属性 public integer $starCount;

星星的数量。默认值是5。

starWidth 属性 public integer $starWidth;

星星图标的宽度。默认值是null,表示使用系统定义的值(16)。

titles 属性 public array $titles;

与评价选项相关联的标题。键是评价选项,值是评价的标题。 默认值是null,表示使用评价值作为标题。

方法详细

getClientOptions() 方法
protected array getClientOptions()
{return}array星级评价控件的JavaScript选项
源码: framework/web/widgets/CStarRating.php#181 (显示) protectedfunctiongetClientOptions()
{
$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;
if($this->focus!==null)
{
if(strncmp($this->focus,'js:',3))
$options['focus']='js:'.$this->focus;
else
$options['focus']=$this->focus;
}
if($this->blur!==null)
{
if(strncmp($this->blur,'js:',3))
$options['blur']='js:'.$this->blur;
else
$options['blur']=$this->blur;
}
if($this->callback!==null)
{
if(strncmp($this->callback,'js:',3))
$options['callback']='js:'.$this->callback;
else
$options['callback']=$this->callback;
}
return$options;
}
registerClientScript() 方法
public void registerClientScript(string $id)
$idstring容器的ID。
源码: framework/web/widgets/CStarRating.php#123 (显示) publicfunctionregisterClientScript($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);
}

注册必须的JavaScript和CSS脚本。

registerCssFile() 方法
public static void registerCssFile(string $url=NULL)
$urlstringCSS文件的URL。如果此值为null,将使用一个默认的CSS文件的URL。
源码: framework/web/widgets/CStarRating.php#140 (显示) publicstaticfunctionregisterCssFile($url=null)
{
$cs=Yii::app()->getClientScript();
if($url===null)
$url=$cs->getCoreScriptUrl().'/rating/jquery.rating.css';
$cs->registerCssFile($url);
}

注册必须的CSS文件。

renderStars() 方法
protected void renderStars(string $id, string $name)
$idstringHTML容器的ID
$namestring输入控件的名字
源码: framework/web/widgets/CStarRating.php#153 (显示) protectedfunctionrenderStars($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>1?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']);
echoCHtml::radioButton($name,!strcmp($value,$selection),$options)."n";
}
}

渲染出星星。

run() 方法
public void run()
源码: framework/web/widgets/CStarRating.php#102 (显示) publicfunctionrun()
{
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);

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

运行此微件。 此方法注册所有必须的客户端脚本然后渲染 出输入域。