Browse Source

Refactor DataColum to use model label for sorting link

tags/2.0.0-beta
Carsten Brandt 12 years ago
parent
commit
c5d65e09af
  1. 32
      framework/yii/grid/DataColumn.php

32
framework/yii/grid/DataColumn.php

@ -62,25 +62,33 @@ class DataColumn extends Column
if ($this->attribute !== null && $this->header === null) { if ($this->attribute !== null && $this->header === null) {
$provider = $this->grid->dataProvider; $provider = $this->grid->dataProvider;
if ($this->enableSorting && ($sort = $provider->getSort()) !== false && $sort->hasAttribute($this->attribute)) { if ($this->enableSorting && ($sort = $provider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
return $sort->link($this->attribute); if (!isset($sort->attributes[$this->attribute]['label'])) {
} $sort->attributes[$this->attribute]['label'] = $this->getHeaderLabel();
$models = $provider->getModels();
if (($model = reset($models)) instanceof Model) {
/** @var Model $model */
return $model->getAttributeLabel($this->attribute);
} elseif ($provider instanceof ActiveDataProvider) {
if ($provider->query instanceof ActiveQuery) {
/** @var Model $model */
$model = new $provider->query->modelClass;
return $model->getAttributeLabel($this->attribute);
} }
return $sort->link($this->attribute);
} }
return Inflector::camel2words($this->attribute); return $this->getHeaderLabel();
} else { } else {
return parent::renderHeaderCellContent(); return parent::renderHeaderCellContent();
} }
} }
protected function getHeaderLabel()
{
$provider = $this->grid->dataProvider;
if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQuery) {
/** @var Model $model */
$model = new $provider->query->modelClass;
return $model->getAttributeLabel($this->attribute);
}
$models = $provider->getModels();
if (($model = reset($models)) instanceof Model) {
/** @var Model $model */
return $model->getAttributeLabel($this->attribute);
}
return Inflector::camel2words($this->attribute);
}
protected function renderFilterCellContent() protected function renderFilterCellContent()
{ {
if (is_string($this->filter)) { if (is_string($this->filter)) {

Loading…
Cancel
Save