mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +08:00 
			
		
		
		
	Merge branch 'mdmunir-enhance_listview'
This commit is contained in:
		@ -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 #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 #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 #12744: Added `afterInit` event to `yii.activeForm.js` (werew01f)
 | 
				
			||||||
 | 
					- Enh #12710: Added `beforeItem` and `afterItem` to `yii\widgets\ListView` (mdmunir, silverfire)
 | 
				
			||||||
- Enh: Method `yii\console\controllers\AssetController::getAssetManager()` automatically enables `yii\web\AssetManager::forceCopy` in case it is not explicitly specified (pana1990, klimov-paul)
 | 
					- 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
 | 
					2.0.9 July 11, 2016
 | 
				
			||||||
 | 
				
			|||||||
@ -7,7 +7,6 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace yii\widgets;
 | 
					namespace yii\widgets;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use Yii;
 | 
					 | 
				
			||||||
use Closure;
 | 
					use Closure;
 | 
				
			||||||
use yii\helpers\ArrayHelper;
 | 
					use yii\helpers\ArrayHelper;
 | 
				
			||||||
use yii\helpers\Html;
 | 
					use yii\helpers\Html;
 | 
				
			||||||
@ -75,7 +74,32 @@ class ListView extends BaseListView
 | 
				
			|||||||
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
 | 
					     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public $options = ['class' => 'list-view'];
 | 
					    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.
 | 
				
			||||||
 | 
					     * @see renderBeforeItem
 | 
				
			||||||
 | 
					     * @since 2.0.11
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    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.
 | 
				
			||||||
 | 
					     * @see renderAfterItem
 | 
				
			||||||
 | 
					     * @since 2.0.11
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public $afterItem;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Renders all data models.
 | 
					     * Renders all data models.
 | 
				
			||||||
@ -87,12 +111,61 @@ class ListView extends BaseListView
 | 
				
			|||||||
        $keys = $this->dataProvider->getKeys();
 | 
					        $keys = $this->dataProvider->getKeys();
 | 
				
			||||||
        $rows = [];
 | 
					        $rows = [];
 | 
				
			||||||
        foreach (array_values($models) as $index => $model) {
 | 
					        foreach (array_values($models) as $index => $model) {
 | 
				
			||||||
            $rows[] = $this->renderItem($model, $keys[$index], $index);
 | 
					            $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);
 | 
					        return implode($this->separator, $rows);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Calls [[beforeItem]] closure, returns execution result.
 | 
				
			||||||
 | 
					     * If [[beforeItem]] is not a closure, `null` will be returned.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param mixed $model the data model to be rendered
 | 
				
			||||||
 | 
					     * @param mixed $key the key value associated with the data model
 | 
				
			||||||
 | 
					     * @param int $index the zero-based index of the data model in the model array returned by [[dataProvider]].
 | 
				
			||||||
 | 
					     * @return string|null [[beforeItem]] call result or `null` when [[beforeItem]] is not a closure
 | 
				
			||||||
 | 
					     * @see beforeItem
 | 
				
			||||||
 | 
					     * @since 2.0.11
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    protected function renderBeforeItem($model, $key, $index)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if ($this->beforeItem instanceof Closure) {
 | 
				
			||||||
 | 
					            return call_user_func($this->beforeItem, $model, $key, $index, $this);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return null;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Calls [[afterItem]] closure, returns execution result.
 | 
				
			||||||
 | 
					     * If [[afterItem]] is not a closure, `null` will be returned.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param mixed $model the data model to be rendered
 | 
				
			||||||
 | 
					     * @param mixed $key the key value associated with the data model
 | 
				
			||||||
 | 
					     * @param int $index the zero-based index of the data model in the model array returned by [[dataProvider]].
 | 
				
			||||||
 | 
					     * @return string|null [[afterItem]] call result or `null` when [[afterItem]] is not a closure
 | 
				
			||||||
 | 
					     * @see afterItem
 | 
				
			||||||
 | 
					     * @since 2.0.11
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    protected function renderAfterItem($model, $key, $index)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if ($this->afterItem instanceof Closure) {
 | 
				
			||||||
 | 
					            return call_user_func($this->afterItem, $model, $key, $index, $this);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return null;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Renders a single data model.
 | 
					     * Renders a single data model.
 | 
				
			||||||
     * @param mixed $model the data model to be rendered
 | 
					     * @param mixed $model the data model to be rendered
 | 
				
			||||||
 | 
				
			|||||||
@ -170,4 +170,35 @@ HTML
 | 
				
			|||||||
        $this->getListView(['itemOptions' => $itemOptions])->run();
 | 
					        $this->getListView(['itemOptions' => $itemOptions])->run();
 | 
				
			||||||
        $this->expectOutputString($expected);
 | 
					        $this->expectOutputString($expected);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function testBeforeAndAfterItem()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $before = function ($model, $key, $index, $widget) {
 | 
				
			||||||
 | 
					            $widget = get_class($widget);
 | 
				
			||||||
 | 
					            return "<!-- before: {$model['id']}, key: $key, index: $index, widget: $widget -->";
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					        $after = function ($model, $key, $index, $widget) {
 | 
				
			||||||
 | 
					            $widget = get_class($widget);
 | 
				
			||||||
 | 
					            return "<!-- after: {$model['id']}, key: $key, index: $index, widget: $widget -->";
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					        $this->getListView([
 | 
				
			||||||
 | 
					            'beforeItem' => $before,
 | 
				
			||||||
 | 
					            'afterItem' => $after
 | 
				
			||||||
 | 
					        ])->run();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->expectOutputString(<<<HTML
 | 
				
			||||||
 | 
					<div id="w0" class="list-view"><div class="summary">Showing <b>1-3</b> of <b>3</b> items.</div>
 | 
				
			||||||
 | 
					<!-- before: 1, key: 0, index: 0, widget: yii\widgets\ListView -->
 | 
				
			||||||
 | 
					<div data-key="0">0</div>
 | 
				
			||||||
 | 
					<!-- after: 1, key: 0, index: 0, widget: yii\widgets\ListView -->
 | 
				
			||||||
 | 
					<!-- before: 2, key: 1, index: 1, widget: yii\widgets\ListView -->
 | 
				
			||||||
 | 
					<div data-key="1">1</div>
 | 
				
			||||||
 | 
					<!-- after: 2, key: 1, index: 1, widget: yii\widgets\ListView -->
 | 
				
			||||||
 | 
					<!-- before: 3, key: 2, index: 2, widget: yii\widgets\ListView -->
 | 
				
			||||||
 | 
					<div data-key="2">2</div>
 | 
				
			||||||
 | 
					<!-- after: 3, key: 2, index: 2, widget: yii\widgets\ListView -->
 | 
				
			||||||
 | 
					</div>
 | 
				
			||||||
 | 
					HTML
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user