Column.php 3.71 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;
Alexander Makarov committed
42 43
	public $options = [];
	public $headerOptions = [];
Qiang Xue committed
44 45 46
	/**
	 * @var array|\Closure
	 */
Alexander Makarov committed
47 48
	public $contentOptions = [];
	public $footerOptions = [];
Qiang Xue committed
49 50 51
	/**
	 * @var array the HTML attributes for the filter cell tag.
	 */
Alexander Makarov committed
52
	public $filterOptions=[];
Qiang Xue committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72


	/**
	 * 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.
73
	 * @param mixed $model the data model being rendered
74
	 * @param mixed $key the key associated with the data model
75
	 * @param integer $index the zero-based index of the data item among the item array returned by [[GridView::dataProvider]].
Qiang Xue committed
76 77
	 * @return string the rendering result
	 */
78
	public function renderDataCell($model, $key, $index)
Qiang Xue committed
79
	{
Qiang Xue committed
80
		if ($this->contentOptions instanceof Closure) {
81
			$options = call_user_func($this->contentOptions, $model, $key, $index, $this);
Qiang Xue committed
82
		} else {
Qiang Xue committed
83
			$options = $this->contentOptions;
Qiang Xue committed
84
		}
85
		return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
Qiang Xue committed
86 87 88 89 90 91 92 93 94 95 96 97
	}

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

	/**
	 * Renders the header cell content.
Qiang Xue committed
98
	 * The default implementation simply renders [[header]].
Qiang Xue committed
99 100 101 102 103 104 105 106 107 108
	 * 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
109
	 * The default implementation simply renders [[footer]].
Qiang Xue committed
110 111 112 113 114 115 116 117 118 119
	 * 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;
	}

	/**
	 * Renders the data cell content.
120
	 * @param mixed $model the data model
121
	 * @param mixed $key the key associated with the data model
122
	 * @param integer $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
Qiang Xue committed
123 124
	 * @return string the rendering result
	 */
125
	protected function renderDataCellContent($model, $key, $index)
Qiang Xue committed
126 127
	{
		if ($this->content !== null) {
128
			return call_user_func($this->content, $model, $key, $index, $this);
Qiang Xue committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
		} else {
			return $this->grid->emptyCell;
		}
	}

	/**
	 * 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;
	}
}