DataColumn.php 9.8 KB

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