Browse Source

introduced DataColumn::label property

tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
9847472144
  1. 59
      framework/yii/grid/DataColumn.php
  2. 12
      framework/yii/grid/GridView.php

59
framework/yii/grid/DataColumn.php

@ -28,6 +28,14 @@ class DataColumn extends Column
*/ */
public $attribute; 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. * @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. * 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; 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) * @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. * 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() protected function renderHeaderCellContent()
{ {
if ($this->header === null) {
$provider = $this->grid->dataProvider; $provider = $this->grid->dataProvider;
if ($this->attribute !== null && $this->enableSorting &&
($sort = $provider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
$label = $this->getHeaderLabel(); if ($this->label === null) {
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 ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQuery) { if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQuery) {
/** @var Model $model */ /** @var Model $model */
$model = new $provider->query->modelClass; $model = new $provider->query->modelClass;
return $model->getAttributeLabel($this->attribute); $label = $model->getAttributeLabel($this->attribute);
} } else {
$models = $provider->getModels(); $models = $provider->getModels();
if (($model = reset($models)) instanceof Model) { if (($model = reset($models)) instanceof Model) {
/** @var Model $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() protected function renderFilterCellContent()

12
framework/yii/grid/GridView.php

@ -108,7 +108,7 @@ class GridView extends ListViewBase
* 'class' => DataColumn::className(), * 'class' => DataColumn::className(),
* 'attribute' => 'name', * 'attribute' => 'name',
* 'format' => 'text', * 'format' => 'text',
* 'header' => 'Name', * 'label' => 'Name',
* ), * ),
* array( * array(
* 'class' => CheckboxColumn::className(), * 'class' => CheckboxColumn::className(),
@ -119,9 +119,9 @@ class GridView extends ListViewBase
* If a column is of class [[DataColumn]], the "class" element can be omitted. * 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 * 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"`. * 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(); 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 * @param string $text the column specification string
* @return DataColumn the column instance * @return DataColumn the column instance
* @throws InvalidConfigException if the column specification is invalid * @throws InvalidConfigException if the column specification is invalid
@ -380,14 +380,14 @@ class GridView extends ListViewBase
protected function createDataColumn($text) protected function createDataColumn($text)
{ {
if (!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/', $text, $matches)) { 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( return Yii::createObject(array(
'class' => $this->dataColumnClass ?: DataColumn::className(), 'class' => $this->dataColumnClass ?: DataColumn::className(),
'grid' => $this, 'grid' => $this,
'attribute' => $matches[1], 'attribute' => $matches[1],
'format' => isset($matches[3]) ? $matches[3] : 'text', 'format' => isset($matches[3]) ? $matches[3] : 'text',
'header' => isset($matches[5]) ? $matches[5] : null, 'label' => isset($matches[5]) ? $matches[5] : null,
)); ));
} }

Loading…
Cancel
Save