MyListView.php
2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace frontend\components;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\ListView;
class MyListView extends ListView
{
public $itemOptions = [];
public $itemView;
/**
* @var array additional parameters to be passed to [[itemView]] when it is being rendered.
* This property is used only when [[itemView]] is a string representing a view name.
*/
public $viewParams = [];
/**
* @var string the HTML code to be displayed between any two consecutive items.
*/
public $separator = "\n";
/**
* @var array the HTML attributes for the container tag of the list view.
* The "tag" element specifies the tag name of the container element and defaults to "div".
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = ['class' => 'list-view'];
/**
* Renders all data models.
* @return string the rendering result
*/
public function renderItems()
{
$models = $this->dataProvider->getModels();
$keys = $this->dataProvider->getKeys();
$rows = [];
foreach (array_values($models) as $index => $model) {
$rows[] = $this->renderItem($model, $keys[$index], $index);
}
return implode($this->separator, $rows);
}
/**
* Renders a single data model.
* @param mixed $model the data model to be rendered
* @param mixed $key the key value associated with the data model
* @param integer $index the zero-based index of the data model in the model array returned by [[dataProvider]].
* @return string the rendering result
*/
public function renderItem($model, $key, $index)
{
if ($this->itemView === null) {
$content = $key;
} elseif (is_string($this->itemView)) {
$content = $this->getView()->render($this->itemView, array_merge([
'model' => $model,
'key' => $key,
'index' => $index,
'widget' => $this,
], $this->viewParams));
} else {
$content = call_user_func($this->itemView, $model, $key, $index, $this);
}
return $content;
}
}