Browse Source

Add `beforeItem` and `afterItem` to the `ListView'

added changelog
tags/2.0.11
mdmunir 8 years ago committed by SilverFire - Dmitry Naumenko
parent
commit
cc2e6bcd56
No known key found for this signature in database
GPG Key ID: 39DD917A92B270A
  1. 1
      framework/CHANGELOG.md
  2. 41
      framework/widgets/ListView.php

1
framework/CHANGELOG.md

@ -172,6 +172,7 @@ Yii Framework 2 Change Log
- Enh #12612: Query conditions added with `yii\db\Query::andWhere()` now get appended to the existing conditions if they were already being joined with the `and` operator (brandonkelly)
- Enh #12664: Added support for wildcards for `optional` at `yii\filters\auth\AuthMethod` (mg-code)
- Enh #12744: Added `afterInit` event to `yii.activeForm.js` (werew01f)
- Enh #12710: Added `beforeItem` and `afterItem` at `yii\widgets\ListView` (mdmunir)
- Enh: Method `yii\console\controllers\AssetController::getAssetManager()` automatically enables `yii\web\AssetManager::forceCopy` in case it is not explicitly specified (pana1990, klimov-paul)
2.0.9 July 11, 2016

41
framework/widgets/ListView.php

@ -75,6 +75,30 @@ class ListView extends BaseListView
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = ['class' => 'list-view'];
/**
* @var Closure an anonymous function that is called once BEFORE rendering each data model.
* It should have the following signature:
*
* ```php
* function ($model, $key, $index, $widget)
* ```
*
* - `$model`: the current data model being rendered
* - `$key`: the key value associated with the current data model
* - `$index`: the zero-based index of the data model in the model array returned by [[dataProvider]]
* - `$widget`: the ListView object
*
* The return result of the function will be rendered directly.
* @since 2.0.10
*/
public $beforeItem;
/**
* @var Closure an anonymous function that is called once AFTER rendering each data model.
* It should have the similar signature as [[beforeItem]]. The return result of the function
* will be rendered directly.
* @since 2.0.10
*/
public $afterItem;
/**
@ -87,7 +111,22 @@ class ListView extends BaseListView
$keys = $this->dataProvider->getKeys();
$rows = [];
foreach (array_values($models) as $index => $model) {
$rows[] = $this->renderItem($model, $keys[$index], $index);
$key = $keys[$index];
if ($this->beforeItem !== null) {
$row = call_user_func($this->beforeItem, $model, $key, $index, $this);
if ($row !== null && $row !== false) {
$rows[] = $row;
}
}
$rows[] = $this->renderItem($model, $key, $index);
if ($this->afterItem !== null) {
$row = call_user_func($this->afterItem, $model, $key, $index, $this);
if ($row !== null && $row !== false) {
$rows[] = $row;
}
}
}
return implode($this->separator, $rows);

Loading…
Cancel
Save