CGridColumn.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. * @property string $filterCellContent The filter cell content.
  23. * @property string $headerCellContent The header cell content.
  24. * @property string $footerCellContent The footer cell content.
  25. *
  26. * @author Qiang Xue <qiang.xue@gmail.com>
  27. * @package zii.widgets.grid
  28. * @since 1.1
  29. */
  30. abstract class CGridColumn extends CComponent
  31. {
  32. /**
  33. * @var string the ID of this column. This value should be unique among all grid view columns.
  34. * If this is not set, it will be assigned one automatically.
  35. */
  36. public $id;
  37. /**
  38. * @var CGridView the grid view object that owns this column.
  39. */
  40. public $grid;
  41. /**
  42. * @var string the header cell text. Note that it will not be HTML-encoded.
  43. */
  44. public $header;
  45. /**
  46. * @var string the footer cell text. Note that it will not be HTML-encoded.
  47. */
  48. public $footer;
  49. /**
  50. * @var boolean whether this column is visible. Defaults to true.
  51. */
  52. public $visible=true;
  53. /**
  54. * @var string a PHP expression that is evaluated for every data cell and whose result
  55. * is used as the CSS class name for the data cell. In this expression, you can use the following variables:
  56. * <ul>
  57. * <li><code>$row</code> the row number (zero-based)</li>
  58. * <li><code>$data</code> the data model for the row</li>
  59. * <li><code>$this</code> the column object</li>
  60. * </ul>
  61. * The PHP expression will be evaluated using {@link evaluateExpression}.
  62. *
  63. * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
  64. * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
  65. */
  66. public $cssClassExpression;
  67. /**
  68. * @var array the HTML options for the data cell tags.
  69. */
  70. public $htmlOptions=array();
  71. /**
  72. * @var array the HTML options for the filter cell tag.
  73. */
  74. public $filterHtmlOptions=array();
  75. /**
  76. * @var array the HTML options for the header cell tag.
  77. */
  78. public $headerHtmlOptions=array();
  79. /**
  80. * @var array the HTML options for the footer cell tag.
  81. */
  82. public $footerHtmlOptions=array();
  83. /**
  84. * Constructor.
  85. * @param CGridView $grid the grid view that owns this column.
  86. */
  87. public function __construct($grid)
  88. {
  89. $this->grid=$grid;
  90. }
  91. /**
  92. * Initializes the column.
  93. * This method is invoked by the grid view when it initializes itself before rendering.
  94. * You may override this method to prepare the column for rendering.
  95. */
  96. public function init()
  97. {
  98. }
  99. /**
  100. * @return boolean whether this column has a footer cell.
  101. * This is determined based on whether {@link footer} is set.
  102. */
  103. public function getHasFooter()
  104. {
  105. return $this->footer!==null;
  106. }
  107. /**
  108. * Renders the filter cell.
  109. * @since 1.1.1
  110. */
  111. public function renderFilterCell()
  112. {
  113. echo CHtml::openTag('td',$this->filterHtmlOptions);
  114. $this->renderFilterCellContent();
  115. echo "</td>";
  116. }
  117. /**
  118. * Renders the header cell.
  119. */
  120. public function renderHeaderCell()
  121. {
  122. $this->headerHtmlOptions['id']=$this->id;
  123. echo CHtml::openTag('th',$this->headerHtmlOptions);
  124. $this->renderHeaderCellContent();
  125. echo "</th>";
  126. }
  127. /**
  128. * Renders a data cell.
  129. * @param integer $row the row number (zero-based)
  130. */
  131. public function renderDataCell($row)
  132. {
  133. $data=$this->grid->dataProvider->data[$row];
  134. $options=$this->htmlOptions;
  135. if($this->cssClassExpression!==null)
  136. {
  137. $class=$this->evaluateExpression($this->cssClassExpression,array('row'=>$row,'data'=>$data));
  138. if(!empty($class))
  139. {
  140. if(isset($options['class']))
  141. $options['class'].=' '.$class;
  142. else
  143. $options['class']=$class;
  144. }
  145. }
  146. echo CHtml::openTag('td',$options);
  147. $this->renderDataCellContent($row,$data);
  148. echo '</td>';
  149. }
  150. /**
  151. * Renders the footer cell.
  152. */
  153. public function renderFooterCell()
  154. {
  155. echo CHtml::openTag('td',$this->footerHtmlOptions);
  156. $this->renderFooterCellContent();
  157. echo '</td>';
  158. }
  159. /**
  160. * Returns the header cell content.
  161. * The default implementation simply returns {@link header}.
  162. * This method may be overridden to customize the rendering of the header cell.
  163. * @return string the header cell content.
  164. * @since 1.1.16
  165. */
  166. public function getHeaderCellContent()
  167. {
  168. return trim($this->header)!=='' ? $this->header : $this->grid->blankDisplay;
  169. }
  170. /**
  171. * Renders the header cell content.
  172. * @deprecated since 1.1.16. Use {@link getHeaderCellContent()} instead.
  173. */
  174. protected function renderHeaderCellContent()
  175. {
  176. echo $this->getHeaderCellContent();
  177. }
  178. /**
  179. * Returns the footer cell content.
  180. * The default implementation simply returns {@link footer}.
  181. * This method may be overridden to customize the rendering of the footer cell.
  182. * @return string the footer cell content.
  183. * @since 1.1.16
  184. */
  185. public function getFooterCellContent()
  186. {
  187. return trim($this->footer)!=='' ? $this->footer : $this->grid->blankDisplay;
  188. }
  189. /**
  190. * Renders the footer cell content.
  191. * @deprecated since 1.1.16. Use {@link getFooterCellContent()} instead.
  192. */
  193. protected function renderFooterCellContent()
  194. {
  195. echo $this->getFooterCellContent();
  196. }
  197. /**
  198. * Returns the data cell content.
  199. * This method SHOULD be overridden to customize the rendering of the data cell.
  200. * @param integer $row the row number (zero-based)
  201. * The data for this row is available via <code>$this->grid->dataProvider->data[$row];</code>
  202. * @return string the data cell content.
  203. * @since 1.1.16
  204. */
  205. public function getDataCellContent($row)
  206. {
  207. return $this->grid->blankDisplay;
  208. }
  209. /**
  210. * Renders the data cell content.
  211. * @param integer $row the row number (zero-based)
  212. * @param mixed $data the data associated with the row
  213. * @deprecated since 1.1.16. Use {@link getDataCellContent()} instead.
  214. */
  215. protected function renderDataCellContent($row,$data)
  216. {
  217. echo $this->getDataCellContent($row);
  218. }
  219. /**
  220. * Returns the filter cell content.
  221. * The default implementation simply returns an empty column.
  222. * This method may be overridden to customize the rendering of the filter cell (if any).
  223. * @return string the filter cell content.
  224. * @since 1.1.16
  225. */
  226. public function getFilterCellContent()
  227. {
  228. return $this->grid->blankDisplay;
  229. }
  230. /**
  231. * Renders the filter cell content.
  232. * @since 1.1.1
  233. * @deprecated since 1.1.16. Use {@link getFilterCellContent()} instead.
  234. */
  235. protected function renderFilterCellContent()
  236. {
  237. echo $this->getFilterCellContent();
  238. }
  239. }