Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/Component/BaseGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ abstract class BaseGrid extends Control
/** @var callable */
protected $onDelete;
protected bool $withoutIsActiveColumn = false;
protected ?string $treeViewParentProperty = null;

public function __construct()
{
Expand All @@ -60,9 +61,38 @@ public function __construct()

$this->initGrid($grid);
$this->addIsActive($grid);

if ($this->isTreeView()) {
$grid->setTreeView(
fn ($parentId) => $this->createTreeViewChildrenDataSource($parentId),
fn ($item) => $this->treeViewItemHasChildren($item),
);
$grid->setTemplateFile(__DIR__ . '/DataGridTree.latte');
}
});
}

protected function isTreeView(): bool
{
return $this->treeViewParentProperty !== null;
}

protected function createTreeViewChildrenDataSource(mixed $parentId): mixed
{
$parent = $this->createQueryObject()->byId($parentId)->fetchOne();

$queryObject = $this->createQueryObject();
$this->initQueryObject($queryObject);
$queryObject->by($this->treeViewParentProperty, $parent);

return $this->getQueryObjectDataSourceFactory()->create($queryObject);
}

protected function treeViewItemHasChildren(object $item): bool
{
return $this->createQueryObject()->by($this->treeViewParentProperty, $item)->count() > 0;
}

