From 743d31a8e65457e333a4f6cdcb72df9ea6f4b6cf Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 10 Nov 2013 21:07:13 -0500 Subject: [PATCH] Fixes #1075: separated empty switch and empty display for ListView and GridView. --- framework/yii/grid/GridView.php | 13 +++++++++---- framework/yii/widgets/BaseListView.php | 26 +++++++++++++++++++++----- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/framework/yii/grid/GridView.php b/framework/yii/grid/GridView.php index 2981c82..8ba8c0a 100644 --- a/framework/yii/grid/GridView.php +++ b/framework/yii/grid/GridView.php @@ -89,10 +89,9 @@ class GridView extends BaseListView */ public $showFooter = false; /** - * @var string|boolean the HTML content to be displayed when [[dataProvider]] does not have any data. - * If false, the grid view will still be displayed (without body content though). + * @var boolean whether to show the grid view if [[dataProvider]] returns no data. */ - public $empty = false; + public $showOnEmpty = true; /** * @var array|Formatter the formatter used to format model attribute values into displayable texts. * This can be either an instance of [[Formatter]] or an configuration array for creating the [[Formatter]] @@ -342,7 +341,13 @@ class GridView extends BaseListView } } } - return "\n" . implode("\n", $rows) . "\n"; + + if (empty($rows)) { + return "\n" . implode("\n", $rows) . "\n"; + } else { + $colspan = count($this->columns); + return "\n" . $this->renderEmpty() . "\n"; + } } /** diff --git a/framework/yii/widgets/BaseListView.php b/framework/yii/widgets/BaseListView.php index 310201a..ec9c1cb 100644 --- a/framework/yii/widgets/BaseListView.php +++ b/framework/yii/widgets/BaseListView.php @@ -53,10 +53,13 @@ abstract class BaseListView extends Widget */ public $summary; /** - * @var string|boolean the HTML content to be displayed when [[dataProvider]] does not have any data. - * If false, the list view will still be displayed (without body content though). + * @var boolean whether to show the list view if [[dataProvider]] returns no data. */ - public $empty; + public $showOnEmpty = false; + /** + * @var string the HTML content to be displayed when [[dataProvider]] does not have any data. + */ + public $emptyText; /** * @var string the layout that determines how different sections of the list view should be organized. * The following tokens will be replaced with the corresponding section contents: @@ -83,6 +86,9 @@ abstract class BaseListView extends Widget if ($this->dataProvider === null) { throw new InvalidConfigException('The "dataProvider" property must be set.'); } + if ($this->emptyText === null) { + $this->emptyText = Yii::t('yii', 'No results found.'); + } $this->dataProvider->prepare(); } @@ -91,13 +97,13 @@ abstract class BaseListView extends Widget */ public function run() { - if ($this->dataProvider->getCount() > 0 || $this->empty === false) { + if ($this->dataProvider->getCount() > 0 || $this->showOnEmpty) { $content = preg_replace_callback("/{\\w+}/", function ($matches) { $content = $this->renderSection($matches[0]); return $content === false ? $matches[0] : $content; }, $this->layout); } else { - $content = '
' . ($this->empty === null ? Yii::t('yii', 'No results found.') : $this->empty) . '
'; + $content = $this->renderEmpty(); } $tag = ArrayHelper::remove($this->options, 'tag', 'div'); echo Html::tag($tag, $content, $this->options); @@ -126,6 +132,16 @@ abstract class BaseListView extends Widget } /** + * Renders the HTML content indicating that the list view has no data. + * @return string the rendering result + * @see emptyText + */ + public function renderEmpty() + { + return '
' . ($this->emptyText === null ? Yii::t('yii', 'No results found.') : $this->emptyText) . '
'; + } + + /** * Renders the summary text. */ public function renderSummary()