Skip to content

Commit

Permalink
Refactor DataColum to use model label for sorting link
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed Aug 24, 2013
1 parent f9fab5d commit c5d65e0
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions framework/yii/grid/DataColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,33 @@ protected function renderHeaderCellContent()
if ($this->attribute !== null && $this->header === null) {
$provider = $this->grid->dataProvider;
if ($this->enableSorting && ($sort = $provider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
return $sort->link($this->attribute);
}
$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);
if (!isset($sort->attributes[$this->attribute]['label'])) {
$sort->attributes[$this->attribute]['label'] = $this->getHeaderLabel();
}
return $sort->link($this->attribute);
}
return Inflector::camel2words($this->attribute);
return $this->getHeaderLabel();
} else {
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()
{
if (is_string($this->filter)) {
Expand Down

0 comments on commit c5d65e0

Please sign in to comment.