Commit 98474721 by Carsten Brandt

introduced DataColumn::label property

parent b147760c
......@@ -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,39 +72,37 @@ class DataColumn extends Column
protected function renderHeaderCellContent()
{
if ($this->header === null) {
$provider = $this->grid->dataProvider;
if ($this->attribute !== null && $this->enableSorting &&
($sort = $provider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
$label = $this->getHeaderLabel();
if (($this->header !== null || !isset($sort->attributes[$this->attribute]['label'])) && trim($label) !== '') {
$sort->attributes[$this->attribute]['label'] = $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 ($this->label === null) {
if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQuery) {
/** @var Model $model */
$model = new $provider->query->modelClass;
return $model->getAttributeLabel($this->attribute);
}
$label = $model->getAttributeLabel($this->attribute);
} else {
$models = $provider->getModels();
if (($model = reset($models)) instanceof Model) {
/** @var Model $model */
return $model->getAttributeLabel($this->attribute);
$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);
}
} else {
return parent::renderHeaderCellContent();
}
return Inflector::camel2words($this->attribute);
}
protected function renderFilterCellContent()
......
......@@ -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,
));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment