|
- <?php
-
-
- namespace yii\grid;
-
- use Yii;
- use Closure;
- use yii\i18n\Formatter;
- use yii\base\InvalidConfigException;
- use yii\helpers\Url;
- use yii\helpers\Html;
- use yii\helpers\Json;
- use yii\widgets\BaseListView;
- use yii\base\Model;
-
-
- class GridView extends BaseListView
- {
- const FILTER_POS_HEADER = 'header';
- const FILTER_POS_FOOTER = 'footer';
- const FILTER_POS_BODY = 'body';
-
-
-
- public $dataColumnClass;
-
-
- public $caption;
-
-
- public $captionOptions = [];
-
-
- public $tableOptions = ['class' => 'table table-striped table-bordered'];
-
-
- public $options = ['class' => 'grid-view'];
-
-
- public $headerRowOptions = [];
-
-
- public $footerRowOptions = [];
-
-
- public $rowOptions = [];
-
-
- public $beforeRow;
-
-
- public $afterRow;
-
-
- public $showHeader = true;
-
-
- public $showFooter = false;
-
-
- public $showOnEmpty = true;
-
-
- public $formatter;
-
-
- public $columns = [];
-
-
- public $emptyCell = ' ';
-
-
- public $filterModel;
-
-
- public $filterUrl;
-
-
- public $filterSelector;
-
-
- public $filterPosition = self::FILTER_POS_BODY;
-
-
- public $filterRowOptions = ['class' => 'filters'];
-
-
- public $filterErrorSummaryOptions = ['class' => 'error-summary'];
-
-
- public $filterErrorOptions = ['class' => 'help-block'];
-
-
- public $layout = "{summary}\n{items}\n{pager}";
-
-
-
-
- public function init()
- {
- parent::init();
- if ($this->formatter === null) {
- $this->formatter = Yii::$app->getFormatter();
- } elseif (is_array($this->formatter)) {
- $this->formatter = Yii::createObject($this->formatter);
- }
- if (!$this->formatter instanceof Formatter) {
- throw new InvalidConfigException('The "formatter" property must be either a Format object or a configuration array.');
- }
- if (!isset($this->filterRowOptions['id'])) {
- $this->filterRowOptions['id'] = $this->options['id'] . '-filters';
- }
-
- $this->initColumns();
- }
-
-
-
- public function run()
- {
- $id = $this->options['id'];
- $options = Json::htmlEncode($this->getClientOptions());
- $view = $this->getView();
- GridViewAsset::register($view);
- $view->registerJs("jQuery('#$id').yiiGridView($options);");
- parent::run();
- }
-
-
-
- public function renderErrors()
- {
- if ($this->filterModel instanceof Model && $this->filterModel->hasErrors()) {
- return Html::errorSummary($this->filterModel, $this->filterErrorSummaryOptions);
- } else {
- return '';
- }
- }
-
-
-
- public function renderSection($name)
- {
- switch ($name) {
- case '{errors}':
- return $this->renderErrors();
- default:
- return parent::renderSection($name);
- }
- }
-
-
-
- protected function getClientOptions()
- {
- $filterUrl = isset($this->filterUrl) ? $this->filterUrl : Yii::$app->request->url;
- $id = $this->filterRowOptions['id'];
- $filterSelector = "#$id input, #$id select";
- if (isset($this->filterSelector)) {
- $filterSelector .= ', ' . $this->filterSelector;
- }
-
- return [
- 'filterUrl' => Url::to($filterUrl),
- 'filterSelector' => $filterSelector,
- ];
- }
-
-
-
- public function renderItems()
- {
- $caption = $this->renderCaption();
- $columnGroup = $this->renderColumnGroup();
- $tableHeader = $this->showHeader ? $this->renderTableHeader() : false;
- $tableBody = $this->renderTableBody();
- $tableFooter = $this->showFooter ? $this->renderTableFooter() : false;
- $content = array_filter([
- $caption,
- $columnGroup,
- $tableHeader,
- $tableFooter,
- $tableBody,
- ]);
-
- return Html::tag('table', implode("\n", $content), $this->tableOptions);
- }
-
-
-
- public function renderCaption()
- {
- if (!empty($this->caption)) {
- return Html::tag('caption', $this->caption, $this->captionOptions);
- } else {
- return false;
- }
- }
-
-
-
- public function renderColumnGroup()
- {
- $requireColumnGroup = false;
- foreach ($this->columns as $column) {
-
- if (!empty($column->options)) {
- $requireColumnGroup = true;
- break;
- }
- }
- if ($requireColumnGroup) {
- $cols = [];
- foreach ($this->columns as $column) {
- $cols[] = Html::tag('col', '', $column->options);
- }
-
- return Html::tag('colgroup', implode("\n", $cols));
- } else {
- return false;
- }
- }
-
-
-
- public function renderTableHeader()
- {
- $cells = [];
- foreach ($this->columns as $column) {
-
- $cells[] = $column->renderHeaderCell();
- }
- $content = Html::tag('tr', implode('', $cells), $this->headerRowOptions);
- if ($this->filterPosition === self::FILTER_POS_HEADER) {
- $content = $this->renderFilters() . $content;
- } elseif ($this->filterPosition === self::FILTER_POS_BODY) {
- $content .= $this->renderFilters();
- }
-
- return "<thead>\n" . $content . "\n</thead>";
- }
-
-
-
- public function renderTableFooter()
- {
- $cells = [];
- foreach ($this->columns as $column) {
-
- $cells[] = $column->renderFooterCell();
- }
- $content = Html::tag('tr', implode('', $cells), $this->footerRowOptions);
- if ($this->filterPosition === self::FILTER_POS_FOOTER) {
- $content .= $this->renderFilters();
- }
-
- return "<tfoot>\n" . $content . "\n</tfoot>";
- }
-
-
-
- public function renderFilters()
- {
- if ($this->filterModel !== null) {
- $cells = [];
- foreach ($this->columns as $column) {
-
- $cells[] = $column->renderFilterCell();
- }
-
- return Html::tag('tr', implode('', $cells), $this->filterRowOptions);
- } else {
- return '';
- }
- }
-
-
-
- public function renderTableBody()
- {
- $models = array_values($this->dataProvider->getModels());
- $keys = $this->dataProvider->getKeys();
- $rows = [];
- foreach ($models as $index => $model) {
- $key = $keys[$index];
- if ($this->beforeRow !== null) {
- $row = call_user_func($this->beforeRow, $model, $key, $index, $this);
- if (!empty($row)) {
- $rows[] = $row;
- }
- }
-
- $rows[] = $this->renderTableRow($model, $key, $index);
-
- if ($this->afterRow !== null) {
- $row = call_user_func($this->afterRow, $model, $key, $index, $this);
- if (!empty($row)) {
- $rows[] = $row;
- }
- }
- }
-
- if (empty($rows)) {
- $colspan = count($this->columns);
-
- return "<tbody>\n<tr><td colspan=\"$colspan\">" . $this->renderEmpty() . "</td></tr>\n</tbody>";
- } else {
- return "<tbody>\n" . implode("\n", $rows) . "\n</tbody>";
- }
- }
-
-
-
- public function renderTableRow($model, $key, $index)
- {
- $cells = [];
-
- foreach ($this->columns as $column) {
- $cells[] = $column->renderDataCell($model, $key, $index);
- }
- if ($this->rowOptions instanceof Closure) {
- $options = call_user_func($this->rowOptions, $model, $key, $index, $this);
- } else {
- $options = $this->rowOptions;
- }
- $options['data-key'] = is_array($key) ? json_encode($key) : (string) $key;
-
- return Html::tag('tr', implode('', $cells), $options);
- }
-
-
-
- protected function initColumns()
- {
- if (empty($this->columns)) {
- $this->guessColumns();
- }
- foreach ($this->columns as $i => $column) {
- if (is_string($column)) {
- $column = $this->createDataColumn($column);
- } else {
- $column = Yii::createObject(array_merge([
- 'class' => $this->dataColumnClass ? : DataColumn::className(),
- 'grid' => $this,
- ], $column));
- }
- if (!$column->visible) {
- unset($this->columns[$i]);
- continue;
- }
- $this->columns[$i] = $column;
- }
- }
-
-
-
- protected function createDataColumn($text)
- {
- if (!preg_match('/^([^:]+)(:(\w*))?(:(.*))?$/', $text, $matches)) {
- throw new InvalidConfigException('The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"');
- }
-
- return Yii::createObject([
- 'class' => $this->dataColumnClass ? : DataColumn::className(),
- 'grid' => $this,
- 'attribute' => $matches[1],
- 'format' => isset($matches[3]) ? $matches[3] : 'text',
- 'label' => isset($matches[5]) ? $matches[5] : null,
- ]);
- }
-
-
-
- protected function guessColumns()
- {
- $models = $this->dataProvider->getModels();
- $model = reset($models);
- if (is_array($model) || is_object($model)) {
- foreach ($model as $name => $value) {
- $this->columns[] = (string) $name;
- }
- }
- }
- }
|