ListView.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. namespace yii\widgets;
  8. use Closure;
  9. use yii\helpers\ArrayHelper;
  10. use yii\helpers\Html;
  11. /**
  12. * The ListView widget is used to display data from data
  13. * provider. Each data model is rendered using the view
  14. * specified.
  15. *
  16. * For more details and usage information on ListView, see the [guide article on data widgets](guide:output-data-widgets).
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @since 2.0
  20. */
  21. class ListView extends BaseListView
  22. {
  23. /**
  24. * @var array|Closure the HTML attributes for the container of the rendering result of each data model.
  25. * This can be either an array specifying the common HTML attributes for rendering each data item,
  26. * or an anonymous function that returns an array of the HTML attributes. The anonymous function will be
  27. * called once for every data model returned by [[dataProvider]].
  28. * The "tag" element specifies the tag name of the container element and defaults to "div".
  29. * If "tag" is false, it means no container element will be rendered.
  30. *
  31. * If this property is specified as an anonymous function, it should have the following signature:
  32. *
  33. * ```php
  34. * function ($model, $key, $index, $widget)
  35. * ```
  36. *
  37. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  38. */
  39. public $itemOptions = [];
  40. /**
  41. * @var string|callable the name of the view for rendering each data item, or a callback (e.g. an anonymous function)
  42. * for rendering each data item. If it specifies a view name, the following variables will
  43. * be available in the view:
  44. *
  45. * - `$model`: mixed, the data model
  46. * - `$key`: mixed, the key value associated with the data item
  47. * - `$index`: integer, the zero-based index of the data item in the items array returned by [[dataProvider]].
  48. * - `$widget`: ListView, this widget instance
  49. *
  50. * Note that the view name is resolved into the view file by the current context of the [[view]] object.
  51. *
  52. * If this property is specified as a callback, it should have the following signature:
  53. *
  54. * ```php
  55. * function ($model, $key, $index, $widget)
  56. * ```
  57. */
  58. public $itemView;
  59. /**
  60. * @var array additional parameters to be passed to [[itemView]] when it is being rendered.
  61. * This property is used only when [[itemView]] is a string representing a view name.
  62. */
  63. public $viewParams = [];
  64. /**
  65. * @var string the HTML code to be displayed between any two consecutive items.
  66. */
  67. public $separator = "\n";
  68. /**
  69. * @var array the HTML attributes for the container tag of the list view.
  70. * The "tag" element specifies the tag name of the container element and defaults to "div".
  71. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  72. */
  73. public $options = ['class' => 'list-view'];
  74. /**
  75. * @var Closure an anonymous function that is called once BEFORE rendering each data model.
  76. * It should have the following signature:
  77. *
  78. * ```php
  79. * function ($model, $key, $index, $widget)
  80. * ```
  81. *
  82. * - `$model`: the current data model being rendered
  83. * - `$key`: the key value associated with the current data model
  84. * - `$index`: the zero-based index of the data model in the model array returned by [[dataProvider]]
  85. * - `$widget`: the ListView object
  86. *
  87. * The return result of the function will be rendered directly.
  88. * Note: If the function returns `null`, nothing will be rendered before the item.
  89. * @see renderBeforeItem
  90. * @since 2.0.11
  91. */
  92. public $beforeItem;
  93. /**
  94. * @var Closure an anonymous function that is called once AFTER rendering each data model.
  95. *
  96. * It should have the same signature as [[beforeItem]].
  97. *
  98. * The return result of the function will be rendered directly.
  99. * Note: If the function returns `null`, nothing will be rendered after the item.
  100. * @see renderAfterItem
  101. * @since 2.0.11
  102. */
  103. public $afterItem;
  104. /**
  105. * Renders all data models.
  106. * @return string the rendering result
  107. */
  108. public function renderItems()
  109. {
  110. $models = $this->dataProvider->getModels();
  111. $keys = $this->dataProvider->getKeys();
  112. $rows = [];
  113. foreach (array_values($models) as $index => $model) {
  114. $key = $keys[$index];
  115. if (($before = $this->renderBeforeItem($model, $key, $index)) !== null) {
  116. $rows[] = $before;
  117. }
  118. $rows[] = $this->renderItem($model, $key, $index);
  119. if (($after = $this->renderAfterItem($model, $key, $index)) !== null) {
  120. $rows[] = $after;
  121. }
  122. }
  123. return implode($this->separator, $rows);
  124. }
  125. /**
  126. * Calls [[beforeItem]] closure, returns execution result.
  127. * If [[beforeItem]] is not a closure, `null` will be returned.
  128. *
  129. * @param mixed $model the data model to be rendered
  130. * @param mixed $key the key value associated with the data model
  131. * @param int $index the zero-based index of the data model in the model array returned by [[dataProvider]].
  132. * @return string|null [[beforeItem]] call result or `null` when [[beforeItem]] is not a closure
  133. * @see beforeItem
  134. * @since 2.0.11
  135. */
  136. protected function renderBeforeItem($model, $key, $index)
  137. {
  138. if ($this->beforeItem instanceof Closure) {
  139. return call_user_func($this->beforeItem, $model, $key, $index, $this);
  140. }
  141. return null;
  142. }
  143. /**
  144. * Calls [[afterItem]] closure, returns execution result.
  145. * If [[afterItem]] is not a closure, `null` will be returned.
  146. *
  147. * @param mixed $model the data model to be rendered
  148. * @param mixed $key the key value associated with the data model
  149. * @param int $index the zero-based index of the data model in the model array returned by [[dataProvider]].
  150. * @return string|null [[afterItem]] call result or `null` when [[afterItem]] is not a closure
  151. * @see afterItem
  152. * @since 2.0.11
  153. */
  154. protected function renderAfterItem($model, $key, $index)
  155. {
  156. if ($this->afterItem instanceof Closure) {
  157. return call_user_func($this->afterItem, $model, $key, $index, $this);
  158. }
  159. return null;
  160. }
  161. /**
  162. * Renders a single data model.
  163. * @param mixed $model the data model to be rendered
  164. * @param mixed $key the key value associated with the data model
  165. * @param int $index the zero-based index of the data model in the model array returned by [[dataProvider]].
  166. * @return string the rendering result
  167. */
  168. public function renderItem($model, $key, $index)
  169. {
  170. if ($this->itemView === null) {
  171. $content = $key;
  172. } elseif (is_string($this->itemView)) {
  173. $content = $this->getView()->render($this->itemView, array_merge([
  174. 'model' => $model,
  175. 'key' => $key,
  176. 'index' => $index,
  177. 'widget' => $this,
  178. ], $this->viewParams));
  179. } else {
  180. $content = call_user_func($this->itemView, $model, $key, $index, $this);
  181. }
  182. if ($this->itemOptions instanceof Closure) {
  183. $options = call_user_func($this->itemOptions, $model, $key, $index, $this);
  184. } else {
  185. $options = $this->itemOptions;
  186. }
  187. $tag = ArrayHelper::remove($options, 'tag', 'div');
  188. $options['data-key'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : (string) $key;
  189. return Html::tag($tag, $content, $options);
  190. }
  191. }