Commit 743d31a8 by Qiang Xue

Fixes #1075: separated empty switch and empty display for ListView and GridView.

parent de062c26
......@@ -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
}
}
}
if (empty($rows)) {
return "<tbody>\n" . implode("\n", $rows) . "\n</tbody>";
} else {
$colspan = count($this->columns);
return "<tbody>\n<tr><td colspan=\"$colspan\">" . $this->renderEmpty() . "</td></tr>\n</tbody>";
}
}
/**
......
......@@ -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 = '<div class="empty">' . ($this->empty === null ? Yii::t('yii', 'No results found.') : $this->empty) . '</div>';
$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 '<div class="empty">' . ($this->emptyText === null ? Yii::t('yii', 'No results found.') : $this->emptyText) . '</div>';
}
/**
* Renders the summary text.
*/
public function renderSummary()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment