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
Comments
Есть идеи, как это поправить? |
Could't reproduce. Is i18n component properly configured and language files placed in right directories? |
Also failed to reproduce. @m00nk, could you show code from the view? |
I have the same issue. It's pretty easy to reproduce. I'm using |
@flowip for |
Maybe, @m00nk didn't mention that. I myself have to create empty model object, get labels from it and pass labels to view. |
@m00nk ? |
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']);
}
],
/* ... */
],
]);
|
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 |
@artur-ciesielski looks good :) |
Если в выводимой таблице есть содержимое, то заголовки столбцов берутся из массива, который возвращает Model::attributeLabels(), но если таблица выводится пустой (одна строка "Ничего не найдено."), то в заголовки столбцов попадают просто имена полей из БД, т.е. значение из Model::attributeLabels() просто игнорируется.
The text was updated successfully, but these errors were encountered: