Yii Framework v1.1.10 绫诲弬鑰

CListView

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.

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
actionPrefix string actions鐨処D鐨勫墠缂銆 褰撳井浠跺湪CController::actions涓0鏄庝簡 鍔ㄤ綔鎻愪緵鑰咃紝鍙互涓哄叾鍔ㄤ綔鐨処D鎸囧畾鍓嶇紑浠ュ尯鍒 浜庡埆鐨勫井浠舵垨鎺у埗鍣ㄣ傚綋寰欢鐢ㄤ簬鎺у埗鍣 鐨勮鍥句腑鏃讹紝蹇呴』閰嶇疆鍚屾牱鐨勫墠缂銆 CWidget
afterAjaxUpdate string a javascript function that will be invoked after a successful AJAX response is received. CListView
ajaxUpdate mixed the ID of the container whose content may be updated with an AJAX response. CListView
ajaxUrl mixed the URL for the AJAX requests should be sent to. CListView
ajaxVar string the name of the GET variable that indicates the request is an AJAX request triggered by this widget. CListView
baseScriptUrl string the base script URL for all list view resources (e. CListView
beforeAjaxUpdate string a javascript function that will be invoked before an AJAX update occurs. CListView
controller CController 杩斿洖姝ゅ井浠舵墍灞炵殑鎺у埗鍣ㄣ CWidget
cssFile string the URL of the CSS file used by this list view. CListView
dataProvider IDataProvider 瑙嗗浘闇瑕佹樉绀虹殑鏁版嵁婧愩 CBaseListView
emptyText string dataProvider涓虹┖鏃舵樉绀虹殑鍐呭銆 CBaseListView
enablePagination boolean 鏄惁鍚敤鍒嗛〉銆傛敞鎰忥細褰IDataProvider::pagination鐨勫睘鎬 dataProvider涓篺alse鏃跺垯姝ゅ睘鎬т负false锛屽嵆涓嶅惎鐢ㄥ垎椤点 褰撳惎鐢ㄥ垎椤垫椂锛岄〉鐮佷細鍦ㄨ鍥句腑鏄剧ず銆 榛樿涓簍rue銆 CBaseListView
enableSorting boolean 鏄惁鍚敤鎺掑簭銆傛敞鎰忥細褰IDataProvider::sort鐨勫睘鎬 dataProvider涓篺alse鏃跺垯姝ゅ睘鎬т负false锛屽嵆涓嶅惎鐢ㄦ帓搴忋 褰撳惎鐢ㄦ帓搴忔椂锛岀偣鍑诲垪鍚嶅氨鍙寜璇ュ垪鎺掑簭銆 榛樿涓簍rue銆 CBaseListView
htmlOptions array 閮ㄤ欢鐨凥TML灞炴с CBaseListView
id string 杩斿洖姝ゅ井浠剁殑ID銆傚鏋滈渶瑕佺殑璇濓紝灏嗙敓浜т竴涓柊鐨処D骞跺皢鍏惰繑鍥炪 CWidget
itemView string the view used for rendering each data item. CListView
itemsCssClass string 鏄剧ず鍚勬潯鏁版嵁鐨勫鍣ㄧ殑CSS绫汇傞粯璁や负鈥榠tems鈥欍 CBaseListView
itemsTagName string the HTML tag name for the container of all data item display. CListView
loadingCssClass string the CSS class name that will be assigned to the widget container element when the widget is updating its content via AJAX. CListView
owner CBaseController 杩斿洖姝ゅ井浠剁殑鎵鏈夎呮垨鍒涘缓鑰呫 CWidget
pager array pager 鐨勯厤缃俊鎭傞粯璁や负array('class'=>'CLinkPager'). CBaseListView
pagerCssClass string 鏄剧ず椤电爜鐨勫鍣ㄧ殑CSS绫汇傞粯璁や负鈥榩ager鈥欍 CBaseListView
separator string the HTML code to be displayed between any two consecutive items. CListView
skin mixed 寰欢浣跨敤鐨勭毊鑲ょ殑鍚嶇О銆傞粯璁や负鈥渄efault鈥濄 濡傛灉姝ゅ睘鎬ц缃负false锛屽井浠跺皢涓嶄細鏈夌毊鑲よ浣跨敤銆 CWidget
sortableAttributes array list of sortable attribute names. CListView
sorterCssClass string the CSS class name for the sorter container. CListView
sorterFooter string the text shown after sort links. CListView
sorterHeader string the text shown before sort links. CListView
summaryCssClass string 鏄剧ず鎽樿鐨勫鍣ㄧ殑CSS绫汇傞粯璁や负鈥榮ummary鈥欍 CBaseListView
summaryText string 鎽樿鍐呭浣跨敤鐨勬ā鏉裤 涓嬮潰鐨勮繖浜涙爣璁板皢浼氳瀵瑰簲鐨勫唴瀹规浛鎹細
  • {start}: 褰撳墠鏄剧ず鐨勮捣濮嬭鏁 (浠1寮濮)
  • {end}: 褰撳墠鏄剧ず鐨勭粨鏉熻鏁 (浠1寮濮)
  • {count}: 鎬昏鏁
  • {page}: 褰撳墠鏄剧ず鐨勯〉鐮侊紝鑷 1.
CBaseListView
tagName string 瑙嗗浘瀹瑰櫒鐨勬爣绛俱傞粯璁や负鈥榙iv鈥欍 CBaseListView
template string the template to be used to control the layout of various components in the list view. CListView
updateSelector string the jQuery selector of the HTML elements that may trigger AJAX updates when they are clicked. CListView
viewData array additional data to be passed to itemView when rendering each data item. CListView
viewPath string 杩斿洖鍖呭惈姝ゅ井浠舵墍闇鐨勮鍥炬枃浠剁殑璺緞銆 CWidget

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__construct() 鏋勯犲櫒銆 CWidget
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
actions() 杩斿洖姝idget浣跨敤鐨勫姩浣滅殑鍒楄〃銆 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() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
getController() 杩斿洖姝ゅ井浠舵墍灞炵殑鎺у埗鍣ㄣ CWidget
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getId() 杩斿洖姝ゅ井浠剁殑ID銆傚鏋滈渶瑕佺殑璇濓紝灏嗙敓浜т竴涓柊鐨処D骞跺皢鍏惰繑鍥炪 CWidget
getOwner() 杩斿洖姝ゅ井浠剁殑鎵鏈夎呮垨鍒涘缓鑰呫 CWidget
getViewFile() 鏍规嵁瑙嗗浘鍚嶆煡鎵捐鍥炬枃浠躲 CWidget
getViewPath() 杩斿洖鍖呭惈姝ゅ井浠舵墍闇鐨勮鍥炬枃浠剁殑璺緞銆 CWidget
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
init() 鍒濆鍖杔ist 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:

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.

鍙傝

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 (鏄剧ず)
public function init()
{
    if(
$this->itemView===null)
        throw new 
CException(Yii::t('zii','The property "itemView" cannot be empty.'));
    
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);
    }
}

