123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- namespace yii\widgets;
- use Closure;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Html;
- class ListView extends BaseListView
- {
-
- public $itemOptions = [];
-
- public $itemView;
-
- public $viewParams = [];
-
- public $separator = "\n";
-
- public $options = ['class' => 'list-view'];
-
- public $beforeItem;
-
- public $afterItem;
-
- public function renderItems()
- {
- $models = $this->dataProvider->getModels();
- $keys = $this->dataProvider->getKeys();
- $rows = [];
- foreach (array_values($models) as $index => $model) {
- $key = $keys[$index];
- if (($before = $this->renderBeforeItem($model, $key, $index)) !== null) {
- $rows[] = $before;
- }
- $rows[] = $this->renderItem($model, $key, $index);
- if (($after = $this->renderAfterItem($model, $key, $index)) !== null) {
- $rows[] = $after;
- }
- }
- return implode($this->separator, $rows);
- }
-
- protected function renderBeforeItem($model, $key, $index)
- {
- if ($this->beforeItem instanceof Closure) {
- return call_user_func($this->beforeItem, $model, $key, $index, $this);
- }
- return null;
- }
-
- protected function renderAfterItem($model, $key, $index)
- {
- if ($this->afterItem instanceof Closure) {
- return call_user_func($this->afterItem, $model, $key, $index, $this);
- }
- return null;
- }
-
- 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);
- }
- if ($this->itemOptions instanceof Closure) {
- $options = call_user_func($this->itemOptions, $model, $key, $index, $this);
- } else {
- $options = $this->itemOptions;
- }
- $tag = ArrayHelper::remove($options, 'tag', 'div');
- $options['data-key'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : (string) $key;
- return Html::tag($tag, $content, $options);
- }
- }
|