perspectives: show nested columns

This commit is contained in:
Jan Prochazka
2022-06-30 10:38:03 +02:00
parent b6b75f0743
commit 301222d118
2 changed files with 58 additions and 25 deletions

View File

@@ -1,37 +1,68 @@
import { PerspectiveTableNode, PerspectiveTreeNode } from './PerspectiveTreeNode'; import { PerspectiveTableNode, PerspectiveTreeNode } from './PerspectiveTreeNode';
import _max from 'lodash/max';
export class PerspectiveDisplayColumn { export class PerspectiveDisplayColumn {
subColumns: PerspectiveDisplayColumn[] = [];
title: string; title: string;
dataField: string; dataField: string;
parentColumns: string[] = [];
display: PerspectiveDisplay;
colSpanAtLevel = {};
get rowSpan() {
return this.display.columnLevelCount - this.parentColumns.length;
}
showParent(level: number) {
return !!this.colSpanAtLevel[level];
}
getColSpan(level: number) {
return this.colSpanAtLevel[level];
}
isVisible(level: number) {
return level == this.columnLevel;
}
get columnLevel() {
return this.parentColumns.length;
}
constructor() {} constructor() {}
} }
export class PerspectiveDisplay { export class PerspectiveDisplay {
columns: PerspectiveDisplayColumn[] = []; columns: PerspectiveDisplayColumn[] = [];
readonly columnLevelCount: number;
constructor(public root: PerspectiveTreeNode, public rows: any[]) { constructor(public root: PerspectiveTreeNode, public rows: any[]) {
const children = root.childNodes; this.fillChildren(root.childNodes, []);
this.fillChildren(root.childNodes, this.columns); this.columnLevelCount = _max(this.columns.map(x => x.parentColumns.length)) + 1;
} }
fillChildren(children: PerspectiveTreeNode[], columns: PerspectiveDisplayColumn[]) { fillChildren(children: PerspectiveTreeNode[], parentColumns: string[]) {
for (const child of children) { for (const child of children) {
if (child.isChecked) { if (child.isChecked) {
const childColumn = this.nodeToColumn(child); this.processColumn(child, parentColumns);
columns.push(childColumn);
} }
} }
} }
nodeToColumn(node: PerspectiveTreeNode) { processColumn(node: PerspectiveTreeNode, parentColumns: string[]) {
const res = new PerspectiveDisplayColumn();
res.title = node.columnTitle;
res.dataField = node.dataField;
if (node.isExpandable) { if (node.isExpandable) {
this.fillChildren(node.childNodes, res.subColumns); const countBefore = this.columns.length;
this.fillChildren(node.childNodes, [...parentColumns, node.title]);
if (this.columns.length > countBefore) {
this.columns[countBefore].colSpanAtLevel[parentColumns.length] = this.columns.length - countBefore;
}
} else {
const column = new PerspectiveDisplayColumn();
column.title = node.columnTitle;
column.dataField = node.dataField;
column.parentColumns = parentColumns;
column.display = this;
this.columns.push(column);
} }
return res;
} }
} }

View File

@@ -6,7 +6,7 @@
PerspectiveDisplay, PerspectiveDisplay,
PerspectiveTreeNode, PerspectiveTreeNode,
} from 'dbgate-datalib'; } from 'dbgate-datalib';
import _ from 'lodash'; import _, { range } from 'lodash';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { prop_dev } from 'svelte/internal'; import { prop_dev } from 'svelte/internal';
@@ -60,7 +60,8 @@
const rows = []; const rows = [];
await loadLevelData(node, rows); await loadLevelData(node, rows);
dataRows = rows; dataRows = rows;
// console.log('RESULT', rows);
console.log('DISPLAY ROWS', rows);
// const rows = await node.loadLevelData(); // const rows = await node.loadLevelData();
// for (const child of node.childNodes) { // for (const child of node.childNodes) {
// const loadProps = []; // const loadProps = [];
@@ -78,17 +79,18 @@
{#if display} {#if display}
<table> <table>
<thead> <thead>
<!-- {#each display.columnLevels as columnLevel} {#each _.range(display.columnLevelCount) as columnLevel}
<tr> <tr>
{#each display.columns as column}
</tr> {#if column.isVisible(columnLevel)}
{/each} --> <th rowspan={column.rowSpan}>{column.title}</th>
{/if}
<tr> {#if column.showParent(columnLevel)}
{#each display.columns as column} <th colspan={column.getColSpan(columnLevel)}>{column.parentColumns[columnLevel]}</th>
<th>{column.title}</th> {/if}
{/each} {/each}
</tr> </tr>
{/each}
</thead> </thead>
<tbody> <tbody>
{#each display.rows as row} {#each display.rows as row}