Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to do the layout as in your example, with colspans to align the cells then I don't see any way to do so without first finding out, or knowing in advance (hard coding), the max depth of the tree - you will need this to determine the total number of columns used for departments.</p> <p>Assumptions of some table properties (will help understand the code below):</p> <ul> <li>Only the rightmost cells span multiple columns, all others have colWidth 1</li> <li>The rightmost cells never span multiple rows, always have colHeight 1</li> <li>There exists some function to compute maximum width, or alternatively max column index, for the 'department' part of the table (to set the variable <code>blockWidth</code> or <code>maxY</code> in the code)</li> </ul> <p>Pseudocode to layout the table:</p> <pre><code>displayDeptTree(Department root) { startX, startY: beginning coord of the root dept blockWidth: total no. of columns (tree depth + 1, or hardcoded to value &gt; possible depth) maxY = startY + blockWidth - 1 // global var? addDeptIntoCell(root, startX, startY) } // returns height of the cell int addDeptIntoCell(Department d, x, y) { colHeight = 1 if (d-&gt;subDeps is empty) { colWidth = maxY - y addDepartment(d, x, y, colHeight, colWidth) // colHeight = 1 always } else { currY = y for (Dept child : d-&gt;subDeps) { childHeight = addDeptIntoCell(child, x + 1, currY); // increment x pos for children currY += childHeight } colHeight = currY - y addDepartment(d, x, colHeight, 1) } return colHeight } addDepartment(Department d, x, y, height, width) { // Actually adds the department info into table, parameters should be self-explanatory, implementation not shown } </code></pre> <p>This may have off by one errors etc., and I'm using d->subDeps in the Java sense not C++ pointer deference, but you should get the general idea :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload