Home > Backend Development > PHP Tutorial > PHP development framework Yii Framework tutorial (30) Zii component-ListView example

PHP development framework Yii Framework tutorial (30) Zii component-ListView example

黄舟
Release: 2023-03-05 09:14:01
Original
1216 people have browsed it

CListView can be used to display lists. CListView supports using custom View templates to display list records, so it can display data tables very flexibly. This is a bit like Android's ListView:-).

CListView supports paging and sorting. Paging and sorting support the use of AJAX to improve the response performance of the page. The use of CListView requires a DataProvider, usually using CActiveDataProvider.

This example modifies the database-Active Record example of Yii Framework Development Tutorial (26), but in order to display paging, we use the Customer database table to display 10 records per page.

Modify the default view protected/views/site/index.php and use the ListView component.

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',),)); ?>
Copy after login

Parameter template Configure the template displayed on the page. The supported parameters are {summary}, {sorter}, {items} and {pager}, which respectively correspond to the summary, sorting, list items and paging control of ListView.

The parameter itemView specifies the View display corresponding to each list item. This example uses site/_view.php, defined as follows:

FirstName . ' ' . $data->LastName);?>
getAttributeLabel('Company')); ?>
:Company); ?>
getAttributeLabel('Address')); 
?>:format->formatUrl($data->Address); ?>
getAttributeLabel('Country')); ?>:Country); ?>
getAttributeLabel('Email')); ?>:
format->formatEmail($data->Email); ?>
Copy after login

Then modify the indexAction method of SiteController:

public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Customer', array(
'pagination'=>array(
'pageSize'=>10,
'pageVar'=>'page',
),
'sort'=>array(
'defaultOrder'=>'Lastname',
),
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
Copy after login

The display result is as follows:

PHP development framework Yii Framework tutorial (30) Zii component-ListView example

The above is the content of the PHP development framework Yii Framework tutorial (30) Zii component-ListView example. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template