DataColumn.php 11 KB

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