1 | <?php |
||
50 | class DetailView extends Widget |
||
51 | { |
||
52 | /** |
||
53 | * @var array|object the data model whose details are to be displayed. This can be a [[Model]] instance, |
||
54 | * an associative array, an object that implements [[Arrayable]] interface or simply an object with defined |
||
55 | * public accessible non-static properties. |
||
56 | */ |
||
57 | public $model; |
||
58 | /** |
||
59 | * @var array a list of attributes to be displayed in the detail view. Each array element |
||
60 | * represents the specification for displaying one particular attribute. |
||
61 | * |
||
62 | * An attribute can be specified as a string in the format of "attribute", "attribute:format" or "attribute:format:label", |
||
63 | * where "attribute" refers to the attribute name, and "format" represents the format of the attribute. The "format" |
||
64 | * is passed to the [[Formatter::format()]] method to format an attribute value into a displayable text. |
||
65 | * Please refer to [[Formatter]] for the supported types. Both "format" and "label" are optional. |
||
66 | * They will take default values if absent. |
||
67 | * |
||
68 | * An attribute can also be specified in terms of an array with the following elements: |
||
69 | * |
||
70 | * - attribute: the attribute name. This is required if either "label" or "value" is not specified. |
||
71 | * - label: the label associated with the attribute. If this is not specified, it will be generated from the attribute name. |
||
72 | * - value: the value to be displayed. If this is not specified, it will be retrieved from [[model]] using the attribute name |
||
73 | * by calling [[ArrayHelper::getValue()]]. Note that this value will be formatted into a displayable text |
||
74 | * according to the "format" option. |
||
75 | * - format: the type of the value that determines how the value would be formatted into a displayable text. |
||
76 | * Please refer to [[Formatter]] for supported types. |
||
77 | * - visible: whether the attribute is visible. If set to `false`, the attribute will NOT be displayed. |
||
78 | * - contentOptions: the HTML attributes to customize value tag. For example: `['class' => 'bg-red']`. |
||
79 | * Please refer to [[\yii\helpers\BaseHtml::renderTagAttributes()]] for the supported syntax. |
||
80 | * - captionOptions: the HTML attributes to customize label tag. For example: `['class' => 'bg-red']`. |
||
81 | * Please refer to [[\yii\helpers\BaseHtml::renderTagAttributes()]] for the supported syntax. |
||
82 | */ |
||
83 | public $attributes; |
||
84 | /** |
||
85 | * @var string|callable the template used to render a single attribute. If a string, the token `{label}` |
||
86 | * and `{value}` will be replaced with the label and the value of the corresponding attribute. |
||
87 | * If a callback (e.g. an anonymous function), the signature must be as follows: |
||
88 | * |
||
89 | * ```php |
||
90 | * function ($attribute, $index, $widget) |
||
91 | * ``` |
||
92 | * |
||
93 | * where `$attribute` refer to the specification of the attribute being rendered, `$index` is the zero-based |
||
94 | * index of the attribute in the [[attributes]] array, and `$widget` refers to this widget instance. |
||
95 | */ |
||
96 | public $template = '<tr><th{captionOptions}>{label}</th><td{contentOptions}>{value}</td></tr>'; |
||
97 | /** |
||
98 | * @var array the HTML attributes for the container tag of this widget. The "tag" option specifies |
||
99 | * what container tag should be used. It defaults to "table" if not set. |
||
100 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
101 | */ |
||
102 | public $options = ['class' => 'table table-striped table-bordered detail-view']; |
||
103 | /** |
||
104 | * @var array|Formatter the formatter used to format model attribute values into displayable texts. |
||
105 | * This can be either an instance of [[Formatter]] or an configuration array for creating the [[Formatter]] |
||
106 | * instance. If this property is not set, the "formatter" application component will be used. |
||
107 | */ |
||
108 | public $formatter; |
||
109 | |||
110 | |||
111 | /** |
||
112 | * Initializes the detail view. |
||
113 | * This method will initialize required property values. |
||
114 | */ |
||
115 | 5 | public function init() |
|
134 | |||
135 | /** |
||
136 | * Renders the detail view. |
||
137 | * This is the main entry of the whole detail view rendering. |
||
138 | */ |
||
139 | public function run() |
||
151 | |||
152 | /** |
||
153 | * Renders a single attribute. |
||
154 | * @param array $attribute the specification of the attribute to be rendered. |
||
155 | * @param integer $index the zero-based index of the attribute in the [[attributes]] array |
||
156 | * @return string the rendering result |
||
157 | */ |
||
158 | 2 | protected function renderAttribute($attribute, $index) |
|
173 | |||
174 | /** |
||
175 | * Normalizes the attribute specifications. |
||
176 | * @throws InvalidConfigException |
||
177 | */ |
||
178 | 5 | protected function normalizeAttributes() |
|
232 | } |
||
233 |