Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GridView игнорирует заголовки полей, если таблица пуста. #9950

Closed
m00nk opened this issue Oct 17, 2015 · 10 comments

Comments

@m00nk
Copy link

m00nk commented Oct 17, 2015

Если в выводимой таблице есть содержимое, то заголовки столбцов берутся из массива, который возвращает Model::attributeLabels(), но если таблица выводится пустой (одна строка "Ничего не найдено."), то в заголовки столбцов попадают просто имена полей из БД, т.е. значение из Model::attributeLabels() просто игнорируется.

grid-view-bug

@samdark
Copy link
Member

samdark commented Oct 18, 2015

Есть идеи, как это поправить?

@ashkarpetin
Copy link
Contributor

Could't reproduce. Is i18n component properly configured and language files placed in right directories?

@SilverFire
Copy link
Member

Also failed to reproduce. @m00nk, could you show code from the view?

@flowip
Copy link

flowip commented Oct 19, 2015

I have the same issue. It's pretty easy to reproduce. I'm using ArrayDataProvider with array of models. When array is empty, attributeLabels method is not used, because it's not even possible for GridView to know which model to use.

@samdark
Copy link
Member

samdark commented Oct 19, 2015

@flowip for ArrayDataProvider that's understandable. Original issue seems to be about ActiveDataProvider.

@flowip
Copy link

flowip commented Oct 19, 2015

Maybe, @m00nk didn't mention that. I myself have to create empty model object, get labels from it and pass labels to view.

@samdark
Copy link
Member

samdark commented Oct 19, 2015

@m00nk ?

@m00nk
Copy link
Author

m00nk commented Nov 22, 2015

Yes, I use ArrayDataProvider and GridView.

echo \yii\grid\GridView::widget([
    'layout' => '{items}{pager}',
    'dataProvider' => new \yii\data\ArrayDataProvider([
        'allModels' => SitemapNode::find()-> /* ... */ ->all(),
        'pagination' => ['pageSize' => 20]
    ]),

    'columns' => [

        [
            'class' => \yii\grid\CheckboxColumn::className(),
            'checkboxOptions' => function ($model, $key, $index, $column)
            { return ['value' => $model->id, 'class' => 'node_checkbox'];   },
        ],

        'id',

        [
            'attribute' => 'title',
            'format' => 'raw',
            'contentOptions' => ['class' => 'column_title'],
            'value' => function ($model)
            {
                /** @var $model SitemapNode */
                return
                    $model->menu_title
                    .Html::a(
                        '<span class="pull-right glyphicon glyphicon-pencil"></span>',
                        ['edit', 'id' => $model->id],
                        ['title' => Yii::t('moduleBlog', 'Редактировать страницу')]
                    );
            }
        ],

        [
            'attribute' => 'alias',
            'format' => 'raw',
            'value' => function ($model)
            {
                /** @var $model SitemapNode */
                return
                    Html::a($model->alias, $model->getPageUrl(), ['title' => Yii::t('moduleBlog', 'Открыть страницу в новом окне'), 'target' => '_blank']);
            }
        ],
/* ... */
    ],
]);

@ar-turek
Copy link

ar-turek commented Apr 7, 2016

I have written a fairly elegant solution for that problem if you are using a Model cast to an array and a filterModel.

<?php

namespace app\base;

use yii\base\Model;
use yii\data\ArrayDataProvider;

class DataColumn extends \yii\grid\DataColumn
{
    /**
     * @inheritdoc
     */
    protected function renderHeaderCellContent()
    {
        $filterModel = $this->grid->filterModel;
        if ($this->label === null && $this->grid->dataProvider instanceof ArrayDataProvider && $filterModel instanceof Model) {
            $this->label = $filterModel->getAttributeLabel($this->attribute);
        }
        return parent::renderHeaderCellContent();
    }
}

Then it's enough to add
'dataColumnClass' => 'app\base\DataColumn',
to your GridView configuration.

@SilverFire
Copy link
Member

@artur-ciesielski looks good :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants