ListView.php 2.82 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\widgets;

use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

/**
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
19
class ListView extends BaseListView
Qiang Xue committed
20
{
Qiang Xue committed
21
	/**
22
	 * @var array the HTML attributes for the container of the rendering result of each data model.
Qiang Xue committed
23 24 25
	 * The "tag" element specifies the tag name of the container element and defaults to "div".
	 * If "tag" is false, it means no container element will be rendered.
	 */
Alexander Makarov committed
26
	public $itemOptions = [];
Qiang Xue committed
27 28 29 30 31
	/**
	 * @var string|callback the name of the view for rendering each data item, or a callback (e.g. an anonymous function)
	 * for rendering each data item. If it specifies a view name, the following variables will
	 * be available in the view:
	 *
32
	 * - `$model`: mixed, the data model
Qiang Xue committed
33 34 35 36 37 38 39 40 41
	 * - `$key`: mixed, the key value associated with the data item
	 * - `$index`: integer, the zero-based index of the data item in the items array returned by [[dataProvider]].
	 * - `$widget`: ListView, this widget instance
	 *
	 * Note that the view name is resolved into the view file by the current context of the [[view]] object.
	 *
	 * If this property is specified as a callback, it should have the following signature:
	 *
	 * ~~~
42
	 * function ($model, $key, $index, $widget)
Qiang Xue committed
43 44 45
	 * ~~~
	 */
	public $itemView;
Qiang Xue committed
46 47 48 49 50 51 52
	/**
	 * @var string the HTML code to be displayed between any two consecutive items.
	 */
	public $separator = "\n";


	/**
53
	 * Renders all data models.
Qiang Xue committed
54 55 56 57
	 * @return string the rendering result
	 */
	public function renderItems()
	{
58
		$models = $this->dataProvider->getModels();
Qiang Xue committed
59
		$keys = $this->dataProvider->getKeys();
Alexander Makarov committed
60
		$rows = [];
61 62
		foreach (array_values($models) as $index => $model) {
			$rows[] = $this->renderItem($model, $keys[$index], $index);
Qiang Xue committed
63 64 65 66 67
		}
		return implode($this->separator, $rows);
	}

	/**
68 69 70 71
	 * 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]].
Qiang Xue committed
72 73
	 * @return string the rendering result
	 */
74
	public function renderItem($model, $key, $index)
Qiang Xue committed
75
	{
Qiang Xue committed
76
		if ($this->itemView === null) {
Qiang Xue committed
77
			$content = $key;
Qiang Xue committed
78
		} elseif (is_string($this->itemView)) {
Alexander Makarov committed
79
			$content = $this->getView()->render($this->itemView, [
80
				'model' => $model,
Qiang Xue committed
81 82 83
				'key' => $key,
				'index' => $index,
				'widget' => $this,
Alexander Makarov committed
84
			]);
Qiang Xue committed
85
		} else {
86
			$content = call_user_func($this->itemView, $model, $key, $index, $this);
Qiang Xue committed
87 88 89 90 91 92 93 94 95 96 97
		}
		$options = $this->itemOptions;
		$tag = ArrayHelper::remove($options, 'tag', 'div');
		if ($tag !== false) {
			$options['data-key'] = $key;
			return Html::tag($tag, $content, $options);
		} else {
			return $content;
		}
	}
}