123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- <?php
- namespace backend\widgets\grid;
- use Closure;
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\base\Widget;
- use yii\grid\DataColumn;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Html;
- use yii\helpers\Json;
- use yii\i18n\Formatter;
- class TreeGrid extends Widget
- {
-
- public $dataProvider;
-
- public $dataColumnClass;
-
- public $options = ['class' => 'table table-striped table-bordered'];
-
- public $pluginOptions = [];
-
- public $headerRowOptions = [];
-
- public $footerRowOptions = [];
-
- public $emptyCell = ' ';
-
- public $emptyText;
-
- public $emptyTextOptions = ['class' => 'empty'];
-
- public $showHeader = true;
-
- public $showFooter = false;
-
- public $showOnEmpty = true;
-
- public $formatter;
-
- public $rowOptions = [];
-
- public $beforeRow;
-
- public $afterRow;
-
- public $keyColumnName;
-
- public $parentColumnName;
-
- public $parentRootValue = null;
-
- public $columns = [];
-
- public function init()
- {
- if ($this->dataProvider === null) {
- throw new InvalidConfigException('The "dataProvider" property must be set.');
- }
- if ($this->emptyText === null) {
- $this->emptyText = Yii::t('yii', 'No results found.');
- }
- if (!isset($this->options['id'])) {
- $this->options['id'] = $this->getId();
- }
- 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 (!$this->keyColumnName) {
- throw new InvalidConfigException('The "keyColumnName" property must be specified"');
- }
- if (!$this->parentColumnName) {
- throw new InvalidConfigException('The "parentColumnName" property must be specified"');
- }
- $this->initColumns();
- }
-
- public function run()
- {
- $id = $this->options['id'];
- $options = Json::htmlEncode($this->pluginOptions);
- $view = $this->getView();
- TreeGridAsset::register($view);
- $view->registerJs("jQuery('#$id').treegrid($options);");
- if ($this->showOnEmpty || $this->dataProvider->getCount() > 0) {
- $header = $this->showHeader ? $this->renderTableHeader() : false;
- $body = $this->renderItems();
- $footer = $this->showFooter ? $this->renderTableFooter() : false;
- $content = array_filter([
- $header,
- $body,
- $footer
- ]);
- return Html::tag('table', implode("\n", $content), $this->options);
- } else {
- return $this->renderEmpty();
- }
- }
-
- public function renderEmpty()
- {
- $options = $this->emptyTextOptions;
- $tag = ArrayHelper::remove($options, 'tag', 'div');
- return Html::tag($tag, ($this->emptyText === null ? Yii::t('yii', 'No results found.') : $this->emptyText), $options);
- }
-
- 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;
- $id = ArrayHelper::getValue($model, $this->keyColumnName);
- Html::addCssClass($options, "treegrid-$id");
- $parentId = ArrayHelper::getValue($model, $this->parentColumnName);
- if ($parentId) {
- if(ArrayHelper::getValue($this->pluginOptions, 'initialState') == 'collapsed'){
- Html::addCssStyle($options, 'display: none;');
- }
- Html::addCssClass($options, "treegrid-parent-$parentId");
- }
- return Html::tag('tr', implode('', $cells), $options);
- }
-
- public function renderTableHeader()
- {
- $cells = [];
- foreach ($this->columns as $column) {
-
- $cells[] = $column->renderHeaderCell();
- }
- $content = Html::tag('tr', implode('', $cells), $this->headerRowOptions);
- 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);
- return "<tfoot>\n" . $content . "\n</tfoot>";
- }
-
- public function renderItems()
- {
- $rows = [];
- $this->dataProvider->setKeys([]);
- $models = array_values($this->dataProvider->getModels());
- $models = $this->normalizeData($models, $this->parentRootValue);
- $this->dataProvider->setModels($models);
- $this->dataProvider->setKeys(null);
- $this->dataProvider->prepare();
- $keys = $this->dataProvider->getKeys();
- 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 "<tr><td colspan=\"$colspan\">" . $this->renderEmpty() . "</td></tr>";
- } else {
- return implode("\n", $rows);
- }
- }
-
- 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 ? : TreeColumn::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 ? : TreeColumn::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[] = $name;
- }
- }
- }
-
- protected function normalizeData(array $data, $parentId = null) {
- $result = [];
- foreach ($data as $element) {
- if (ArrayHelper::getValue($element, $this->parentColumnName) === $parentId) {
- $result[] = $element;
- $children = $this->normalizeData($data, ArrayHelper::getValue($element, $this->keyColumnName));
- if ($children) {
- $result = array_merge($result, $children);
- }
- }
- }
- return $result;
- }
- }
|