Create HTML Table with Nested Collapsible Rows
This example shows how to create HTML tables with collapsible rows or with nested collapsible rows.
Create Table with Collapsible Rows
Import the mlreportgen.report
and mlreportgen.dom
namespaces so that you do not have to include the fully qualified names for the object constructors and methods.
import mlreportgen.report.* import mlreportgen.dom.*
Create an HTML document.
rpt = Report("myCollapsibleTables","html"); open(rpt);
Create the first chapter
chap1 = Chapter("Table with collapsible rows");
Specify the table data.
collapsibleTableData = { ... "Parent Row: Click to expand next 2 rows"; ... " Collapsible Content for Parent Row"; ... " More Collapsible Content for Parent Row"; ... "Static Row" ... };
Create the table and define basic styling. Use WhiteSpace("preserve")
to keep the leading whitespaces in the second and third rows.
tbl = Table(collapsibleTableData); tbl.Border = "solid"; tbl.RowSep = "solid"; tbl.ColSep = "solid"; tbl.Style = [tbl.Style {WhiteSpace("preserve")}];
Make the first row of the table collapsible, so that clicking on it collapses the following two rows.
row1 = tbl.row(1); row1.Style = [row1.Style {Collapsible(2)}];
Append the table to the chapter and the chapter to the report.
append(chap1,tbl); append(rpt,chap1);
Create Table with Nested Collapsible Rows
Create a second chapter.
chap2 = Chapter("Table with nested collapsible rows");
Specify the table data for a second table.
collapsiblenestedTableData = { ... "Parent Row: Click to expand next 3 rows"; ... " Collapsible Content for Parent Row"; ... " More Collapsible Content for Parent Row"; ... " Sub-Parent Row: : Click to expand next 2 rows" " Nested Collapsible Content for Sub-Parent Row"; ... " Nested More Collapsible Content for Sub-Parent Row"; ... "Static Row" ... };
Create the table.
tbl2 = Table(collapsiblenestedTableData); tbl2.Border = "solid"; tbl2.RowSep = "solid"; tbl2.ColSep = "solid"; tbl2.Style = [tbl2.Style {WhiteSpace("preserve")}];
For the fourth row, create a nested collapsible row that contains the next two rows.
row4 = tbl2.row(4); row4.Style = [row4.Style {Collapsible(2)}];
Make the first row of the table collapsible, so that clicking on it collapses the second, third, and fourth rows, including the rows nested under the fourth row.
row1 = tbl2.row(1); row1.Style = [row1.Style {Collapsible(5)}];
Append the nested table to the chapter and the chapter to the report.
append(chap2,tbl2); append(rpt,chap2);
Close and view the report.
close(rpt); rptview(rpt);
To open or close a group of collapsible rows, click the parent row. When you point to a collapsed row, the row has gray highlighting.
This image shows the table in the first chapter fully expanded and with the second row collapsed.
Table with all rows fully expanded | Table with the second row collapsed |
This image shows the table in the second chapter fully expanded, the fourth row collapsed, and the second and fourth row collapsed.
Table with all rows fully expanded | Table with the fourth row collapsed | Table with the second and fourth row collapsed |