/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/grid/GridView.php
170 строк
6 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\grid; use Yii; use yii\bootstrap\Html; use yii\grid\GridViewAsset; use yii\helpers\ArrayHelper; use yii\helpers\Json; use yii\widgets\Pjax; /** * Базовый класс для работы с талицами * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2020 */ class GridView extends \yii\grid\GridView { /** * @inheritdoc */ public $tableOptions = ['class' => 'table table-striped table-bordered table-hover table-condensed']; /** * Размер таблицы * @var string */ public $tableSise = 'nornal'; // nornal, medium, large /** * @inheritdoc */ public $layout = "{items}"; /** * @var boolean whether the grid view will be rendered within a pjax container. Defaults to `false`. If set to * `true`, the entire GridView widget will be parsed via Pjax and auto-rendered inside a yii\widgets\Pjax * widget container. If set to `false` pjax will be disabled and none of the pjax settings will be applied. */ public $pjax = true; /** * @var array the pjax settings for the widget. This will be considered only when [[pjax]] is set to true. The * following settings are recognized: * - `neverTimeout`: `boolean`, whether the pjax request should never timeout. Defaults to `true`. The pjax:timeout * event will be configured to disable timing out of pjax requests for the pjax container. * - `options`: _array_, the options for the [[\yii\widgets\Pjax]] widget. * - `loadingCssClass`: boolean/string, the CSS class to be applied to the grid when loading via pjax. If set to * `false` - no css class will be applied. If it is empty, null, or set to `true`, will default to * `grid-loading`. * - `beforeGrid`: _string_, any content to be embedded within pjax container before the Grid widget. * - `afterGrid`: _string_, any content to be embedded within pjax container after the Grid widget. */ public $pjaxSettings = []; /** * @inheritdoc * @throws InvalidConfigException */ public function init() { parent::init(); if (empty($this->pjaxSettings['options']['id'])) { $this->pjaxSettings['options']['id'] = $this->options['id'] . '-pjax'; } } /** * Runs the widget. */ public function run() { $id = $this->options['id']; $this->tableOptions['id'] = $id; Html::addCssClass($this->tableOptions, 'table-' . $this->tableSise); $options = Json::htmlEncode($this->getClientOptions()); $view = $this->getView(); GridViewAsset::register($view); $view->registerJs("jQuery('#$id').yiiGridView($options);"); if ($this->showOnEmpty || $this->dataProvider->getCount() > 0) { $content = preg_replace_callback("/{\\w+}/", function ($matches) { $content = $this->renderSection($matches[0]); return $content === false ? $matches[0] : $content; }, $this->layout); } else { $content = $this->renderEmpty(); } if ($this->pjax) { $this->registerAssets(); $this->beginPjax(); echo $content; $this->endPjax(); } else { return $content; } } /** * @var string the generated client script for the grid */ protected $_gridClientFunc = ''; /** * Registers client assets for the [[GridView]] widget. * @throws \Exception */ protected function registerAssets() { $view = $this->getView(); $script = '$(document).trigger(\'ajax-loaded\', \'#' . $this->pjaxSettings['options']['id'] . '\');'; $id = $this->options['id']; $options = Json::htmlEncode($this->getClientOptions()); $script .= "jQuery('#$id').yiiGridView($options);"; $this->_gridClientFunc = 'GridInit_' . hash('crc32', $script); $this->options['data-grid'] = $this->_gridClientFunc; $view->registerJs("var {$this->_gridClientFunc}=function(){\n{$script}\n};\n{$this->_gridClientFunc}();"); } /** * Begins the pjax widget rendering */ protected function beginPjax() { $view = $this->getView(); $pjaxCont = '$("#' . $this->pjaxSettings['options']['id'] . '")'; $js = $pjaxCont; if (ArrayHelper::getValue($this->pjaxSettings, 'neverTimeout', true)) { $js .= ".on('pjax:timeout', function(e){e.preventDefault()})"; } $loadingCss = ArrayHelper::getValue($this->pjaxSettings, 'loadingCssClass', 'grid-loading'); $postPjaxJs = $this->_gridClientFunc . "();"; if ($loadingCss !== false) { if ($loadingCss === true) { $loadingCss = 'grid-loading'; } $js .= ".on('pjax:send', function(){{$pjaxCont}.addClass('{$loadingCss}')})"; $postPjaxJs .= "{$pjaxCont}.removeClass('{$loadingCss}');"; } if (!empty($postPjaxJs)) { $event = 'pjax:complete.' . hash('crc32', $postPjaxJs); $js .= ".off('{$event}').on('{$event}', function(){{$postPjaxJs}})"; } if ($js != $pjaxCont) { $view->registerJs("{$js};"); } Pjax::begin($this->pjaxSettings['options']); echo '<div class="loader-overlay"><div class="loader"></div></div>'; echo ArrayHelper::getValue($this->pjaxSettings, 'beforeGrid', ''); } /** * Completes the pjax widget rendering */ protected function endPjax() { echo ArrayHelper::getValue($this->pjaxSettings, 'afterGrid', ''); Pjax::end(); } }