1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\widgets; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use Closure; |
12
|
|
|
use yii\helpers\ArrayHelper; |
13
|
|
|
use yii\helpers\Html; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The ListView widget is used to display data from data |
17
|
|
|
* provider. Each data model is rendered using the view |
18
|
|
|
* specified. |
19
|
|
|
* |
20
|
|
|
* For more details and usage information on ListView, see the [guide article on data widgets](guide:output-data-widgets). |
21
|
|
|
* |
22
|
|
|
* @author Qiang Xue <[email protected]> |
23
|
|
|
* @since 2.0 |
24
|
|
|
*/ |
25
|
|
|
class ListView extends BaseListView |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var array|Closure the HTML attributes for the container of the rendering result of each data model. |
29
|
|
|
* This can be either an array specifying the common HTML attributes for rendering each data item, |
30
|
|
|
* or an anonymous function that returns an array of the HTML attributes. The anonymous function will be |
31
|
|
|
* called once for every data model returned by [[dataProvider]]. |
32
|
|
|
* The "tag" element specifies the tag name of the container element and defaults to "div". |
33
|
|
|
* If "tag" is false, it means no container element will be rendered. |
34
|
|
|
* |
35
|
|
|
* If this property is specified as an anonymous function, it should have the following signature: |
36
|
|
|
* |
37
|
|
|
* ```php |
38
|
|
|
* function ($model, $key, $index, $widget) |
39
|
|
|
* ``` |
40
|
|
|
* |
41
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
42
|
|
|
*/ |
43
|
|
|
public $itemOptions = []; |
44
|
|
|
/** |
45
|
|
|
* @var string|callable the name of the view for rendering each data item, or a callback (e.g. an anonymous function) |
46
|
|
|
* for rendering each data item. If it specifies a view name, the following variables will |
47
|
|
|
* be available in the view: |
48
|
|
|
* |
49
|
|
|
* - `$model`: mixed, the data model |
50
|
|
|
* - `$key`: mixed, the key value associated with the data item |
51
|
|
|
* - `$index`: integer, the zero-based index of the data item in the items array returned by [[dataProvider]]. |
52
|
|
|
* - `$widget`: ListView, this widget instance |
53
|
|
|
* |
54
|
|
|
* Note that the view name is resolved into the view file by the current context of the [[view]] object. |
55
|
|
|
* |
56
|
|
|
* If this property is specified as a callback, it should have the following signature: |
57
|
|
|
* |
58
|
|
|
* ```php |
59
|
|
|
* function ($model, $key, $index, $widget) |
60
|
|
|
* ``` |
61
|
|
|
*/ |
62
|
|
|
public $itemView; |
63
|
|
|
/** |
64
|
|
|
* @var array additional parameters to be passed to [[itemView]] when it is being rendered. |
65
|
|
|
* This property is used only when [[itemView]] is a string representing a view name. |
66
|
|
|
*/ |
67
|
|
|
public $viewParams = []; |
68
|
|
|
/** |
69
|
|
|
* @var string the HTML code to be displayed between any two consecutive items. |
70
|
|
|
*/ |
71
|
|
|
public $separator = "\n"; |
72
|
|
|
/** |
73
|
|
|
* @var array the HTML attributes for the container tag of the list view. |
74
|
|
|
* The "tag" element specifies the tag name of the container element and defaults to "div". |
75
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
76
|
|
|
*/ |
77
|
|
|
public $options = ['class' => 'list-view']; |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Renders all data models. |
82
|
|
|
* @return string the rendering result |
83
|
|
|
*/ |
84
|
7 |
|
public function renderItems() |
85
|
|
|
{ |
86
|
7 |
|
$models = $this->dataProvider->getModels(); |
87
|
7 |
|
$keys = $this->dataProvider->getKeys(); |
88
|
7 |
|
$rows = []; |
89
|
7 |
|
foreach (array_values($models) as $index => $model) { |
90
|
7 |
|
$rows[] = $this->renderItem($model, $keys[$index], $index); |
91
|
7 |
|
} |
92
|
|
|
|
93
|
7 |
|
return implode($this->separator, $rows); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Renders a single data model. |
98
|
|
|
* @param mixed $model the data model to be rendered |
99
|
|
|
* @param mixed $key the key value associated with the data model |
100
|
|
|
* @param int $index the zero-based index of the data model in the model array returned by [[dataProvider]]. |
101
|
|
|
* @return string the rendering result |
102
|
|
|
*/ |
103
|
7 |
|
public function renderItem($model, $key, $index) |
104
|
|
|
{ |
105
|
7 |
|
if ($this->itemView === null) { |
106
|
5 |
|
$content = $key; |
107
|
7 |
|
} elseif (is_string($this->itemView)) { |
108
|
1 |
|
$content = $this->getView()->render($this->itemView, array_merge([ |
109
|
1 |
|
'model' => $model, |
110
|
1 |
|
'key' => $key, |
111
|
1 |
|
'index' => $index, |
112
|
1 |
|
'widget' => $this, |
113
|
1 |
|
], $this->viewParams)); |
114
|
1 |
|
} else { |
115
|
1 |
|
$content = call_user_func($this->itemView, $model, $key, $index, $this); |
116
|
|
|
} |
117
|
7 |
|
if ($this->itemOptions instanceof Closure) { |
118
|
1 |
|
$options = call_user_func($this->itemOptions, $model, $key, $index, $this); |
119
|
1 |
|
} else { |
120
|
6 |
|
$options = $this->itemOptions; |
121
|
|
|
} |
122
|
7 |
|
$tag = ArrayHelper::remove($options, 'tag', 'div'); |
123
|
7 |
|
$options['data-key'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : (string) $key; |
124
|
|
|
|
125
|
7 |
|
return Html::tag($tag, $content, $options); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|