|
- <?php
-
-
- namespace yii\grid;
-
- use yii\base\Model;
- use yii\data\ActiveDataProvider;
- use yii\data\ArrayDataProvider;
- use yii\db\ActiveQueryInterface;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Html;
- use yii\helpers\Inflector;
-
-
- class DataColumn extends Column
- {
-
-
- public $attribute;
-
-
- public $label;
-
-
- public $encodeLabel = true;
-
-
- public $value;
-
-
- public $format = 'text';
-
-
- public $enableSorting = true;
-
-
- public $sortLinkOptions = [];
-
-
- public $filter;
-
-
- public $filterInputOptions = ['class' => 'form-control', 'id' => null];
-
-
-
-
- protected function renderHeaderCellContent()
- {
- if ($this->header !== null || $this->label === null && $this->attribute === null) {
- return parent::renderHeaderCellContent();
- }
-
- $label = $this->getHeaderCellLabel();
- if ($this->encodeLabel) {
- $label = Html::encode($label);
- }
-
- if ($this->attribute !== null && $this->enableSorting &&
- ($sort = $this->grid->dataProvider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
- return $sort->link($this->attribute, array_merge($this->sortLinkOptions, ['label' => $label]));
- } else {
- return $label;
- }
- }
-
-
-
- protected function getHeaderCellLabel()
- {
- $provider = $this->grid->dataProvider;
-
- if ($this->label === null) {
- if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
-
- $model = new $provider->query->modelClass;
- $label = $model->getAttributeLabel($this->attribute);
- } elseif ($provider instanceof ArrayDataProvider && $provider->modelClass !== null) {
-
- $model = new $provider->modelClass;
- $label = $model->getAttributeLabel($this->attribute);
- } elseif ($this->grid->filterModel !== null && $this->grid->filterModel instanceof Model) {
- $label = $this->grid->filterModel->getAttributeLabel($this->attribute);
- } else {
- $models = $provider->getModels();
- if (($model = reset($models)) instanceof Model) {
-
- $label = $model->getAttributeLabel($this->attribute);
- } else {
- $label = Inflector::camel2words($this->attribute);
- }
- }
- } else {
- $label = $this->label;
- }
-
- return $label;
- }
-
-
-
- protected function renderFilterCellContent()
- {
- if (is_string($this->filter)) {
- return $this->filter;
- }
-
- $model = $this->grid->filterModel;
-
- if ($this->filter !== false && $model instanceof Model && $this->attribute !== null && $model->isAttributeActive($this->attribute)) {
- if ($model->hasErrors($this->attribute)) {
- Html::addCssClass($this->filterOptions, 'has-error');
- $error = ' ' . Html::error($model, $this->attribute, $this->grid->filterErrorOptions);
- } else {
- $error = '';
- }
- if (is_array($this->filter)) {
- $options = array_merge(['prompt' => ''], $this->filterInputOptions);
- return Html::activeDropDownList($model, $this->attribute, $this->filter, $options) . $error;
- } else {
- return Html::activeTextInput($model, $this->attribute, $this->filterInputOptions) . $error;
- }
- } else {
- return parent::renderFilterCellContent();
- }
- }
-
-
-
- public function getDataCellValue($model, $key, $index)
- {
- if ($this->value !== null) {
- if (is_string($this->value)) {
- return ArrayHelper::getValue($model, $this->value);
- } else {
- return call_user_func($this->value, $model, $key, $index, $this);
- }
- } elseif ($this->attribute !== null) {
- return ArrayHelper::getValue($model, $this->attribute);
- }
- return null;
- }
-
-
-
- protected function renderDataCellContent($model, $key, $index)
- {
- if ($this->content === null) {
- return $this->grid->formatter->format($this->getDataCellValue($model, $key, $index), $this->format);
- } else {
- return parent::renderDataCellContent($model, $key, $index);
- }
- }
- }
|