protected function createQueryObject(): QueryObject
{
return $this->getDic()->getByType($this->getQueryObjectFactoryClass())->create();
Expand Down Expand Up @@ -130,6 +160,10 @@ protected function setGridDataSource(DataGrid $grid): void
$queryObject = $this->createQueryObject();
$this->initQueryObject($queryObject);

if ($this->isTreeView()) {
$queryObject->by($this->treeViewParentProperty, null);
}

$queryObjectDataSource = $this->getQueryObjectDataSourceFactory()->create($queryObject);
if ($this->getDataSourceFilterCallback()) {
$queryObjectDataSource->setFilterCallback($this->getDataSourceFilterCallback());
Expand Down Expand Up @@ -206,8 +240,15 @@ public function handleSortRows(): void
$itemId = $this->getParameter('item_id');
$nextId = $this->getParameter('next_id');
$previousId = $this->getParameter('prev_id');
$parentId = $this->getParameter('parent_id');
$item = $this->createQueryObject()->byId($itemId)->fetchOne();

if ($this->isTreeView()) {
$parent = $parentId ? $this->createQueryObject()->byId($parentId)->fetchOne() : null;
$setter = 'set' . ucfirst($this->treeViewParentProperty);
$item->$setter($parent);
}

if ($previousId) {
$previousItem = $this->createQueryObject()->byId($previousId)->fetchOne();
$newPosition = $previousItem->getPosition() + 1;
Expand Down
126 changes: 126 additions & 0 deletions src/Component/DataGridTree.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{**
* Stromové zobrazení gridu kompatibilní se starým jQuery datagrid.js (datagrid.tree extension,
* datagridSortableTree). Liší se od stock contributte šablony tím, že:
* - šipka používá `data-toggle-tree` (ne `data-bs-toggle-tree`) a jedinou ikonu rotovanou přes .toggle-rotate
* - každý kontejner `.datagrid-tree-item-children` nese `data-sortable-url` (řazení i ve vnořených)
*
* @param array $columns
* @param array $actions
* @param Row[] $rows
* @param Datagrid $control
* @param string $originalTemplate
* @param string $iconPrefix
*}

{extends $originalTemplate}

<div class="datagrid-tree-item-children datagrid-tree" n:snippet="tbody" n:block="data" {if $control->isSortable()}data-sortable-url="{plink $control->getSortableHandler()}" data-sortable-parent-path="{$control->getSortableParentPath()}"{/if}>
{snippetArea items}
<div class="datagrid-tree-item datagrid-tree-header" n:snippet="item-header">
<div class="datagrid-tree-item-content" data-has-children="">
<div class="datagrid-tree-item-left">
{foreach $columns as $key => $column}
<strong>{$column->getName()|translate}</strong>
{breakIf TRUE}
{/foreach}
</div>

<div class="datagrid-tree-item-right">
<div class="datagrid-tree-item-right-columns">
{foreach $columns as $key => $column}
{continueIf $iterator->isFirst()}
<div class="datagrid-tree-item-right-columns-column col-{$column->getColumnName()} text-{$column->hasAlign() ? $column->getAlign() : 'start'}">
<strong>{$column->getName()|translate}</strong>
</div>
{/foreach}
</div>
<div class="datagrid-tree-item-right-actions" n:if="($actions || $control->isSortable()) && $rows">
<div class="datagrid-tree-item-right-actions-action"></div>
</div>
</div>
</div>
</div>

{foreach $rows as $row}
{var $hasChildren = $control->hasTreeViewChildrenCallback() ? $control->treeViewChildrenCallback($row->getItem()) : $row->getValue($treeViewHasChildrenColumn)}
{var $item = $row->getItem()}

<div n:class="$hasChildren ? has-children, 'datagrid-tree-item'" data-id="{$row->getId()}" n:snippet="item-{$row->getId()}">
<div n:class="datagrid-tree-item-content, $row->getControlClass()" data-id="{$row->getId()}" data-has-children="{$hasChildren ? true : false}">
<div class="datagrid-tree-item-left">
<a n:href="getChildren! parent => $row->getId()" data-toggle-tree="true" n:class="!$hasChildren ? hidden, 'chevron ajax'">
<i class="{$iconPrefix}chevron-right"></i>
</a>
{foreach $columns as $key => $column}
{var $col = 'col-' . $key}
{php $column = $row->applyColumnCallback($key, clone $column)}

{if $column->hasTemplate()}
{include $column->getTemplate(), item => $item, (expand) $column->getTemplateVariables()}
{else}
{ifset #$col}
{include #$col, item => $item}
{else}
{if $column->isTemplateEscaped()}
{$column->render($row)}
{else}
{$column->render($row)|noescape}
{/if}
{/ifset}
{/if}

{breakIf TRUE}
{/foreach}
</div>
<div class="datagrid-tree-item-right">
<div class="datagrid-tree-item-right-columns">
{foreach $columns as $key => $column}
{continueIf $iterator->isFirst()}

<div class="datagrid-tree-item-right-columns-column text-{$column->hasAlign() ? $column->getAlign() : 'start'}">
{var $col = 'col-' . $key}
{php $column = $row->applyColumnCallback($key, clone $column)}

{if $column->hasTemplate()}
{include $column->getTemplate(), row => $row, item => $item, (expand) $column->getTemplateVariables()}
{else}
{ifset #$col}
{include #$col, item => $item}
{else}
{if $column->isTemplateEscaped()}
{$column->render($row)}
{else}
{$column->render($row)|noescape}
{/if}
{/ifset}
{/if}
</div>
{/foreach}
</div>
<div class="datagrid-tree-item-right-actions">
<div class="datagrid-tree-item-right-actions-action">
{foreach $actions as $key => $action}
{if $row->hasAction($key)}
{if $action->hasTemplate()}
{include $action->getTemplate(), item => $item, (expand) $action->getTemplateVariables(), row => $row}
{else}
{$action->render($row)|noescape}
{/if}
{/if}
{/foreach}

<span class="handle-sort btn btn-xs btn-default" n:if="$control->isSortable()">
<i class="{$iconPrefix}arrows {$iconPrefix}arrows-alt"></i>
</span>
</div>
</div>
</div>
</div>
<div class="datagrid-tree-item-children" {if $control->isSortable()}data-sortable-url="{plink $control->getSortableHandler()}"{/if}></div>
</div>
{/foreach}
{if !$rows}
{='contributte_datagrid.no_item_found'|translate}
{/if}
{/snippetArea}
</div>