主要内容

本页采用了机器翻译。点击此处可查看英文原文。

创建带嵌套可折叠行的 HTML 表

此示例演示了如何创建带有可折叠行或嵌套可折叠行的 HTML 表。

创建带可折叠行的表

导入 mlreportgen.reportmlreportgen.dom 命名空间,这样就无需为对象构造函数和方法包含完全限定名称。

import mlreportgen.report.*
import mlreportgen.dom.*

创建一个 HTML 文档。

rpt = Report("myCollapsibleTables","html");
open(rpt);

创建第一章

chap1 = Chapter("Table with collapsible rows");

指定表数据。

collapsibleTableData = { ...
    "Parent Row: Click to expand next 2 rows"; ...
    "  Collapsible Content for Parent Row"; ...
    "  More Collapsible Content for Parent Row"; ...
    "Static Row" ...
    };

创建表并设置基本样式。使用 WhiteSpace("preserve") 保持第二行和第三行的首行空格。

tbl = Table(collapsibleTableData);
tbl.Border = "solid";
tbl.RowSep = "solid";
tbl.ColSep = "solid";
tbl.Style = [tbl.Style {WhiteSpace("preserve")}];

将表的第一行设置为可折叠,以便点击该行时,以下两行可折叠。

row1 = tbl.row(1);
row1.Style = [row1.Style {Collapsible(2)}];

将表附加到章节,将章节附加到报告。

append(chap1,tbl);
append(rpt,chap1);

创建带嵌套可折叠行表

创建第二个章节。

chap2 = Chapter("Table with nested collapsible rows");

指定第二个表的表数据。

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" ...
    };

创建表格。

tbl2 = Table(collapsiblenestedTableData);
tbl2.Border = "solid";
tbl2.RowSep = "solid";
tbl2.ColSep = "solid";
tbl2.Style = [tbl2.Style {WhiteSpace("preserve")}];

对于第四行,创建一个嵌套的可折叠行,其中包含接下来的两行。

row4 = tbl2.row(4);
row4.Style = [row4.Style {Collapsible(2)}];

使表的第一行可折叠,以便点击它时,第二、第三和第四行(包括第四行下嵌套的行)都会折叠。

row1 = tbl2.row(1);
row1.Style = [row1.Style {Collapsible(5)}];

将嵌套表附加到章节,并将章节附加到报告。

append(chap2,tbl2);
append(rpt,chap2);

关闭并查看报告。

close(rpt);
rptview(rpt);

要打开或关闭一组可折叠行,请点击父行。当您将鼠标悬停在折叠的行上时,该行会显示灰色高亮。

此图展示了第一章中的表完全展开且第二行折叠的状态。

Chapter 1 heading followed by a table with 4 rows. The rows describe their function as parent, collapsible, or static.

所有行均已展开的表

Chapter 1 heading followed by a table with 2 of the 4 rows collapsed.

表,第二行已折叠

此图展示了第二章中的表完全展开的状态,第四行已折叠,第二行和第四行均已折叠。

Chapter 2 heading followed by a table with 7 rows. The row text describes their function as parent, collapsible, or static and whether they are part of the nested collapsible rows.

所有行均已展开的表

Chapter 2 heading followed by a table with the nested rows collapsed.

表,第四行已折叠

Chapter 2 heading followed by a table with both the top level and nested rows collapsed.

表,第二行和第四行已折叠

另请参阅