Main Content

Multilevel List

This example shows two ways to create multilevel lists. The first way creates a cell array that models a multilevel list and appends the cell array to the document. The second way programmatically builds up the multilevel list by constructing List and ListItem objects.

A nested bullet list with letters and numbers. The first level has items "a" and "b." The second level under "b" lists numbers 1 to 4, with letters "a" to "d" under number 1. The first level continues with items "c" and "d."

Setup

Import the DOM packages so that you do not have to use the fully qualified class names.

import mlreportgen.dom.*

Cell Array List

Simple List

A simple list can be represented as a simple cell array where cell array elements are used to create list items. To create this simple list:

A simple bullet list with four items: "a," "b," "c," and "d."

Create this cell array.

simpleCellArray = { ...
    "a", ...
    "b", ...
    "c", ...
    "d"};

Append the cell array to a Document object. To create an HTML report, replace "pdf" with "html". To create a Word report, replace "pdf" with "docx".

d = Document("cell_simple_list", "pdf");
append(d, simpleCellArray);
close(d);
rptview(d);

Two-Level List

A two-level list can be represented as a cell array where one or more elements are cells. To create this two-level list:

A nested bullet list with letters and numbers. The first level contains "a" and "b," with numbers 1 to 4 under "b." The list continues with "c" and "d."

Create this cell array:

twoLevelCellArray = { ...
    "a", ...
    "b", ...
    { ...
        "1", ...
        "2", ...
        "3", ...
        "4" ...
    }, ...
    "c", ...
    "d"};        

Append the two-level cell array to a Document object. To create an HTML report, replace "pdf" with "html". To create a Word report, replace "pdf" with "docx".

d = Document("cell_two_level_list", "pdf");
append(d, twoLevelCellArray);
close(d);
rptview(d);

Three Level List

A three-level list can be represented as a nested cell array that is three levels deep. To create this three-level list:

A nested bullet list with letters and numbers. The first level includes "a" and "b." Under "b," there are numbers 1 to 4, with letters "a" to "d" under number 1. The first level ends with "c" and "d."

Create this cell array:

threeLevelCellArray = {
    "a", ...
    "b", ...
    { ...
        "1", ...
        { ...
            "a", ...
            "b", ...
            "c", ...
            "d" ...
        }, ...
        "2", ...
        "3", ...
        "4" ...
    }, ...
    "c", ...
    "d"};

Append the three-level cell array to a Document object. To create an HTML report, replace "pdf" with "html". To create a Word report, replace "pdf" with "docx".

d = Document("cell_three_level_list", "pdf");
append(d, threeLevelCellArray);
close(d);
rptview(d);

To create even deeper multi-level lists, add more nested cell arrays to represent inner lists.

Programmatic List

Simple List

A simple list can be constructed by creating ListItem objects and appending them to an OrderedList or UnorderedList object. For the following simple unordered list:

A simple bullet list with items "a," "b," "c," and "d."

Create ListItem objects.

itemA = ListItem("a");
itemB = ListItem("b");
itemC = ListItem("c");
itemD = ListItem("d");

Append the ListItem objects to an UnorderedList object.

unorderedList = UnorderedList();
append(unorderedList, itemA);
append(unorderedList, itemB);
append(unorderedList, itemC);
append(unorderedList, itemD);

Append the list to a Document object. To create an HTML report, replace "pdf" with "html". To create a Word report, replace "pdf" with "docx".

d = Document("prog_simple_list", "pdf");
append(d, unorderedList);
close(d);
rptview(d);

Two-Level List

A two-level list can be constructed by appending either an OrderedList or an UnorderedList object to the parent List Object. For the following two-level list:

Numbered list with four items. Item 2 has a nested bullet list with numbers 1 to 4.

Create a second level unordered list by using a cell array. To create an HTML report, replace "pdf" with "html". To create a Word report, replace "pdf" with "docx". For Word report, mixing unordered and ordered lists may not produce the best results. See the custom styled word list example.

secondLevelList = UnorderedList({ ...
    "1", ...
    "2", ...
    "3", ...
    "4"});

Create first level list.

itemA = ListItem("a");
itemB = ListItem("b");
itemC = ListItem("c");
itemD = ListItem("d");

firstLevelList = OrderedList();
append(firstLevelList, itemA);
append(firstLevelList, itemB);
% Not a ListItem, but an OrderedList
append(firstLevelList, secondLevelList); 
append(firstLevelList, itemC);
append(firstLevelList, itemD);

Append the list to a Document object.

d = Document("prog_two_level_list", "pdf");
append(d, firstLevelList);
close(d);
rptview(d);

Three-Level List

A three-level list can be constructed by appending a two-level list to the parent List Object. For the following three-level list:

Numbered list with four items, "a", "b", "c", and "d". Item 2, "b", contains a nested numbered list with two items, "a" and "b". The nested item 2, "b", has a further nested bullet list with numbers 1 to 4.

Create the third-level list.

thirdLevelList = UnorderedList({ ...
    "1", ...
    "2", ...
    "3", ...
    "4"});

Create the second-level list.

secondLevelList = OrderedList({ ...
    "a", ...
    "b", ...
    thirdLevelList, ... % This is a List, the rest are ListItems.
    "c", ...
    "d"});

Create the first-level list.

firstLevelList = OrderedList({ 
    "a", ...
    "b", ...
    secondLevelList, ...
    "c", ...
    "d"});

Append the list to a Document object. To create a Word report, replace "pdf" with "docx". To create an HTML report, replace "pdf" with "html". For Word report, mixing unordered and ordered lists may not produce the best results. See the custom styled word list example.

d = Document("prog_three_level_list", "pdf");
append(d, firstLevelList);
close(d);
rptview(d);

To create even deeper multilevel lists, append List objects to List objects.

See Also

| | |

Topics