Browse Source

Do not render empty row in GridView when data is empty and emptyText set to false

Fixes #13352
close #13422
tags/2.0.12
Alexey Rogachev 8 years ago committed by Carsten Brandt
parent
commit
80d6f7323a
No known key found for this signature in database
GPG Key ID: BE4F41DE1DEEEED0
  1. 1
      framework/CHANGELOG.md
  2. 2
      framework/grid/GridView.php
  3. 13
      framework/widgets/BaseListView.php
  4. 60
      tests/framework/grid/GridViewTest.php
  5. 10
      tests/framework/widgets/ListViewTest.php

1
framework/CHANGELOG.md

@ -236,6 +236,7 @@ Yii Framework 2 Change Log
- Enh #13268: Added logging of memory usage (bashkarev)
- Enh #13417: Allow customizing `yii\data\ActiveDataProvider` in `yii\rest\IndexAction` (leandrogehlen)
- Enh #13453: Select only primary key when counting records in UniqueValidator (developeruz)
- Enh #13352: Added option to not render empty row in `yii\grid\GridView` when data is empty and `emptyText` set to `false` (arogachev)
- Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe)
- Enh: Refactored `yii\web\ErrorAction` to make it reusable (silverfire)
- Enh: Added support for field `yii\console\controllers\BaseMigrateController::$migrationNamespaces` setup from CLI (schmunk42)

2
framework/grid/GridView.php

@ -483,7 +483,7 @@ class GridView extends BaseListView
}
}
if (empty($rows)) {
if (empty($rows) && $this->emptyText !== false) {
$colspan = count($this->columns);
return "<tbody>\n<tr><td colspan=\"$colspan\">" . $this->renderEmpty() . "</td></tr>\n</tbody>";

13
framework/widgets/BaseListView.php

@ -72,12 +72,16 @@ abstract class BaseListView extends Widget
public $summaryOptions = ['class' => 'summary'];
/**
* @var bool whether to show an empty list view if [[dataProvider]] returns no data.
* The default value is false which displays an element according to the `emptyText`
* and `emptyTextOptions` properties.
* The default value is false which displays an element according to the [[emptyText]]
* and [[emptyTextOptions]] properties.
*/
public $showOnEmpty = false;
/**
* @var string the HTML content to be displayed when [[dataProvider]] does not have any data.
* @var string|false the HTML content to be displayed when [[dataProvider]] does not have any data.
* When this is set to `false` no extra HTML content will be generated.
* The default value is the text "No results found." which will be translated to the current application language.
* @see showOnEmpty
* @see emptyTextOptions
*/
public $emptyText;
/**
@ -169,6 +173,9 @@ abstract class BaseListView extends Widget
*/
public function renderEmpty()
{
if ($this->emptyText === false) {
return '';
}
$options = $this->emptyTextOptions;
$tag = ArrayHelper::remove($options, 'tag', 'div');
return Html::tag($tag, $this->emptyText, $options);

60
tests/framework/grid/GridViewTest.php

@ -5,6 +5,7 @@ namespace yiiunit\framework\grid;
use yii\data\ArrayDataProvider;
use yii\grid\DataColumn;
use yii\web\View;
use yii\grid\GridView;
/**
@ -13,9 +14,66 @@ use yii\grid\GridView;
*/
class GridViewTest extends \yiiunit\TestCase
{
protected function setUp()
{
parent::setUp();
$this->mockApplication([
'components' => [
'assetManager' => [
'bundles' => [
'yii\grid\GridViewAsset' => false,
'yii\web\JqueryAsset' => false,
],
],
],
]);
}
/**
* @return array
*/
public function emptyDataProvider()
{
return [
[null, 'No results found.'],
['Empty', 'Empty'],
// https://github.com/yiisoft/yii2/issues/13352
[false, ''],
];
}
/**
* @dataProvider emptyDataProvider
* @param mixed $emptyText
* @param string $expectedText
* @throws \Exception
*/
public function testEmpty($emptyText, $expectedText)
{
$html = GridView::widget([
'id' => 'grid',
'dataProvider' => new ArrayDataProvider(['allModels' => []]),
'showHeader' => false,
'emptyText' => $emptyText,
'options' => [],
'tableOptions' => [],
'view' => new View(),
'filterUrl' => '/',
]);
$html = preg_replace("/\r|\n/", '', $html);
if ($expectedText) {
$emptyRowHtml = "<tr><td colspan=\"0\"><div class=\"empty\">{$expectedText}</div></td></tr>";
} else {
$emptyRowHtml = '';
}
$expectedHtml = "<div id=\"grid\"><table><tbody>{$emptyRowHtml}</tbody></table></div>";
$this->assertEquals($expectedHtml, $html);
}
public function testGuessColumns()
{
$this->mockApplication();
$row = ['id' => 1, 'name' => 'Name1', 'value' => 'Value1', 'description' => 'Description1',];
$grid = new GridView([

10
tests/framework/widgets/ListViewTest.php

@ -28,6 +28,16 @@ class ListViewTest extends \yiiunit\TestCase
$this->expectOutputString('<div id="w0" class="list-view"><div class="empty">Nothing at all</div></div>');
}
public function testEmpty()
{
$this->getListView([
'dataProvider' => new ArrayDataProvider(['allModels' => []]),
'emptyText' => false,
])->run();
$this->expectOutputString('<div id="w0" class="list-view"></div>');
}
public function testEmptyListNotShown()
{
$this->getListView([

Loading…
Cancel
Save