鍒濆鍖杔ist view銆 杩欎釜鏂规硶浼氬垵濮嬪寲璇锋眰鍊煎苟瀹炰緥鍖栧垪columns瀵硅薄銆

registerClientScript() 鏂规硶
public void registerClientScript()
婧愮爜锛 framework/zii/widgets/CListView.php#192 (鏄剧ず)
public function registerClientScript()
{
    
$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:')!=='js:' '').$this->beforeAjaxUpdate;
    if(
$this->afterAjaxUpdate!==null)
        
$options['afterAjaxUpdate']=(strpos($this->afterAjaxUpdate,'js:')!=='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 (鏄剧ず)
public function renderItems()
{
    echo 
CHtml::openTag($this->itemsTagName,array('class'=>$this->itemsCssClass))."\n";
    
$data=$this->dataProvider->getData();
    if((
$n=count($data))>0)
    {
        
$owner=$this->getOwner();
        
$render=$owner instanceof CController 'renderPartial' 'render';
        
$j=0;
        foreach(
$data as $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();
    echo 
CHtml::closeTag($this->itemsTagName);
}

鏄剧ず姣忎竴涓暟鎹」銆

renderSorter() 鏂规硶
public void renderSorter()
婧愮爜锛 framework/zii/widgets/CListView.php#255 (鏄剧ず)
public function renderSorter()
{
    if(
$this->dataProvider->getItemCount()<=|| !$this->enableSorting || empty($this->sortableAttributes))
        return;
    echo 
CHtml::openTag('div',array('class'=>$this->sorterCssClass))."\n";
    echo 
$this->sorterHeader===null Yii::t('zii','Sort by: ') : $this->sorterHeader;
    echo 
"<ul>\n";
    
$sort=$this->dataProvider->getSort();
    foreach(
$this->sortableAttributes as $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;
    echo 
CHtml::closeTag('div');
}

鏄剧ず鎺掑簭浜嗙殑鏁版嵁銆

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.