# TreeColumn

Show a hierarchical tree.

TreeColumn class does not have the feature to open and close branch nodes, you must implement it yourself if you need it.

# Constructor Properties

Property Description Default
cache Set true when caching the calculation result of the branch structure. Please call clearCache(grid) when deleting the cached data. false

# Style Properties

Property Description
lineColor Sets the tree lines color.
lineStyle Sets the tree lines style. Allowed values ​​are 'none' or 'solid'
lineWidth Sets the with of of the tree lines.
treeIcon Sets the icon to display on the node tree. Allowed values ​​are "chevron_right", "expand_more" or "none".

# Data Format

The value provided from each record through the field must be an object of the format described below.

The object has the following properties:

Property Description
path (Required) An array of path indicating the hierarchy. See the path description section for more information.
caption The caption of the record. If not specified, the last value of the path will be used as the caption.
nodeType Set to "leaf" or "branch". Set whether the node is a leaf node or a branch node. See the nodeType description section for more information.

# path

This is a required property.
This is an array of path indicating the hierarchy. The path must contain an element that identifies the node itself.

Example:

Node code path Value
Grandparent 'g' ['g']
├ Parent 'p' ['g', 'p']
│└ Child1 'c1' ['g', 'p', 'c1']
└ Child2 'c2' ['g', 'c2']

# nodeType

Set to "leaf" or "branch". Set whether the node is a leaf node ("leaf") or a branch node ("branch"). Use this to display an icon if the node does not display any child nodes in the display. If the child nodes are displayed, they are forced to be treated as branch nodes.

# Instance Methods

# drawnIconActionArea(params)

A predicate that makes the icon an actionable area of ​​the Action class. Use it for the area property of ​​the Action class.

Parameter Description
params It's a parameter passed from the area property of ​​the Action class.

# clearCache(grid)

Clear the cache.

Parameter Description
grid It should be given an instance of a grid.

# Example

<div class="sample1 demo-grid large"></div>
1
const expands = { p1: true };

const tree = [
  {
    code: "p1",
    children: [
      {
        code: "c1_1",
        children: [
          {
            code: "d1_1_1",
          },
          {
            code: "d1_1_2",
            children: [
              {
                code: "e1_1_2_1",
              },
              {
                code: "e1_1_2_2",
              },
            ],
          },
        ],
      },
      {
        code: "c1_2",
      },
    ],
  },
  {
    code: "p2",
    children: [
      {
        code: "c2_1",
      },
      {
        code: "c2_2",
      },
    ],
  },
];

// Set the parent property to the parent node so that we can keep track of the parent node.
const buffer = [...tree];
while (buffer.length) {
  const node = buffer.shift();
  for (const child of node.children || []) {
    child.parent = node;
    buffer.push(child);
  }
}

/** Gets the specified node and its ancestor nodes. */
function* ancestors(node) {
  let n = node;
  while (n) {
    yield n;
    n = n.parent;
  }
}

const treeColumn = new cheetahGrid.columns.type.TreeColumn({
  cache: false, // cache enable. default false
});
const grid = new cheetahGrid.ListGrid({
  parentElement: document.querySelector(".sample1"),
  header: [
    {
      field: (node) => {
        // Build tree data
        const hasChildren = !!node.children?.length;

        const path = [...ancestors(node)].reverse().map((node) => node.code);

        return {
          caption: node.code,
          /**
           * An array of paths indicating the hierarchy to the record.
           * The path must contain an element that identifies the node itself.
           */
          path,
          nodeType: hasChildren ? "branch" : "leaf",
        };
      },
      caption: "Tree",
      width: 200,
      columnType: treeColumn,
      action: new cheetahGrid.columns.action.Action({
        disabled: (node) => {
          const hasChildren = !!node.children?.length;
          return !hasChildren;
        },
        action: (node) => {
          expands[node.code] = !expands[node.code];
          grid.records = buildRecords(tree);
        },
        area: treeColumn.drawnIconActionArea,
      }),
    },
    {
      field: "code",
      caption: "Code",
      width: 200,
    },
    {
      field: (node) => {
        const path = [...ancestors(node)].reverse().map((node) => node.code);
        return "[" + path.join(", ") + "]";
      },
      caption: "Path",
      width: 300,
    },
  ],
  frozenColCount: 1,
});
grid.records = buildRecords(tree);

function buildRecords(nodes) {
  const records = [];
  for (const node of nodes) {
    records.push(node);
    if (expands[node.code]) {
      records.push(...buildRecords(node.children));
    }
  }
  return records;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
Last Updated: 10/23/2024, 6:31:33 AM