You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
9.0KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\grid;
  8. use yii\base\Model;
  9. use yii\data\ActiveDataProvider;
  10. use yii\db\ActiveQueryInterface;
  11. use yii\helpers\ArrayHelper;
  12. use yii\helpers\Html;
  13. use yii\helpers\Inflector;
  14. /**
  15. * DataColumn is the default column type for the [[GridView]] widget.
  16. *
  17. * It is used to show data columns and allows [[enableSorting|sorting]] and [[filter|filtering]] them.
  18. *
  19. * A simple data column definition refers to an attribute in the data model of the
  20. * GridView's data provider. The name of the attribute is specified by [[attribute]].
  21. *
  22. * By setting [[value]] and [[label]], the header and cell content can be customized.
  23. *
  24. * A data column differentiates between the [[getDataCellValue|data cell value]] and the
  25. * [[renderDataCellContent|data cell content]]. The cell value is an un-formatted value that
  26. * may be used for calculation, while the actual cell content is a [[format|formatted]] version of that
  27. * value which may contain HTML markup.
  28. *
  29. * @author Qiang Xue <qiang.xue@gmail.com>
  30. * @since 2.0
  31. */
  32. class DataColumn extends Column
  33. {
  34. /**
  35. * @var string the attribute name associated with this column. When neither [[content]] nor [[value]]
  36. * is specified, the value of the specified attribute will be retrieved from each data model and displayed.
  37. *
  38. * Also, if [[label]] is not specified, the label associated with the attribute will be displayed.
  39. */
  40. public $attribute;
  41. /**
  42. * @var string label to be displayed in the [[header|header cell]] and also to be used as the sorting
  43. * link label when sorting is enabled for this column.
  44. * If it is not set and the models provided by the GridViews data provider are instances
  45. * of [[\yii\db\ActiveRecord]], the label will be determined using [[\yii\db\ActiveRecord::getAttributeLabel()]].
  46. * Otherwise [[\yii\helpers\Inflector::camel2words()]] will be used to get a label.
  47. */
  48. public $label;
  49. /**
  50. * @var boolean whether the header label should be HTML-encoded.
  51. * @see label
  52. * @since 2.0.1
  53. */
  54. public $encodeLabel = true;
  55. /**
  56. * @var string|\Closure an anonymous function or a string that is used to determine the value to display in the current column.
  57. *
  58. * If this is an anonymous function, it will be called for each row and the return value will be used as the value to
  59. * display for every data model. The signature of this function should be: `function ($model, $key, $index, $column)`.
  60. * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
  61. * and `$column` is a reference to the [[DataColumn]] object.
  62. *
  63. * You may also set this property to a string representing the attribute name to be displayed in this column.
  64. * This can be used when the attribute to be displayed is different from the [[attribute]] that is used for
  65. * sorting and filtering.
  66. *
  67. * If this is not set, `$model[$attribute]` will be used to obtain the value, where `$attribute` is the value of [[attribute]].
  68. */
  69. public $value;
  70. /**
  71. * @var string|array in which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`,
  72. * `['date', 'php:Y-m-d']`). Supported formats are determined by the [[GridView::formatter|formatter]] used by
  73. * the [[GridView]]. Default format is "text" which will format the value as an HTML-encoded plain text when
  74. * [[\yii\i18n\Formatter]] is used as the [[GridView::$formatter|formatter]] of the GridView.
  75. */
  76. public $format = 'text';
  77. /**
  78. * @var boolean whether to allow sorting by this column. If true and [[attribute]] is found in
  79. * the sort definition of [[GridView::dataProvider]], then the header cell of this column
  80. * will contain a link that may trigger the sorting when being clicked.
  81. */
  82. public $enableSorting = true;
  83. /**
  84. * @var array the HTML attributes for the link tag in the header cell
  85. * generated by [[\yii\data\Sort::link]] when sorting is enabled for this column.
  86. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  87. */
  88. public $sortLinkOptions = [];
  89. /**
  90. * @var string|array|boolean the HTML code representing a filter input (e.g. a text field, a dropdown list)
  91. * that is used for this data column. This property is effective only when [[GridView::filterModel]] is set.
  92. *
  93. * - If this property is not set, a text field will be generated as the filter input;
  94. * - If this property is an array, a dropdown list will be generated that uses this property value as
  95. * the list options.
  96. * - If you don't want a filter for this data column, set this value to be false.
  97. */
  98. public $filter;
  99. /**
  100. * @var array the HTML attributes for the filter input fields. This property is used in combination with
  101. * the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to
  102. * render the HTML attributes for the generated filter input fields.
  103. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  104. */
  105. public $filterInputOptions = ['class' => 'form-control', 'id' => null];
  106. /**
  107. * @inheritdoc
  108. */
  109. protected function renderHeaderCellContent()
  110. {
  111. if ($this->header !== null || $this->label === null && $this->attribute === null) {
  112. return parent::renderHeaderCellContent();
  113. }
  114. $provider = $this->grid->dataProvider;
  115. if ($this->label === null) {
  116. if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
  117. /* @var $model Model */
  118. $model = new $provider->query->modelClass;
  119. $label = $model->getAttributeLabel($this->attribute);
  120. } else {
  121. $models = $provider->getModels();
  122. if (($model = reset($models)) instanceof Model) {
  123. /* @var $model Model */
  124. $label = $model->getAttributeLabel($this->attribute);
  125. } else {
  126. $label = Inflector::camel2words($this->attribute);
  127. }
  128. }
  129. } else {
  130. $label = $this->label;
  131. }
  132. if ($this->attribute !== null && $this->enableSorting &&
  133. ($sort = $provider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
  134. return $sort->link($this->attribute, array_merge($this->sortLinkOptions, ['label' => ($this->encodeLabel ? Html::encode($label) : $label)]));
  135. } else {
  136. return $this->encodeLabel ? Html::encode($label) : $label;
  137. }
  138. }
  139. /**
  140. * @inheritdoc
  141. */
  142. protected function renderFilterCellContent()
  143. {
  144. if (is_string($this->filter)) {
  145. return $this->filter;
  146. }
  147. $model = $this->grid->filterModel;
  148. if ($this->filter !== false && $model instanceof Model && $this->attribute !== null && $model->isAttributeActive($this->attribute)) {
  149. if ($model->hasErrors($this->attribute)) {
  150. Html::addCssClass($this->filterOptions, 'has-error');
  151. $error = ' ' . Html::error($model, $this->attribute, $this->grid->filterErrorOptions);
  152. } else {
  153. $error = '';
  154. }
  155. if (is_array($this->filter)) {
  156. $options = array_merge(['prompt' => ''], $this->filterInputOptions);
  157. return Html::activeDropDownList($model, $this->attribute, $this->filter, $options) . $error;
  158. } else {
  159. return Html::activeTextInput($model, $this->attribute, $this->filterInputOptions) . $error;
  160. }
  161. } else {
  162. return parent::renderFilterCellContent();
  163. }
  164. }
  165. /**
  166. * Returns the data cell value.
  167. * @param mixed $model the data model
  168. * @param mixed $key the key associated with the data model
  169. * @param integer $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
  170. * @return string the data cell value
  171. */
  172. public function getDataCellValue($model, $key, $index)
  173. {
  174. if ($this->value !== null) {
  175. if (is_string($this->value)) {
  176. return ArrayHelper::getValue($model, $this->value);
  177. } else {
  178. return call_user_func($this->value, $model, $key, $index, $this);
  179. }
  180. } elseif ($this->attribute !== null) {
  181. return ArrayHelper::getValue($model, $this->attribute);
  182. }
  183. return null;
  184. }
  185. /**
  186. * @inheritdoc
  187. */
  188. protected function renderDataCellContent($model, $key, $index)
  189. {
  190. if ($this->content === null) {
  191. return $this->grid->formatter->format($this->getDataCellValue($model, $key, $index), $this->format);
  192. } else {
  193. return parent::renderDataCellContent($model, $key, $index);
  194. }
  195. }
  196. }