DataColumn.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\ActiveQuery;
  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 sorting them.
  18. *
  19. * @author Qiang Xue <[email protected]>
  20. * @since 2.0
  21. */
  22. class DataColumn extends Column
  23. {
  24. /**
  25. * @var string the attribute name associated with this column. When neither [[content]] nor [[value]]
  26. * is specified, the value of the specified attribute will be retrieved from each data model and displayed.
  27. *
  28. * Also, if [[header]] is not specified, the label associated with the attribute will be displayed.
  29. */
  30. public $attribute;
  31. /**
  32. * @var string label to be displayed in the [[header|header cell]] and also to be used as the sorting
  33. * link label when sorting is enabled for this column.
  34. * If it is not set and the models provided by the GridViews data provider are instances
  35. * of [[yii\db\ActiveRecord]], the label will be determined using [[yii\db\ActiveRecord::getAttributeLabel()]].
  36. * Otherwise [[yii\helpers\Inflector::camel2words()]] will be used to get a label.
  37. */
  38. public $label;
  39. /**
  40. * @var \Closure an anonymous function that returns the value to be displayed for every data model.
  41. * The signature of this function is `function ($model, $index, $widget)`.
  42. * If this is not set, `$model[$attribute]` will be used to obtain the value.
  43. */
  44. public $value;
  45. /**
  46. * @var string|array in which format should the value of each data model be displayed as (e.g. "text", "html",
  47. * ['date', 'Y-m-d']). Supported formats are determined by the [[GridView::formatter|formatter]] used by
  48. * the [[GridView]]. Default format is "text" which will format the value as an HTML-encoded plain text when
  49. * [[\yii\base\Formatter::format()]] or [[\yii\i18n\Formatter::format()]] is used.
  50. */
  51. public $format = 'text';
  52. /**
  53. * @var boolean whether to allow sorting by this column. If true and [[attribute]] is found in
  54. * the sort definition of [[GridView::dataProvider]], then the header cell of this column
  55. * will contain a link that may trigger the sorting when being clicked.
  56. */
  57. public $enableSorting = true;
  58. /**
  59. * @var array the HTML attributes for the link tag in the header cell
  60. * generated by [[Sort::link]] when sorting is enabled for this column.
  61. */
  62. public $sortLinkOptions = [];
  63. /**
  64. * @var string|array|boolean the HTML code representing a filter input (e.g. a text field, a dropdown list)
  65. * that is used for this data column. This property is effective only when [[GridView::filterModel]] is set.
  66. *
  67. * - If this property is not set, a text field will be generated as the filter input;
  68. * - If this property is an array, a dropdown list will be generated that uses this property value as
  69. * the list options.
  70. * - If you don't want a filter for this data column, set this value to be false.
  71. */
  72. public $filter;
  73. /**
  74. * @var array the HTML attributes for the filter input fields. This property is used in combination with
  75. * the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to
  76. * render the HTML attributes for the generated filter input fields.
  77. */
  78. public $filterInputOptions = ['class' => 'form-control', 'id' => null];
  79. protected function renderHeaderCellContent()
  80. {
  81. if ($this->header !== null || $this->label === null && $this->attribute === null) {
  82. return parent::renderHeaderCellContent();
  83. }
  84. $provider = $this->grid->dataProvider;
  85. if ($this->label === null) {
  86. if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQuery) {
  87. /** @var Model $model */
  88. $model = new $provider->query->modelClass;
  89. $label = $model->getAttributeLabel($this->attribute);
  90. } else {
  91. $models = $provider->getModels();
  92. if (($model = reset($models)) instanceof Model) {
  93. /** @var Model $model */
  94. $label = $model->getAttributeLabel($this->attribute);
  95. } else {
  96. $label = Inflector::camel2words($this->attribute);
  97. }
  98. }
  99. } else {
  100. $label = $this->label;
  101. }
  102. if ($this->attribute !== null && $this->enableSorting &&
  103. ($sort = $provider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
  104. return $sort->link($this->attribute, array_merge($this->sortLinkOptions, ['label' => Html::encode($label)]));
  105. } else {
  106. return Html::encode($label);
  107. }
  108. }
  109. protected function renderFilterCellContent()
  110. {
  111. if (is_string($this->filter)) {
  112. return $this->filter;
  113. } elseif ($this->filter !== false && $this->grid->filterModel instanceof Model &&
  114. $this->attribute !== null && $this->grid->filterModel->isAttributeActive($this->attribute))
  115. {
  116. if (is_array($this->filter)) {
  117. $options = array_merge(['prompt' => ''], $this->filterInputOptions);
  118. return Html::activeDropDownList($this->grid->filterModel, $this->attribute, $this->filter, $options);
  119. } else {
  120. return Html::activeTextInput($this->grid->filterModel, $this->attribute, $this->filterInputOptions);
  121. }
  122. } else {
  123. return parent::renderFilterCellContent();
  124. }
  125. }
  126. /**
  127. * @inheritdoc
  128. */
  129. protected function renderDataCellContent($model, $key, $index)
  130. {
  131. if ($this->value !== null) {
  132. $value = call_user_func($this->value, $model, $index, $this);
  133. } elseif ($this->content === null && $this->attribute !== null) {
  134. $value = ArrayHelper::getValue($model, $this->attribute);
  135. } else {
  136. return parent::renderDataCellContent($model, $key, $index);
  137. }
  138. return $this->grid->formatter->format($value, $this->format);
  139. }
  140. }