|
|
|
@ -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)) { |
|
|
|
|