CLinkColumn.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * CLinkColumn 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. Yii::import('zii.widgets.grid.CGridColumn');
  11. /**
  12. * CLinkColumn represents a grid view column that renders a hyperlink in each of its data cells.
  13. *
  14. * The {@link label} and {@link url} properties determine how each hyperlink will be rendered.
  15. * The {@link labelExpression}, {@link urlExpression} properties may be used instead if they are available.
  16. * In addition, if {@link imageUrl} is set, an image link will be rendered.
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @package zii.widgets.grid
  20. * @since 1.1
  21. */
  22. class CLinkColumn extends CGridColumn
  23. {
  24. /**
  25. * @var string the label to the hyperlinks in the data cells. Note that the label will not
  26. * be HTML-encoded when rendering. This property is ignored if {@link labelExpression} is set.
  27. * @see labelExpression
  28. */
  29. public $label='Link';
  30. /**
  31. * @var string a PHP expression that will be evaluated for every data cell and whose result will be rendered
  32. * as the label of the hyperlink of the data cell.
  33. * In this expression, you can use the following variables:
  34. * <ul>
  35. * <li><code>$row</code> the row number (zero-based).</li>
  36. * <li><code>$data</code> the data model for the row.</li>
  37. * <li><code>$this</code> the column object.</li>
  38. * </ul>
  39. * The PHP expression will be evaluated using {@link evaluateExpression}.
  40. *
  41. * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
  42. * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
  43. */
  44. public $labelExpression;
  45. /**
  46. * @var string the URL to the image. If this is set, an image link will be rendered.
  47. */
  48. public $imageUrl;
  49. /**
  50. * @var string the URL of the hyperlinks in the data cells.
  51. * This property is ignored if {@link urlExpression} is set.
  52. * @see urlExpression
  53. */
  54. public $url='javascript:void(0)';
  55. /**
  56. * @var string a PHP expression that will be evaluated for every data cell and whose result will be rendered
  57. * as the URL of the hyperlink of the data cells.
  58. * In this expression, you can use the following variables:
  59. * <ul>
  60. * <li><code>$row</code> the row number (zero-based).</li>
  61. * <li><code>$data</code> the data model for the row.</li>
  62. * <li><code>$this</code> the column object.</li>
  63. * </ul>
  64. * The PHP expression will be evaluated using {@link evaluateExpression}.
  65. *
  66. * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
  67. * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
  68. */
  69. public $urlExpression;
  70. /**
  71. * @var array the HTML options for the data cell tags.
  72. */
  73. public $htmlOptions=array('class'=>'link-column');
  74. /**
  75. * @var array the HTML options for the header cell tag.
  76. */
  77. public $headerHtmlOptions=array('class'=>'link-column');
  78. /**
  79. * @var array the HTML options for the footer cell tag.
  80. */
  81. public $footerHtmlOptions=array('class'=>'link-column');
  82. /**
  83. * @var array the HTML options for the hyperlinks
  84. */
  85. public $linkHtmlOptions=array();
  86. /**
  87. * Returns the data cell content.
  88. * This method renders a hyperlink in the data cell.
  89. * @param integer $row the row number (zero-based)
  90. * @return string the data cell content.
  91. * @since 1.1.16
  92. */
  93. public function getDataCellContent($row)
  94. {
  95. $data=$this->grid->dataProvider->data[$row];
  96. if($this->urlExpression!==null)
  97. $url=$this->evaluateExpression($this->urlExpression,array('data'=>$data,'row'=>$row));
  98. else
  99. $url=$this->url;
  100. if($this->labelExpression!==null)
  101. $label=$this->evaluateExpression($this->labelExpression,array('data'=>$data,'row'=>$row));
  102. else
  103. $label=$this->label;
  104. $options=$this->linkHtmlOptions;
  105. if(is_string($this->imageUrl))
  106. return CHtml::link(CHtml::image($this->imageUrl,$label),$url,$options);
  107. else
  108. return CHtml::link($label,$url,$options);
  109. }
  110. }