CListView

优质
小牛编辑
128浏览
2023-12-01
所有包 | 属性 | 方法
zii.widgets
继承class CListView » CBaseListView » CWidget » CBaseController » CComponent
源自1.1
版本$Id: CListView.php 3286 2011-06-16 17:34:34Z qiang.xue $
源码framework/zii/widgets/CListView.php
CListView按照列表方式显示数据项。

Unlike CGridView which displays the data items in a table, CListView allows one to use a view template to render each data item. As a result, CListView could generate more flexible rendering result.

CListView supports both sorting and pagination of the data items. The sorting and pagination can be done in AJAX mode or normal page request. A benefit of using CListView is that when the user browser disables JavaScript, the sorting and pagination automatically degenerate to normal page requests and are still functioning as expected.

CListView should be used together with a data provider, preferrably a CActiveDataProvider.

The minimal code needed to use CListView is as follows:

$dataProvider=new CActiveDataProvider('Post');
$this->widget('zii.widgets.CListView', array(  'dataProvider'=>$dataProvider,  'itemView'=>'_post',   // refers to the partial view named '_post'  'sortableAttributes'=>array(      'title',      'create_time'=>'Post Time',  ),
));


The above code first creates a data provider for the Post ActiveRecord class. It then uses CListView to display every data item as returned by the data provider. The display is done via the partial view named '_post'. This partial view will be rendered once for every data item. In the view, one can access the current data item via variable $data. For more details, see itemView.

In order to support sorting, one has to specify the sortableAttributes property. By doing so, a list of hyperlinks that can sort the data will be displayed.

公共属性

隐藏继承属性

