首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

Yii Framework 教程 (31):Zii CDetailView 元件實戰 - 單筆資料詳細檢視與關聯導航

     編輯:PHP 綜合知識

CDetailView 核心功能

  • 單筆模型 (CModel/AR/關聯陣列) 詳細屬性顯示。
  • 透過 attributes 陣列配置顯示欄位、格式化類型、自定義標籤。
  • 支援 Name:Type:Label 完整規範:Type 決定格式化器 (raw, text, ntext, html, date, time, datetime, boolean, number, email, image, url)。

基本配置範例


// Controller
public function actionView($id) {
    $model = Customer::model()->findByPk($id);
    if (!$model) throw new CHttpException(404, 'Customer not found');
    $this->render('view', ['model' => $model]);
}

// View (view.php)

widget('zii.widgets.CDetailView', [ 'data' => $model, 'attributes' => [ 'FirstName', 'LastName', 'Company', 'Address', 'City', 'State', 'Country', 'PostalCode', 'Phone', 'Fax', 'Email', [ 'name' => 'Employee', // 虛擬屬性 'value' => $model->employee->FirstName, // 關聯導航 'label' => 'Support Rep', ], ], ]); ?>

屬性配置完整語法


'attributes' => [
    'fieldName',                    // 簡寫:屬性名 = 標籤 = 格式化器預設 text
    'fieldName:html',               // 指定格式化器
    'fieldName:email:Contact Email', // 格式化器 + 自定義標籤
    [                               // 完整陣列語法
        'name' => 'fieldName',      // 屬性名 (支援關聯導航: relation.field)
        'label' => 'Custom Label',  // 顯示標籤
        'type' => 'raw',            // 格式化器類型
        'value' => 'expression',    // 自定義值 (PHP 表達式字串或匿名函數 5.3+)
        'visible' => true,          // 是否顯示
        'cssClass' => 'highlight',  // 行 CSS 類別
        'headerHtmlOptions' => [],  // 標籤儲存格 HTML 選項
        'contentHtmlOptions' => [], // 內容儲存格 HTML 選項
    ],
]

內建格式化器類型對照表

類型對應格式化器方法輸出範例
raw無處理,直接輸出<b>Bold</b>
textCHtml::encode&lt;b&gt;Bold&lt;/b&gt;
ntextnl2br + encode換行轉 <br>
htmlCHtml::encode + purifier (需配置)安全 HTML
dateYii::app()->dateFormatter->formatDate()2023/12/25
timeformatTime()14:30:00
datetimeformatDateTime()2023/12/25 14:30:00
boolean1/0 → Yes/No (可本地化)Yes / No
numberformatNumber()1,234.56
emailmailto: 連結<a href="mailto:...">...</a>
image<img src="...">圖片顯示
url<a href="...">...</a>超連結

關聯導航顯示 (需模型定義 relations)


// Customer.php
public function relations() {
    return [
        'employee' => [self::BELONGS_TO, 'Employee', 'SupportRepId'],
    ];
}

// View 中直接使用關聯屬性
'attributes' => [
    'FirstName',
    'LastName',
    'employee.FirstName:firstName:Support Rep First Name', // 關聯導航 + 格式化 + 標籤
    'employee.Email:email:Support Rep Email',
]

渲染結果示意

CDetailView 渲染結果

進階:自定義格式化器


// protected/components/MyFormatter.php
class MyFormatter extends CFormatter {
    public function formatStatus($value) {
        $map = [0 => 'Pending', 1 => 'Active', 2 => 'Closed'];
        return isset($map[$value]) ? $map[$value] : 'Unknown';
    }
}

// main.php
'components' => [
    'format' => ['class' => 'MyFormatter'],
],

// View 使用
'attributes' => [
    'Status:status', // 會呼叫 formatStatus()
]

完整範例下載

DetailViewDemo.zip

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved