Skip to content

CGridTreeColumn

Defines tree column.

Note

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

See also the TreeColumn document.

Vue Template Structure

vue
<template>
  <div class="demo-grid small">
    <c-grid :data="records" :frozen-col-count="1">
      <c-grid-tree-column :field="treeField" width="200" @click="onClickRecord">
        Tree
      </c-grid-tree-column>
      <c-grid-column field="code" width="200"> Code </c-grid-column>
    </c-grid>
  </div>
</template>
<script>
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;
  }
}

export default {
  data() {
    return {
      expands: { p1: true },
    };
  },
  computed: {
    records() {
      const vm = this;

      return buildRecords(tree);

      function buildRecords(nodes) {
        const records = [];
        for (const node of nodes) {
          records.push(node);
          if (vm.expands[node.code]) {
            records.push(...buildRecords(node.children));
          }
        }
        return records;
      }
    },
  },
  methods: {
    treeField(node) {
      const hasChildren = !!node.children?.length;
      const path = [...ancestors(node)].reverse().map((node) => node.code);
      return {
        caption: node.code,
        path,
        nodeType: hasChildren ? "branch" : "leaf",
      };
    },
    onClickRecord(node) {
      this.expands[node.code] = !this.expands[node.code];
    },
  },
};
</script>

Slots

default slot

Use this slot to set the header caption

Properties

Optional Properties

NameTypeDescriptionDefault
fieldobject|string|functionDefines a column data fieldundefined
widthnumber|stringDefines a default column widthundefined
min-widthnumber|stringDefines a column min widthundefined
max-widthnumber|stringDefines a column max widthundefined
column-styleobject|string|functionDefines a column style. Same as the style property of the JS API.undefined
captionstring|functionDefines a header caption''
disabledboolean|functionDefines disabled. You can also control each record by specifying a function.false
colspannumber|stringDefines the layout colspan.
This property can be used when defining in the layout-header and layout-body slots.
undefined
filterstring|functionDefines a vue filter nameundefined
header-actionobject|string|functionDefines a column header action. Same as the headerAction property of the JS API.undefined
header-fieldstringDefines a column header data fieldundefined
header-iconobject|stringDefines a header iconundefined
header-styleobject|string|functionDefines a column header style. Same as the headerStyle property of the JS APIundefined
header-typeobject|string|functionDefines a column header type. Same as the headerStyle property of the JS APIundefined
iconobject|string|functionDefines an icon. Same as the icon property of the JS API.undefined
messageobject|string|functionDefines a message generation method. Same as the message property of the JS API.undefined
rowspannumber|stringDefines the layout rowspan.
This property can be used when defining in the layout-header and layout-body slots.
undefined
sortboolean|string|functionDefines a sort. See "Sort by Column" for detail.undefined

Events

NameDescription
clickFired when a click on cell.

Methods

NameReturn TypeDescription
invalidate---Redraws the whole grid.