Column.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <[email protected]>
  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
  33. */
  34. public $content;
  35. /**
  36. * @var boolean whether this column is visible. Defaults to true.
  37. */
  38. public $visible = true;
  39. public $options = [];
  40. public $headerOptions = [];
  41. /**
  42. * @var array|\Closure
  43. */
  44. public $contentOptions = [];
  45. public $footerOptions = [];
  46. /**
  47. * @var array the HTML attributes for the filter cell tag.
  48. */
  49. public $filterOptions=[];
  50. /**
  51. * Renders the header cell.
  52. */
  53. public function renderHeaderCell()
  54. {
  55. return Html::tag('th', $this->renderHeaderCellContent(), $this->headerOptions);
  56. }
  57. /**
  58. * Renders the footer cell.
  59. */
  60. public function renderFooterCell()
  61. {
  62. return Html::tag('td', $this->renderFooterCellContent(), $this->footerOptions);
  63. }
  64. /**
  65. * Renders a data cell.
  66. * @param mixed $model the data model being rendered
  67. * @param mixed $key the key associated with the data model
  68. * @param integer $index the zero-based index of the data item among the item array returned by [[dataProvider]].
  69. * @return string the rendering result
  70. */
  71. public function renderDataCell($model, $key, $index)
  72. {
  73. if ($this->contentOptions instanceof Closure) {
  74. $options = call_user_func($this->contentOptions, $model, $key, $index, $this);
  75. } else {
  76. $options = $this->contentOptions;
  77. }
  78. return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
  79. }
  80. /**
  81. * Renders the filter cell.
  82. */
  83. public function renderFilterCell()
  84. {
  85. return Html::tag('td', $this->renderFilterCellContent(), $this->filterOptions);
  86. }
  87. /**
  88. * Renders the header cell content.
  89. * The default implementation simply renders [[header]].
  90. * This method may be overridden to customize the rendering of the header cell.
  91. * @return string the rendering result
  92. */
  93. protected function renderHeaderCellContent()
  94. {
  95. return trim($this->header) !== '' ? $this->header : $this->grid->emptyCell;
  96. }
  97. /**
  98. * Renders the footer cell content.
  99. * The default implementation simply renders [[footer]].
  100. * This method may be overridden to customize the rendering of the footer cell.
  101. * @return string the rendering result
  102. */
  103. protected function renderFooterCellContent()
  104. {
  105. return trim($this->footer) !== '' ? $this->footer : $this->grid->emptyCell;
  106. }
  107. /**
  108. * Renders the data cell content.
  109. * @param mixed $model the data model
  110. * @param mixed $key the key associated with the data model
  111. * @param integer $index the zero-based index of the data model among the models array returned by [[dataProvider]].
  112. * @return string the rendering result
  113. */
  114. protected function renderDataCellContent($model, $key, $index)
  115. {
  116. if ($this->content !== null) {
  117. return call_user_func($this->content, $model, $key, $index, $this);
  118. } else {
  119. return $this->grid->emptyCell;
  120. }
  121. }
  122. /**
  123. * Renders the filter cell content.
  124. * The default implementation simply renders a space.
  125. * This method may be overridden to customize the rendering of the filter cell (if any).
  126. * @return string the rendering result
  127. */
  128. protected function renderFilterCellContent()
  129. {
  130. return $this->grid->emptyCell;
  131. }
  132. }