From 9847472144ee99e6a129ad6e53dfee419e6aafb8 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Mon, 26 Aug 2013 15:13:16 +0200 Subject: [PATCH] introduced DataColumn::label property --- framework/yii/grid/DataColumn.php | 67 +++++++++++++++++++++++---------------- framework/yii/grid/GridView.php | 12 +++---- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/framework/yii/grid/DataColumn.php b/framework/yii/grid/DataColumn.php index e1913c3..2b9c0be 100644 --- a/framework/yii/grid/DataColumn.php +++ b/framework/yii/grid/DataColumn.php @@ -28,6 +28,14 @@ class DataColumn extends Column */ public $attribute; /** + * @var string label to be displayed in the [[header|header cell]] and also to be used as the sorting + * link label when sorting is enabled for this column. + * If it is not set and the models provided by the GridViews data provider are instances + * of [[ActiveRecord]], the label will be determined via [[ActiveRecord::getAttributeLabel()]]. + * Otherwise [[yii\helpers\Inflector::camel2words]] will be used to get a label. + */ + public $label; + /** * @var \Closure an anonymous function that returns the value to be displayed for every data model. * If this is not set, `$model[$attribute]` will be used to obtain the value. */ @@ -46,6 +54,11 @@ class DataColumn extends Column */ public $enableSorting = true; /** + * @var array the HTML attributes for the link tag in the header cell + * generated by [[Sort::link]] when sorting is enabled for this column. + */ + public $sortLinkOptions = array(); + /** * @var string|array|boolean the HTML code representing a filter input (e.g. a text field, a dropdown list) * that is used for this data column. This property is effective only when [[GridView::filterModel]] is set. * @@ -59,41 +72,39 @@ class DataColumn extends Column protected function renderHeaderCellContent() { - $provider = $this->grid->dataProvider; - if ($this->attribute !== null && $this->enableSorting && - ($sort = $provider->getSort()) !== false && $sort->hasAttribute($this->attribute)) { + if ($this->header === null) { + $provider = $this->grid->dataProvider; - $label = $this->getHeaderLabel(); - if (($this->header !== null || !isset($sort->attributes[$this->attribute]['label'])) && trim($label) !== '') { - $sort->attributes[$this->attribute]['label'] = $label; + if ($this->label === null) { + if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQuery) { + /** @var Model $model */ + $model = new $provider->query->modelClass; + $label = $model->getAttributeLabel($this->attribute); + } else { + $models = $provider->getModels(); + if (($model = reset($models)) instanceof Model) { + /** @var Model $model */ + $label = $model->getAttributeLabel($this->attribute); + } else { + $label = Inflector::camel2words($this->attribute); + } + } + } else { + $label = $this->label; + } + + if ($this->attribute !== null && $this->enableSorting && + ($sort = $provider->getSort()) !== false && $sort->hasAttribute($this->attribute)) { + + return $sort->link($this->attribute, Html::encode($label), $this->sortLinkOptions); + } else { + return Html::encode($label); } - return $sort->link($this->attribute); - } elseif ($this->header === null) { - return $this->getHeaderLabel(); } else { return parent::renderHeaderCellContent(); } } - protected function getHeaderLabel() - { - $provider = $this->grid->dataProvider; - if ($this->header !== null) { - return $this->header; - } - 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)) { diff --git a/framework/yii/grid/GridView.php b/framework/yii/grid/GridView.php index 9490f27..60d325d 100644 --- a/framework/yii/grid/GridView.php +++ b/framework/yii/grid/GridView.php @@ -108,7 +108,7 @@ class GridView extends ListViewBase * 'class' => DataColumn::className(), * 'attribute' => 'name', * 'format' => 'text', - * 'header' => 'Name', + * 'label' => 'Name', * ), * array( * 'class' => CheckboxColumn::className(), @@ -119,9 +119,9 @@ class GridView extends ListViewBase * If a column is of class [[DataColumn]], the "class" element can be omitted. * * As a shortcut format, a string may be used to specify the configuration of a data column - * which only contains "attribute", "format", and/or "header" options: `"attribute:format:header"`. + * which only contains "attribute", "format", and/or "label" options: `"attribute:format:label"`. * For example, the above "name" column can also be specified as: `"name:text:Name"`. - * Both "format" and "header" are optional. They will take default values if absent. + * Both "format" and "label" are optional. They will take default values if absent. */ public $columns = array(); /** @@ -372,7 +372,7 @@ class GridView extends ListViewBase } /** - * Creates a [[DataColumn]] object based on a string in the format of "attribute:format:header". + * Creates a [[DataColumn]] object based on a string in the format of "attribute:format:label". * @param string $text the column specification string * @return DataColumn the column instance * @throws InvalidConfigException if the column specification is invalid @@ -380,14 +380,14 @@ class GridView extends ListViewBase protected function createDataColumn($text) { if (!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/', $text, $matches)) { - throw new InvalidConfigException('The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:header'); + throw new InvalidConfigException('The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label'); } return Yii::createObject(array( 'class' => $this->dataColumnClass ?: DataColumn::className(), 'grid' => $this, 'attribute' => $matches[1], 'format' => isset($matches[3]) ? $matches[3] : 'text', - 'header' => isset($matches[5]) ? $matches[5] : null, + 'label' => isset($matches[5]) ? $matches[5] : null, )); }