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 |
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 | 鎽樿鍐呭浣跨敤鐨勬ā鏉裤
涓嬮潰鐨勮繖浜涙爣璁板皢浼氳瀵瑰簲鐨勫唴瀹规浛鎹細
|
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 |
灞炴ц缁
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.
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).
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.
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.
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).
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.
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.
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.
the HTML tag name for the container of all data item display. Defaults to 'div'.
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'.
the HTML code to be displayed between any two consecutive items.
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.
鍙傝
the CSS class name for the sorter container. Defaults to 'sorter'.
the text shown after sort links. Defaults to empty.
the text shown before sort links. Defaults to 'Sort by: '.
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.
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.
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.
鏂规硶璇︾粏
public void init()
|
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瀵硅薄銆
public void registerClientScript()
|
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:')!==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);");
}
娉ㄥ唽蹇呰鐨勫鎴风鑴氭湰銆
public void renderItems()
|
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);
}
鏄剧ず姣忎竴涓暟鎹」銆
public void renderSorter()
|
public function renderSorter()
{
if($this->dataProvider->getItemCount()<=0 || !$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');
}
鏄剧ず鎺掑簭浜嗙殑鏁版嵁銆