179 lines
6.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 Closure;
  9. use yii\base\Object;
  10. use yii\helpers\Html;
  11. /**
  12. * Column is the base class of all [[GridView]] column classes.
  13. *
  14. * @author Qiang Xue <qiang.xue@gmail.com>
  15. * @since 2.0
  16. */
  17. class Column extends Object
  18. {
  19. /**
  20. * @var GridView the grid view object that owns this column.
  21. */
  22. public $grid;
  23. /**
  24. * @var string the header cell content. Note that it will not be HTML-encoded.
  25. */
  26. public $header;
  27. /**
  28. * @var string the footer cell content. Note that it will not be HTML-encoded.
  29. */
  30. public $footer;
  31. /**
  32. * @var callable This is a callable that will be used to generate the content of each cell.
  33. * The signature of the function should be the following: `function ($model, $key, $index, $column)`.
  34. * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
  35. * and `$column` is a reference to the [[Column]] object.
  36. */
  37. public $content;
  38. /**
  39. * @var boolean whether this column is visible. Defaults to true.
  40. */
  41. public $visible = true;
  42. /**
  43. * @var array the HTML attributes for the column group tag.
  44. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  45. */
  46. public $options = [];
  47. /**
  48. * @var array the HTML attributes for the header cell tag.
  49. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  50. */
  51. public $headerOptions = [];
  52. /**
  53. * @var array|\Closure the HTML attributes for the data cell tag. This can either be an array of
  54. * attributes or an anonymous function ([[Closure]]) that returns such an array.
  55. * The signature of the function should be the following: `function ($model, $key, $index, $column)`.
  56. * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
  57. * and `$column` is a reference to the [[Column]] object.
  58. * A function may be used to assign different attributes to different rows based on the data in that row.
  59. *
  60. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  61. */
  62. public $contentOptions = [];
  63. /**
  64. * @var array the HTML attributes for the footer cell tag.
  65. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  66. */
  67. public $footerOptions = [];
  68. /**
  69. * @var array the HTML attributes for the filter cell tag.
  70. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  71. */
  72. public $filterOptions = [];
  73. /**
  74. * Renders the header cell.
  75. */
  76. public function renderHeaderCell()
  77. {
  78. return Html::tag('th', $this->renderHeaderCellContent(), $this->headerOptions);
  79. }
  80. /**
  81. * Renders the footer cell.
  82. */
  83. public function renderFooterCell()
  84. {
  85. return Html::tag('td', $this->renderFooterCellContent(), $this->footerOptions);
  86. }
  87. /**
  88. * Renders a data cell.
  89. * @param mixed $model the data model being rendered
  90. * @param mixed $key the key associated with the data model
  91. * @param integer $index the zero-based index of the data item among the item array returned by [[GridView::dataProvider]].
  92. * @return string the rendering result
  93. */
  94. public function renderDataCell($model, $key, $index)
  95. {
  96. if ($this->contentOptions instanceof Closure) {
  97. $options = call_user_func($this->contentOptions, $model, $key, $index, $this);
  98. } else {
  99. $options = $this->contentOptions;
  100. }
  101. return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
  102. }
  103. /**
  104. * Renders the filter cell.
  105. */
  106. public function renderFilterCell()
  107. {
  108. return Html::tag('td', $this->renderFilterCellContent(), $this->filterOptions);
  109. }
  110. /**
  111. * Renders the header cell content.
  112. * The default implementation simply renders [[header]].
  113. * This method may be overridden to customize the rendering of the header cell.
  114. * @return string the rendering result
  115. */
  116. protected function renderHeaderCellContent()
  117. {
  118. return trim($this->header) !== '' ? $this->header : $this->getHeaderCellLabel();
  119. }
  120. /**
  121. * Returns header cell label.
  122. * This method may be overridden to customize the label of the header cell.
  123. * @return string label
  124. * @since 2.0.8
  125. */
  126. protected function getHeaderCellLabel()
  127. {
  128. return $this->grid->emptyCell;
  129. }
  130. /**
  131. * Renders the footer cell content.
  132. * The default implementation simply renders [[footer]].
  133. * This method may be overridden to customize the rendering of the footer cell.
  134. * @return string the rendering result
  135. */
  136. protected function renderFooterCellContent()
  137. {
  138. return trim($this->footer) !== '' ? $this->footer : $this->grid->emptyCell;
  139. }
  140. /**
  141. * Renders the data cell content.
  142. * @param mixed $model the data model
  143. * @param mixed $key the key associated with the data model
  144. * @param integer $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
  145. * @return string the rendering result
  146. */
  147. protected function renderDataCellContent($model, $key, $index)
  148. {
  149. if ($this->content !== null) {
  150. return call_user_func($this->content, $model, $key, $index, $this);
  151. } else {
  152. return $this->grid->emptyCell;
  153. }
  154. }
  155. /**
  156. * Renders the filter cell content.
  157. * The default implementation simply renders a space.
  158. * This method may be overridden to customize the rendering of the filter cell (if any).
  159. * @return string the rendering result
  160. */
  161. protected function renderFilterCellContent()
  162. {
  163. return $this->grid->emptyCell;
  164. }
  165. }