Yii Framework 开发教程(31) Zii组件-DetailView 示例



CDetailView为某个Model显示详细内容。这个要显示的Model可以为CModel或是关联数组。

CDetailView通过配置 attributes来决定Model的那些属性需要显示已经以何种格式显示。

每个属性可以使用Name:Type:Label来配置。其中Type和Label都是可选的。

  • “Name” 属性名称.
  • “Label” 可以选,属性的标签名,如果没有配置,则使用属性名称做为标签名称.
  • “Type” 属性的类型,通过类型来决定显示的格式 formatter.可以使用的类型有 raw, text, ntext, html, date, time, datetime, boolean, number, email, image, url. 等,缺省使用text.

本例修改上例Yii Framework 开发教程(30) Zii组件-ListView 示例 ,修改显示列表的列表项模版_view.php ,使客户名称由普通文字变为Link。

  1. <h3><?php echo CHtml::link($data->FirstName . ' ' . $data->LastName,  
  2. $this->createUrl('view',array('CustomerId'=>$data->CustomerId)));?></h3>  
<h3><?php echo CHtml::link($data->FirstName . ' ' . $data->LastName,
$this->createUrl('view',array('CustomerId'=>$data->CustomerId)));?></h3>

当点击客户姓名时,转到链接view.php, 传入参数CustomerId设为Customer 的ID。
创建View.php,使用CDetailView组件

  1. <h2><?php echo 'View Customer'; ?></h2>  
  2.   
  3. <?php $this->widget('zii.widgets.CDetailView'array(  
  4.     'data'=>$model,  
  5.     'attributes'=>array(  
  6.   
  7.                 'FirstName',  
  8.                 'LastName',  
  9.                 'Company',  
  10.                 'Address',  
  11.                 'City',  
  12.                 'State',  
  13.                 'Country',  
  14.                 'PostalCode',  
  15.                 'Phone',  
  16.                 'Fax',  
  17.                 'Email',  
  18.                 array(  
  19.                     'name'=>'Employee',  
  20.                     'value'=>$model->employee->FirstName,  
  21.                     ),  
  22.   
  23.                 ),  
  24.             ));  
  25. ?>  
<h2><?php echo 'View Customer'; ?></h2>

<?php $this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'attributes'=>array(

				'FirstName',
				'LastName',
				'Company',
				'Address',
				'City',
				'State',
				'Country',
				'PostalCode',
				'Phone',
				'Fax',
				'Email',
				array(
					'name'=>'Employee',
					'value'=>$model->employee->FirstName,
					),

				),
			));
?>

使用缺省的格式显示Customer的每个字段,主要的Employee字段,表Customer定义的是SupportRepId做为外键参考Employee,因此修改类Customer定义Relations,参考 Yii Framework 开发教程(27) 数据库-关联Active Record示例

  1. public function relations()  
  2. {  
  3.     return array(  
  4.         'employee'=>array(self::BELONGS_TO,  
  5.         'Employee''SupportRepId'),  
  6.         );  
  7. }  
public function relations()
{
	return array(
		'employee'=>array(self::BELONGS_TO,
		'Employee', 'SupportRepId'),
		);
}

显示结果如下:
201212128005 本例 下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值