I have a nested list structure as follows:
class Person {
public string Name;
public List<Person> Children;
public int RowSpan {
get
{ int childrenCount = 0;
/* go through children (if any) and update childrenCount */
return childrenCount == 0 ? 1 : childrenCount;
}
};
}
EDIT: my real data structure has nothing to do with parents/ children. I just choose a simple data structure to make the question easy to understand. Assume each grandpa can have any number of sons. Each son can have any number of kids. No two grandpas can share the same son. Likewise, sons can't share kids.
Edit2: I don't have issues with getting RowSpan for each cell. That is easy. The issue is: how can I parse the HTML structure of the table while iterating and traversing the data structure.
I populate a sample of this structure as follows:
Person grandpa1 = new Person { Name = "GrandPa1" };
Person grandpa2 = new Person { Name = "GrandPa2" };
Person son1 = new Person { Name = "Son 1" };
Person son2 = new Person { Name = "Son 2" };
Person grandkid1 = new Person { Name = "GrandKid1" };
Person grandkid2 = new Person { Name = "GrandKid2" };
Person grandkid3 = new Person { Name = "GrandKid3" };
grandpa1.Children = new List<Person> { son1, son2 };
son2.Children = new List<Person> { grandkid1, grandkid2, grandkid3 };
List<Person> family = new List<Person> { grandpa1, grandpa2 } ;
I am trying to represent the family structure in a HTML table, using <td rowspan={number of offsprints}>
For the above family, the output would look like:
.------------------.
| | |
| | son1 |
| |-------|-----------.
| | | grandkid1 |
| grandpa1 | |-----------.
| | | grandkid2 |
| | son2 |-----------.
| | | grandkid3 |
|----------|-------.-----------.
| grandpa2 |
.----------.
In the above table, grandpa has a rowspan of 4. son2 has a rowspan of 3.
The equivelant HTML would be something like:
table {
border-collapse: collapse;
}
td, th {
border: 1px solid black;
padding: 4px;
}
th
{
font-family: monospace;
background-color: #def;
padding: 0 2em;
}
<table>
<thead>
<tr>
<th>Grand Parents</th>
<th>Sons</th>
<th>Kids</th>
</tr>
</thead>
<tr>
<td rowspan="4"> grandpa1 </td>
<td> son1 </td>
</tr>
<tr>
<!-- no cell for grandpa1:in progress -->
<!-- no cell for son1: fulfilled -->
<td rowspan="3"> son2 </td>
<td> kid1 </td>
</tr>
<tr>
<!-- no cell for grandpa1: in progress -->
<!-- no cell for son2: in progress -->
<td> kid2 </td>
</tr>
<tr>
<!-- no cell for grandpa1: in progress -->
<!-- no cell for son2: in progress -->
<td> kid3</td>
</tr>
<tr>
<!-- no cell for grandpa1: fullfilled -->
<!-- no cell for son2: fullfilled -->
<td> grandpa2 </td>
</tr>
</table>
I am struggling to write an algorithm that generates the HTML structure described above. I tried a number of ways:
1- I generated a Queue<TagBuilder> for each level of the family and iterate through the highest level and Enqueue a TD with a rowspan of the number of dependants in the lower levels. Then I generated other queues for the other levels. At the end, my plan was to DeQueue from all levels at once and append the TDs into a fresh TR. But that doesn't work because son1 has no children, and dequeuing from level 3 would fetch kids of son2..
2- I tried to iterate though grandparents level and appending a TR for each grandparent with a single TD having the rowspan of his sons/grandsons. Then iterate through sons level and do the same, likewise in grandkids level. However the overall table will be ill-formatted.
I can easily generate the structure in Excel since I have the ability to reference cells and their merge status while iterating the first level, and can comeback to the rows from before and append new cells.
Is there an easy way to generate the family structure table from the data structure above?
RowSpanproperty to the person class, and update that as you walk through each of the items. This seems like a good use case for writing a recursive function to walk each of the relationships.