Column.php 5.33 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

8
namespace yii\grid;
Qiang Xue committed
9 10 11 12 13 14

use Closure;
use yii\base\Object;
use yii\helpers\Html;

/**
15
 * Column is the base class of all [[GridView]] column classes.
Qiang Xue committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Column extends Object
{
	/**
	 * @var GridView the grid view object that owns this column.
	 */
	public $grid;
	/**
	 * @var string the header cell content. Note that it will not be HTML-encoded.
	 */
	public $header;
	/**
	 * @var string the footer cell content. Note that it will not be HTML-encoded.
	 */
	public $footer;
	/**
	 * @var callable
	 */
	public $content;
	/**
	 * @var boolean whether this column is visible. Defaults to true.
	 */
	public $visible = true;
42 43
	/**
	 * @var array the HTML attributes for the column group tag.
44
	 * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
45
	 */
Alexander Makarov committed
46
	public $options = [];
47 48
	/**
	 * @var array the HTML attributes for the header cell tag.
49
	 * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
50
	 */
Alexander Makarov committed
51
	public $headerOptions = [];
Qiang Xue committed
52
	/**
53 54 55 56 57
	 * @var array|\Closure the HTML attributes for the data cell tag. This can either be an array of
	 * attributes or an anonymous function that ([[Closure]]) that returns such an array.
	 * The signature of the function should be the following: `function ($model, $key, $index, $gridView)`.
	 * A function may be used to assign different attributes to different rows based on the data in that row.
	 *
58
	 * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
Qiang Xue committed
59
	 */
Alexander Makarov committed
60
	public $contentOptions = [];
61 62
	/**
	 * @var array the HTML attributes for the footer cell tag.
63
	 * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
64
	 */
Alexander Makarov committed
65
	public $footerOptions = [];
Qiang Xue committed
66 67
	/**
	 * @var array the HTML attributes for the filter cell tag.
68
	 * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
Qiang Xue committed
69
	 */
Luciano Baraglia committed
70
	public $filterOptions = [];
Qiang Xue committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90


	/**
	 * Renders the header cell.
	 */
	public function renderHeaderCell()
	{
		return Html::tag('th', $this->renderHeaderCellContent(), $this->headerOptions);
	}

	/**
	 * Renders the footer cell.
	 */
	public function renderFooterCell()
	{
		return Html::tag('td', $this->renderFooterCellContent(), $this->footerOptions);
	}

	/**
	 * 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 integer $index the zero-based index of the data item among the item array returned by [[GridView::dataProvider]].
Qiang Xue committed
94 95
	 * @return string the rendering result
	 */
96
	public function renderDataCell($model, $key, $index)
Qiang Xue committed
97
	{
Qiang Xue committed
98
		if ($this->contentOptions instanceof Closure) {
99
			$options = call_user_func($this->contentOptions, $model, $key, $index, $this);
Qiang Xue committed
100
		} else {
Qiang Xue committed
101
			$options = $this->contentOptions;
Qiang Xue committed
102
		}
103
		return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
Qiang Xue committed
104 105 106 107 108 109 110 111 112 113 114 115
	}

	/**
	 * Renders the filter cell.
	 */
	public function renderFilterCell()
	{
		return Html::tag('td', $this->renderFilterCellContent(), $this->filterOptions);
	}

	/**
	 * Renders the header cell content.
Qiang Xue committed
116
	 * The default implementation simply renders [[header]].
Qiang Xue committed
117 118 119 120 121 122 123 124 125 126
	 * This method may be overridden to customize the rendering of the header cell.
	 * @return string the rendering result
	 */
	protected function renderHeaderCellContent()
	{
		return trim($this->header) !== '' ? $this->header : $this->grid->emptyCell;
	}

	/**
	 * Renders the footer cell content.
Qiang Xue committed
127
	 * The default implementation simply renders [[footer]].
Qiang Xue committed
128 129 130 131 132 133 134
	 * This method may be overridden to customize the rendering of the footer cell.
	 * @return string the rendering result
	 */
	protected function renderFooterCellContent()
	{
		return trim($this->footer) !== '' ? $this->footer : $this->grid->emptyCell;
	}
135

Qiang Xue committed
136
	/**
Qiang Xue committed
137 138
	 * Returns the raw data cell content.
	 * This method is called by [[renderDataCellContent()]] when rendering the content of a data cell.
139
	 * @param mixed $model the data model
140
	 * @param mixed $key the key associated with the data model
141
	 * @param integer $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
Qiang Xue committed
142 143
	 * @return string the rendering result
	 */
144
	protected function getDataCellContent($model, $key, $index)
Qiang Xue committed
145 146
	{
		if ($this->content !== null) {
147
			return call_user_func($this->content, $model, $key, $index, $this);
Qiang Xue committed
148
		} else {
149
			return null;
Qiang Xue committed
150 151 152
		}
	}

153 154 155 156 157 158 159 160 161
	/**
	 * Renders the data cell content.
	 * @param mixed $model the data model
	 * @param mixed $key the key associated with the data model
	 * @param integer $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
	 * @return string the rendering result
	 */
	protected function renderDataCellContent($model, $key, $index)
	{
Qiang Xue committed
162
		return $this->content !== null ? $this->getDataCellContent($model, $key, $index) : $this->grid->emptyCell;
163 164
	}

Qiang Xue committed
165 166 167 168 169 170 171 172 173 174 175
	/**
	 * Renders the filter cell content.
	 * The default implementation simply renders a space.
	 * This method may be overridden to customize the rendering of the filter cell (if any).
	 * @return string the rendering result
	 */
	protected function renderFilterCellContent()
	{
		return $this->grid->emptyCell;
	}
}