属性类型描述定义在
actionPrefixstringactions的ID的前缀。 当微件在CController::actions中声明了 动作提供者,可以为其动作的ID指定前缀以区别 于别的微件或控制器。当微件用于控制器 的视图中时,必须配置同样的前缀。CWidget
afterAjaxUpdatestringa javascript function that will be invoked after a successful AJAX response is received.CListView
ajaxUpdatemixedthe ID of the container whose content may be updated with an AJAX response.CListView
ajaxUrlmixedthe URL for the AJAX requests should be sent to.CListView
ajaxVarstringthe name of the GET variable that indicates the request is an AJAX request triggered by this widget.CListView
baseScriptUrlstringthe base script URL for all list view resources (e.CListView
beforeAjaxUpdatestringa javascript function that will be invoked before an AJAX update occurs.CListView
controllerCController返回此微件所属的控制器。CWidget
cssFilestringthe URL of the CSS file used by this list view.CListView
dataProviderIDataProvider视图需要显示的数据源。CBaseListView
emptyTextstring当dataProvider为空时显示的内容。CBaseListView
enablePaginationboolean是否启用分页。注意:当IDataProvider::pagination的属性 dataProvider为false时则此属性为false,即不启用分页。 当启用分页时,页码会在视图中显示。 默认为true。CBaseListView
enableSortingboolean是否启用排序。注意:当IDataProvider::sort的属性 dataProvider为false时则此属性为false,即不启用排序。 当启用排序时,点击列名就可按该列排序。 默认为true。CBaseListView
htmlOptionsarray部件的HTML属性。CBaseListView
idstring返回此微件的ID。如果需要的话,将生产一个新的ID并将其返回。CWidget
itemViewstringthe view used for rendering each data item.CListView
itemsCssClassstring显示各条数据的容器的CSS类。默认为‘items’。CBaseListView
itemsTagNamestringthe HTML tag name for the container of all data item display.CListView
loadingCssClassstringthe CSS class name that will be assigned to the widget container element when the widget is updating its content via AJAX.CListView
ownerCBaseController返回此微件的所有者或创建者。CWidget
pagerarraypager 的配置信息。默认为array('class'=>'CLinkPager').CBaseListView
pagerCssClassstring显示页码的容器的CSS类。默认为‘pager’。CBaseListView
separatorstringthe HTML code to be displayed between any two consecutive items.CListView
skinmixed微件使用的皮肤的名称。默认为“default”。 如果此属性设置为false,微件将不会有皮肤被使用。CWidget
sortableAttributesarraylist of sortable attribute names.CListView
sorterCssClassstringthe CSS class name for the sorter container.CListView
sorterFooterstringthe text shown after sort links.CListView
sorterHeaderstringthe text shown before sort links.CListView
summaryCssClassstring显示摘要的容器的CSS类。默认为‘summary’。CBaseListView
summaryTextstring摘要内容使用的模板。 下面的这些标记将会被对应的内容替换:
  • {start}: 当前显示的起始行数 (从1开始)
  • {end}: 当前显示的结束行数 (从1开始)
  • {count}: 总行数
  • {page}: 当前显示的页码,自 1.
CBaseListView
tagNamestring视图容器的标签。默认为‘div’。CBaseListView
templatestringthe template to be used to control the layout of various components in the list view.CListView
updateSelectorstringthe jQuery selector of the HTML elements that may trigger AJAX updates when they are clicked.CListView
viewDataarrayadditional data to be passed to itemView when rendering each data item.CListView
viewPathstring返回包含此微件所需的视图文件的路径。CWidget

公共方法

隐藏继承方法

方法描述定义在
__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()初始化list view。CListView
raiseEvent()发起一个事件。CComponent
registerClientScript()注册必要的客户端脚本。CListView
render()渲染一个视图。CWidget
renderContent()渲染视图的主要内容。CBaseListView
renderEmptyText()渲染数据为空时显示的内容。CBaseListView
renderFile()Renders a view file.CBaseController
renderInternal()Renders a view file.CBaseController
renderItems()显示每一个数据项。CListView
renderKeys()渲染一个隐藏标签数据键值。CBaseListView
renderPager()渲染分页。CBaseListView
renderSorter()显示排序了的数据。CListView
renderSummary()渲染摘要内容。CBaseListView
run()渲染视图。CBaseListView
setId()设置此微件的ID。CWidget
widget()Creates a widget and executes it.CBaseController

受保护方法

隐藏继承方法

方法描述定义在
renderSection()渲染指定部分的内容。CBaseListView

属性详细

afterAjaxUpdate 属性 public string $afterAjaxUpdate;

a javascript function that will be invoked after a successful AJAX response is received. The function signature is function(id, data) where 'id' refers to the ID of the list view 'data' the received ajax response data.

ajaxUpdate 属性 public mixed $ajaxUpdate;

the ID of the container whose content may be updated with an AJAX response. Defaults to null, meaning the container for this list view instance. If it is set false, it means sorting and pagination will be performed in normal page requests instead of AJAX requests. If the sorting and pagination should trigger the update of multiple containers' content in AJAX fashion, these container IDs may be listed here (separated with comma).

ajaxUrl 属性 (可用自 v1.1.8) public mixed $ajaxUrl;

the URL for the AJAX requests should be sent to. CHtml::normalizeUrl() will be called on this property. If not set, the current page URL will be used for AJAX requests.

ajaxVar 属性 public string $ajaxVar;

the name of the GET variable that indicates the request is an AJAX request triggered by this widget. Defaults to 'ajax'. This is effective only when ajaxUpdate is not false.

baseScriptUrl 属性 public string $baseScriptUrl;

the base script URL for all list view resources (e.g. javascript, CSS file, images). Defaults to null, meaning using the integrated list view resources (which are published as assets).

beforeAjaxUpdate 属性 public string $beforeAjaxUpdate;

a javascript function that will be invoked before an AJAX update occurs. The function signature is function(id) where 'id' refers to the ID of the list view.

cssFile 属性 public string $cssFile;

the URL of the CSS file used by this list view. Defaults to null, meaning using the integrated CSS file. If this is set false, you are responsible to explicitly include the necessary CSS file in your page.

itemView 属性 public string $itemView;

the view used for rendering each data item. This property value will be passed as the first parameter to either CController::renderPartial or CWidget::render to render each data item. In the corresponding view template, the following variables can be used in addition to those declared in viewData:

  • $this: refers to the owner of this list view widget. For example, if the widget is in the view of a controller, then $this refers to the controller.
  • $data: refers to the data item currently being rendered.
  • $index: refers to the zero-based index of the data item currently being rendered.
  • $widget: refers to this list view widget instance.

itemsTagName 属性 (可用自 v1.1.4) public string $itemsTagName;

the HTML tag name for the container of all data item display. Defaults to 'div'.

loadingCssClass 属性 (可用自 v1.1.1) public string $loadingCssClass;

the CSS class name that will be assigned to the widget container element when the widget is updating its content via AJAX. Defaults to 'list-view-loading'.

separator 属性 (可用自 v1.1.7) public string $separator;

the HTML code to be displayed between any two consecutive items.

sortableAttributes 属性 public array $sortableAttributes;

list of sortable attribute names. In order for an attribute to be sortable, it must also appear as a sortable attribute in the IDataProvider::sort property of dataProvider.

参见

  • enableSorting
sorterCssClass 属性 public string $sorterCssClass;

the CSS class name for the sorter container. Defaults to 'sorter'.

sorterFooter 属性 public string $sorterFooter;

the text shown after sort links. Defaults to empty.

sorterHeader 属性 public string $sorterHeader;

the text shown before sort links. Defaults to 'Sort by: '.

template 属性 public string $template;

the template to be used to control the layout of various components in the list view. These tokens are recognized: {summary}, {sorter}, {items} and {pager}. They will be replaced with the summary text, the sort links, the data item list, and the pager.

updateSelector 属性 (可用自 v1.1.7) public string $updateSelector;

the jQuery selector of the HTML elements that may trigger AJAX updates when they are clicked. If not set, the pagination links and the sorting links will trigger AJAX updates.

viewData 属性 public array $viewData;

additional data to be passed to itemView when rendering each data item. This array will be extracted into local PHP variables that can be accessed in the itemView.

方法详细

init() 方法
public void init()
源码: framework/zii/widgets/CListView.php#169 (显示) publicfunctioninit()
{
if($this->itemView===null)
thrownewCException(Yii::t('zii','Theproperty"itemView"cannotbeempty.'));
parent::init();

if(!isset($this->htmlOptions['class']))
$this->htmlOptions['class']='list-view';

if($this->baseScriptUrl===null)
$this->baseScriptUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets')).'/listview';

if($this->cssFile!==false)
{
if($this->cssFile===null)
$this->cssFile=$this->baseScriptUrl.'/styles.css';
Yii::app()->getClientScript()->registerCssFile($this->cssFile);
}
}

初始化list view。 这个方法会初始化请求值并实例化列columns对象。

registerClientScript() 方法
public void registerClientScript()
源码: framework/zii/widgets/CListView.php#192 (显示) publicfunctionregisterClientScript()
{
$id=$this->getId();

if($this->ajaxUpdate===false)
$ajaxUpdate=array();
else
$ajaxUpdate=array_unique(preg_split('/s*,s*/',$this->ajaxUpdate.','.$id,-1,PREG_SPLIT_NO_EMPTY));
$options=array(
'ajaxUpdate'=>$ajaxUpdate,
'ajaxVar'=>$this->ajaxVar,
'pagerClass'=>$this->pagerCssClass,
'loadingClass'=>$this->loadingCssClass,
'sorterClass'=>$this->sorterCssClass,
);
if($this->ajaxUrl!==null)
$options['url']=CHtml::normalizeUrl($this->ajaxUrl);
if($this->updateSelector!==null)
$options['updateSelector']=$this->updateSelector;
if($this->beforeAjaxUpdate!==null)
$options['beforeAjaxUpdate']=(strpos($this->beforeAjaxUpdate,'js:')!==0?'js:':'').$this->beforeAjaxUpdate;
if($this->afterAjaxUpdate!==null)
$options['afterAjaxUpdate']=(strpos($this->afterAjaxUpdate,'js:')!==0?'js:':'').$this->afterAjaxUpdate;

$options=CJavaScript::encode($options);
$cs=Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');
$cs->registerCoreScript('bbq');
$cs->registerScriptFile($this->baseScriptUrl.'/jquery.yiilistview.js',CClientScript::POS_END);
$cs->registerScript(__CLASS__.'#'.$id,"jQuery('#$id').yiiListView($options);");
}

注册必要的客户端脚本。

renderItems() 方法
public void renderItems()
源码: framework/zii/widgets/CListView.php#227 (显示) publicfunctionrenderItems()
{
echoCHtml::openTag($this->itemsTagName,array('class'=>$this->itemsCssClass))."n";
$data=$this->dataProvider->getData();
if(($n=count($data))>0)
{
$owner=$this->getOwner();
$render=$ownerinstanceofCController?'renderPartial':'render';
$j=0;
foreach($dataas$i=>$item)
{
$data=$this->viewData;
$data['index']=$i;
$data['data']=$item;
$data['widget']=$this;
$owner->$render($this->itemView,$data);
if($j++<$n-1)
echo$this->separator;
}
}
else
$this->renderEmptyText();
echoCHtml::closeTag($this->itemsTagName);
}

显示每一个数据项。

renderSorter() 方法
public void renderSorter()
源码: framework/zii/widgets/CListView.php#255 (显示) publicfunctionrenderSorter()
{
if($this->dataProvider->getItemCount()<=0||!$this->enableSorting||empty($this->sortableAttributes))
return;
echoCHtml::openTag('div',array('class'=>$this->sorterCssClass))."n";
echo$this->sorterHeader===null?Yii::t('zii','Sortby:'):$this->sorterHeader;
echo"<ul>n";
$sort=$this->dataProvider->getSort();
foreach($this->sortableAttributesas$name=>$label)
{
echo"<li>";
if(is_integer($name))
echo$sort->link($label);
else
echo$sort->link($name,$label);
echo"</li>n";
}
echo"</ul>";
echo$this->sorterFooter;
echoCHtml::closeTag('div');
}

显示排序了的数据。