Below is my json structure:
{
"list": [
{
"Variant": "V1",
"Id": 1,
"TestList": [
{
"SubVariantList": [
{
"Id": 10,
"Name": "A",
"Fluctuation":100
},
{
"Id": 20,
"Name": "B",
"Fluctuation":100
},
{
"Id": 30,
"Name": "C",
"Fluctuation":200
}
]
}
]
},
{
"Variant": "V2",
"Id": 2,
"TestList": [
{
"SubVariantList": [
{
"Id": 40,
"Name": "A",
"Fluctuation":1100
},
{
"Id": 50,
"Name": "B",
"Fluctuation":1200
},
{
"Id": 60,
"Name": "C",
"Fluctuation":1300
}
]
}
]
},
{
"Variant": "V3",
"Id": 3,
"TestList": [
{
"SubVariantList": [
{
"Id": 40,
"Name": "A",
"Fluctuation":500
},
{
"Id": 50,
"Name": "C",
"Fluctuation":600
}
]
}
]
}
]
}
Now with json structure I have 2 below travels:
V1:A - B - C
V2:A - C
Based on above json structure I am creating dynamic table like below:
Table1:
A - B | B - C (Headers)
VariantName AValue BValue CValue (Columns)
V1 100 100 200
V2 1100 1200 1300
Table2:
A - C (Headers)
VariantName AValue CValue (columns)
V3 500 600
I will always have subvariants in fixed order starting from 1 point like below:
A - B - C - D
First A will be fixed, and rest I can have combinations like this:
A - B - C - D
A - B - C
A - C
A- D
A - B - C - D
A - B - C
A - D
A- C
Problem comes with some combinations of subvariants flow like below:
V1: A - B - C
V2 : A - B - C - D
With above data I am getting table structure like this:
Table1 : <thead>
A - B | B - C
Table2:<thead>
A - B | B - C | C - D
But already A - B and B - C structure is created in table as sequence is right so there will be only 1 table like below:
Table1 : <thead>
A - B | B - C | C - D
New table will be created only when there will be jumping like below:
A - C
A - D
This is my code:
<div>
@{
var tableHeaders = new List<string>();
for (int i = 0; i < Model.Count; i++)
{
var subvariants = string.Join(",", Model[i].TestList[0].SubVariantList.Select(cd => cd.Name)); //creating unique combination
if (tableHeaders.Count == 0)
{
tableHeaders.Add(subvariants);
}
else if (!tableHeaders.Contains(subvariants))
{
tableHeaders.Add(subvariants);
}
}
}
@for (int i = 0; i < tableHeaders.Count; i++)
{
string[] subvariants = tableHeaders[i].Split(',');
<table>
<thead>
<tr>
@for (int cnt = 0; cnt < tableHeaders.Count() - 1; cnt++) // for 2 subvariants
{
<td>
@subvariants[cnt] <span>-</span> @subvariants[cnt + 1]
</td>
}
</tr>
<tr>
<th style="border: 1px solid black;">VariantName</th>
@for (int cnt = 0; cnt < subvariants.Count() - 1; cnt++)
{
if (cnt == 0)
{
<th style="border: 1px solid black;">@subvariants[cnt]</th>
}
<th style="border: 1px solid black;">@subvariants[cnt + 1]</th>
}
</tr>
</thead>
<tbody>
@for (int m = 0; m < Model.Count; m++)
{
string subvariants = string.Join(",", Model[m].TestList[0].SubVariantList.Select(cd => cd.Name));
var headers = tableHeaders[i];
if (headers.Contains(subvariants))
{
<tr>
<td>@Model[m].Variant</td>
<td>@Model[m].TestList[0].SubVariantList[0].Value</td>
@for (int j = 1; j < Model[m].TestList[0].SubVariantList.Count; j++)
{
<td>@Model[m].TestList[0].SubVariantList[j].Value</td>
}
</tr>
}
}
</tbody>
</table>
}
</div>
I wonder if I am stuck in this complex logic because of various scenarios and combination of subvariants.
A - B - C - DandA - CandA - D?B - D, and if so, would you need a table for that also?