search
Homephp教程php手册Yii Framework 开发教程Zii组件-ListView 示例

Yii Framework 开发教程Zii组件-ListView 示例

Jun 13, 2016 am 10:54 AM
frameworkyiiCandevelopTutorialshowExamplecomponents

CListView可以用来显示列表,CListView支持使用自定义的View模板显示列表的的记录,因此可以非常灵活的显示数据的表,这点有点像Android的ListView:-)。

 

CListView 支持分页和排序,分页和排序支持使用AJAX实现从而可以提高页面的响应性能。CListView的使用需要通过DataProvider,通常是使用CActiveDataProvider。

 

本例修改Yii Framework 开发教程(26) 数据库-Active Record示例,不过为了显示分页,我们使用Customer数据库表,每页显示10条记录。

 

修改缺省的视图protected/views/site/index.php,使用ListView组件。

 

 

[php]  

widget('zii.widgets.CListView', array(  

    'dataProvider'=>$dataProvider,  

    'ajaxUpdate'=>false,  

    'template'=>'{sorter}{pager}{summary}{items}{pager}',  

    'itemView'=>'_view',  

    'pager'=>array(  

                'maxButtonCount'=>'7',  

                ),  

            'sortableAttributes'=>array(  

                'FirstName',  

                'LastName',  

                'Country',  

                ),  

)); ?>  

 

widget('zii.widgets.CListView', array(

'dataProvider'=>$dataProvider,

'ajaxUpdate'=>false,

'template'=>'{sorter}{pager}{summary}{items}{pager}',

'itemView'=>'_view',

'pager'=>array(

'maxButtonCount'=>'7',

),

'sortableAttributes'=>array(

'FirstName',

'LastName',

'Country',

),

)); ?>

 

参数template 配置页面显示的模板,支持的参数有 {summary}, {sorter}, {items} 和{pager},分别对应于ListView的汇总,排序,列表项,分页控制。

参数itemView 指明每个列表项对应的View显示。本例使用site/_view.php ,定义如下:

 

 

 

[php]  

 

  

FirstName . ' ' . $data->LastName);?>

 

  

getAttributeLabel('Company')); ?>:  

Company); ?>  


 

  

getAttributeLabel('Address')); ?>:  

format->formatUrl($data->Address); ?>  


 

  

getAttributeLabel('Country')); ?>:  

Country); ?>  


 

  

getAttributeLabel('Email')); ?>:  

format->formatEmail($data->Email); ?>  


 

  

 

 

 

FirstName . ' ' . $data->LastName);?>

 

getAttributeLabel('Company')); ?>:

Company); ?>


 

getAttributeLabel('Address')); ?>:

format->formatUrl($data->Address); ?>


 

getAttributeLabel('Country')); ?>:

Country); ?>


 

getAttributeLabel('Email')); ?>:

format->formatEmail($data->Email); ?>


 

 

然后修改SiteController的indexAction方法:

 

 

 

[php]  

public function actionIndex()  

{  

  

    $dataProvider=new CActiveDataProvider('Customer', array(  

        'pagination'=>array(  

            'pageSize'=>10,  

            'pageVar'=>'page',  

        ),  

                'sort'=>array(  

                    'defaultOrder'=>'Lastname',  

                    ),  

                ));  

    $this->render('index',array(  

        'dataProvider'=>$dataProvider,  

        ));  

}  

 

public function actionIndex()

{

 

$dataProvider=new CActiveDataProvider('Customer', array(

'pagination'=>array(

'pageSize'=>10,

'pageVar'=>'page',

),

'sort'=>array(

'defaultOrder'=>'Lastname',

),

));

$this->render('index',array(

'dataProvider'=>$dataProvider,

));

}

 

 

 

显示结果如下:

 

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version