CGridColumn.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * CGridColumn class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CGridColumn is the base class for all grid view column classes.
  12. *
  13. * A CGridColumn object represents the specification for rendering the cells in
  14. * a particular grid view column.
  15. *
  16. * In a column, there is one header cell, multiple data cells, and an optional footer cell.
  17. * Child classes may override {@link renderHeaderCellContent}, {@link renderDataCellContent}
  18. * and {@link renderFooterCellContent} to customize how these cells are rendered.
  19. *
  20. * @property boolean $hasFooter Whether this column has a footer cell.
  21. * This is determined based on whether {@link footer} is set.
  22. *
  23. * @author Qiang Xue <qiang.xue@gmail.com>
  24. * @package zii.widgets.grid
  25. * @since 1.1
  26. */
  27. abstract class CGridColumn extends CComponent
  28. {
  29. /**
  30. * @var string the ID of this column. This value should be unique among all grid view columns.
  31. * If this is not set, it will be assigned one automatically.
  32. */
  33. public $id;
  34. /**
  35. * @var CGridView the grid view object that owns this column.
  36. */
  37. public $grid;
  38. /**
  39. * @var string the header cell text. Note that it will not be HTML-encoded.
  40. */
  41. public $header;
  42. /**
  43. * @var string the footer cell text. Note that it will not be HTML-encoded.
  44. */
  45. public $footer;
  46. /**
  47. * @var boolean whether this column is visible. Defaults to true.
  48. */
  49. public $visible=true;
  50. /**
  51. * @var string a PHP expression that is evaluated for every data cell and whose result
  52. * is used as the CSS class name for the data cell. In this expression, you can use the following variables:
  53. * <ul>
  54. * <li><code>$row</code> the row number (zero-based)</li>
  55. * <li><code>$data</code> the data model for the row</li>
  56. * <li><code>$this</code> the column object</li>
  57. * </ul>
  58. * The PHP expression will be evaluated using {@link evaluateExpression}.
  59. *
  60. * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
  61. * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
  62. */
  63. public $cssClassExpression;
  64. /**
  65. * @var array the HTML options for the data cell tags.
  66. */
  67. public $htmlOptions=array();
  68. /**
  69. * @var array the HTML options for the filter cell tag.
  70. */
  71. public $filterHtmlOptions=array();
  72. /**
  73. * @var array the HTML options for the header cell tag.
  74. */
  75. public $headerHtmlOptions=array();
  76. /**
  77. * @var array the HTML options for the footer cell tag.
  78. */
  79. public $footerHtmlOptions=array();
  80. /**
  81. * Constructor.
  82. * @param CGridView $grid the grid view that owns this column.
  83. */
  84. public function __construct($grid)
  85. {
  86. $this->grid=$grid;
  87. }
  88. /**
  89. * Initializes the column.
  90. * This method is invoked by the grid view when it initializes itself before rendering.
  91. * You may override this method to prepare the column for rendering.
  92. */
  93. public function init()
  94. {
  95. }
  96. /**
  97. * @return boolean whether this column has a footer cell.
  98. * This is determined based on whether {@link footer} is set.
  99. */
  100. public function getHasFooter()
  101. {
  102. return $this->footer!==null;
  103. }
  104. /**
  105. * Renders the filter cell.
  106. * @since 1.1.1
  107. */
  108. public function renderFilterCell()
  109. {
  110. echo CHtml::openTag('td',$this->filterHtmlOptions);
  111. $this->renderFilterCellContent();
  112. echo "</td>";
  113. }
  114. /**
  115. * Renders the header cell.
  116. */
  117. public function renderHeaderCell()
  118. {
  119. $this->headerHtmlOptions['id']=$this->id;
  120. echo CHtml::openTag('th',$this->headerHtmlOptions);
  121. $this->renderHeaderCellContent();
  122. echo "</th>";
  123. }
  124. /**
  125. * Renders a data cell.
  126. * @param integer $row the row number (zero-based)
  127. */
  128. public function renderDataCell($row)
  129. {
  130. $data=$this->grid->dataProvider->data[$row];
  131. $options=$this->htmlOptions;
  132. if($this->cssClassExpression!==null)
  133. {
  134. $class=$this->evaluateExpression($this->cssClassExpression,array('row'=>$row,'data'=>$data));
  135. if(!empty($class))
  136. {
  137. if(isset($options['class']))
  138. $options['class'].=' '.$class;
  139. else
  140. $options['class']=$class;
  141. }
  142. }
  143. echo CHtml::openTag('td',$options);
  144. $this->renderDataCellContent($row,$data);
  145. echo '</td>';
  146. }
  147. /**
  148. * Renders the footer cell.
  149. */
  150. public function renderFooterCell()
  151. {
  152. echo CHtml::openTag('td',$this->footerHtmlOptions);
  153. $this->renderFooterCellContent();
  154. echo '</td>';
  155. }
  156. /**
  157. * Renders the header cell content.
  158. * The default implementation simply renders {@link header}.
  159. * This method may be overridden to customize the rendering of the header cell.
  160. */
  161. protected function renderHeaderCellContent()
  162. {
  163. echo trim($this->header)!=='' ? $this->header : $this->grid->blankDisplay;
  164. }
  165. /**
  166. * Renders the footer cell content.
  167. * The default implementation simply renders {@link footer}.
  168. * This method may be overridden to customize the rendering of the footer cell.
  169. */
  170. protected function renderFooterCellContent()
  171. {
  172. echo trim($this->footer)!=='' ? $this->footer : $this->grid->blankDisplay;
  173. }
  174. /**
  175. * Renders the data cell content.
  176. * This method SHOULD be overridden to customize the rendering of the data cell.
  177. * @param integer $row the row number (zero-based)
  178. * @param mixed $data the data associated with the row
  179. */
  180. protected function renderDataCellContent($row,$data)
  181. {
  182. echo $this->grid->blankDisplay;
  183. }
  184. /**
  185. * Renders the filter cell content.
  186. * The default implementation simply renders a space.
  187. * This method may be overridden to customize the rendering of the filter cell (if any).
  188. * @since 1.1.1
  189. */
  190. protected function renderFilterCellContent()
  191. {
  192. echo $this->grid->blankDisplay;
  193. }
  194. }