Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

111 lines
4.0KB

  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 Yii;
  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. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @since 2.0
  18. */
  19. class ListView extends BaseListView
  20. {
  21. /**
  22. * @var array the HTML attributes for the container of the rendering result of each data model.
  23. * The "tag" element specifies the tag name of the container element and defaults to "div".
  24. * If "tag" is false, it means no container element will be rendered.
  25. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  26. */
  27. public $itemOptions = [];
  28. /**
  29. * @var string|callable the name of the view for rendering each data item, or a callback (e.g. an anonymous function)
  30. * for rendering each data item. If it specifies a view name, the following variables will
  31. * be available in the view:
  32. *
  33. * - `$model`: mixed, the data model
  34. * - `$key`: mixed, the key value associated with the data item
  35. * - `$index`: integer, the zero-based index of the data item in the items array returned by [[dataProvider]].
  36. * - `$widget`: ListView, this widget instance
  37. *
  38. * Note that the view name is resolved into the view file by the current context of the [[view]] object.
  39. *
  40. * If this property is specified as a callback, it should have the following signature:
  41. *
  42. * ```php
  43. * function ($model, $key, $index, $widget)
  44. * ```
  45. */
  46. public $itemView;
  47. /**
  48. * @var array additional parameters to be passed to [[itemView]] when it is being rendered.
  49. * This property is used only when [[itemView]] is a string representing a view name.
  50. */
  51. public $viewParams = [];
  52. /**
  53. * @var string the HTML code to be displayed between any two consecutive items.
  54. */
  55. public $separator = "\n";
  56. /**
  57. * @var array the HTML attributes for the container tag of the list view.
  58. * The "tag" element specifies the tag name of the container element and defaults to "div".
  59. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  60. */
  61. public $options = ['class' => 'list-view'];
  62. /**
  63. * Renders all data models.
  64. * @return string the rendering result
  65. */
  66. public function renderItems()
  67. {
  68. $models = $this->dataProvider->getModels();
  69. $keys = $this->dataProvider->getKeys();
  70. $rows = [];
  71. foreach (array_values($models) as $index => $model) {
  72. $rows[] = $this->renderItem($model, $keys[$index], $index);
  73. }
  74. return implode($this->separator, $rows);
  75. }
  76. /**
  77. * Renders a single data model.
  78. * @param mixed $model the data model to be rendered
  79. * @param mixed $key the key value associated with the data model
  80. * @param integer $index the zero-based index of the data model in the model array returned by [[dataProvider]].
  81. * @return string the rendering result
  82. */
  83. public function renderItem($model, $key, $index)
  84. {
  85. if ($this->itemView === null) {
  86. $content = $key;
  87. } elseif (is_string($this->itemView)) {
  88. $content = $this->getView()->render($this->itemView, array_merge([
  89. 'model' => $model,
  90. 'key' => $key,
  91. 'index' => $index,
  92. 'widget' => $this,
  93. ], $this->viewParams));
  94. } else {
  95. $content = call_user_func($this->itemView, $model, $key, $index, $this);
  96. }
  97. $options = $this->itemOptions;
  98. $tag = ArrayHelper::remove($options, 'tag', 'div');
  99. $options['data-key'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : (string) $key;
  100. return Html::tag($tag, $content, $options);
  101. }
  102. }