Column.php 6.1 KB

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