logo

S2

  • Manual
  • API
  • Examples
  • Playground
  • ChangeLog
  • Productsantv logo arrow
  • 2.x
  • Introduction
  • Quick Start
  • Basic Tutorial
    • Base Concept
    • Sheet Type
      • Pivot Mode
        Updated
      • Table Mode
    • Conditions
      Updated
    • Totals
    • Sort
      • Custom Sort
        Updated
      • Group Sort
        Updated
    • Theme
      Updated
    • Tooltip
      Updated
    • Internationalization
    • 数据格式化
      New
    • Multi Line
      New
    • Pagination
      New
  • Advanced Tutorial
    • Advanced Tutorial
      • Link
      • Render Chart In Cell
      • Draw Chart With @antv/g2
    • Custom
      • Customize Hook
        Updated
      • Customize Tree
        New
      • Customize Icon
      • Customize Cell Alignment
      • Customize Cell Size
        Updated
      • Customize Order
      • Custom Collapse/Expand Nodes
    • Interaction
      • Basic Interaction
      • Custom Interaction
      • Hide Columns
      • Cell Resize
      • Merge Cell
      • Scroll
      • Copy
        New
      • Highlight and select cell
        New
    • Analyze Component
      • Introduction
        New
      • Advanced Sort
        Updated
      • Switcher
      • Export
        Updated
      • Pagination
      • Drill Down
    • Sheet Component
      • Editable Mode
      • Strategy Sheet
        Updated
    • HD Adapter
    • Get Instance
    • Get Cell Data
      Updated
    • Table adaptive
    • AntV/G Plugins
      New
    • Pivot Chart
      Experimental
    • Vue 3.0
  • Extended Reading
    • Data Process
      • Pivot
      • Table
    • Layout
      • Pivot
      • Table
    • Performance
      Updated
  • Contribution
  • FAQ
  • S2 2.0 Migration Guide

Custom Collapse/Expand Nodes

Previous
Customize Order
Next
Basic Interaction

Resources

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-互动图形解决方案
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

S2 supports configuring collapsed/expanded nodes in the tree structure, and expands all nodes by default.

Tiling mode, follow-up support


const s2Options = {
hierarchyType: 'tree',
style: {
rowCell: {
// 折叠节点
collapseFields: {},
// 展开层级
expandDepth: 0,
// 折叠所有
collapseAll: true
},
},
}

Collapse the specified node

Configure collapseFields to support two ways of维值id and维度field , that is, root[&] 浙江省and city

const s2Options = {
style: {
rowCell: {
collapseFields: {},
},
},
}

According to the dimension value id corresponding to the node

Specify id to fold the specified node, if configured as root[&] 浙江省, then all nodes浙江省will be folded

const s2Options = {
style: {
rowCell: {
collapseFields: { 'root[&] 浙江省': true },
},
},
}

preview

According to the dimension field corresponding to the node

Specifies that the field can collapse the node corresponding to the dimension of the node. For example, if the line header is configured with two dimensions of province and city , and collapseFields is configured as ['city'] , then all cities will be collapsed

const s2DataConfig = {
fields: {
rows: ['province', 'city']
}
}
const s2Options = {
style: {
rowCell: {
collapseFields: { province: false, city: true }
},
},
}

preview
const s2Options = {
style: {
dataCell: {
width: 100,
height: 90
},
},
}

collapse all nodes

Just configure collapseAll , the priority is lower than collapseFields , see configuration priority for details

const s2Options = {
style: {
rowCell: {
collapseAll: true,
},
},
}

preview

default expansion level

The table expands all nodes by default. collapseFields needs to know the corresponding node id or field in advance. When you don’t care about the specific node but only the node level, you can use the expandDepth syntactic sugar to configure the expansion level (starting from 0) with a lower priority than collapseFields . For details, see configuration priority

const s2Options = {
style: {
rowCell: {
// 展开两层
expandDepth: 1,
},
},
}

preview

configuration priority

S2 provides three folding/expanding related configurations to meet different usage scenarios, and the priorities are as follows:

collapseFields > expandDepth > collapseAll

If you want collapseAll to take effect, you can set collapseFields and expandDepth to null

const s2Options = {
style: {
rowCell: {
collapseFields: null, // 无效
collapseAll: true, // 生效
expandDepth: null, // 无效
},
},
}

API documentation

style

object is required , default: null Function description: style setting

parametertyperequiredDefaultsFunctional description
layoutWidthTypeadaptive | colAdaptive | compactCell Width Layout Type
adaptive : Rows and columns are equal in width, equally dividing the entire Canvas canvas width
colAdaptive : Equal width of columns, compact layout of line headers, the remaining width of the column equally divided canvas width minus the width of line headers
compact : compact row and column layout, when the indicator dimension is small, it cannot cover the entire canvas
dataCellDataCellValue cell configuration
colCellColCellColumn header cell configuration
rowCellRowCellRow header cell configuration

DataCell

object is required , default: null Function description: Numerical cell configuration

parameterillustratetypeDefaultsrequired
widthcell widthnumber96-
heightcell heightnumber30-
valuesCfgcell configuration{ originalValueField?: string, widthPercent?: number[], showOriginalValue?: boolean }-

ColCell

object is required , default: null Function description: Column header cell configuration

parameterillustratetypeDefaultsrequired
widthCell width, which can be dynamically set according to the current column header node (leaf nodes are valid)number | (colNode: Node) => number96
heightCell height, which can be dynamically set according to the current column header node (leaf nodes are valid)number | (colNode: Node) => number30
widthByFieldSet the width according to the measurement value (drag or preset width scene), field corresponds to the field or column header id in s2DataConfig.fields.columns , see detailsRecord<string, number>-
heightByFieldSet the height according to the measurement value (drag or preset height scene), the field corresponds to the field or column header id in s2DataConfig.fields.columns , see detailsRecord<string, number>-
hideValueThe default value hangs the column header, which will display the column header and the value at the same time, and hide the value to make it more beautiful. (that is, s2DataConfig.fields.values and it is only valid for a single value, and it is recommended to use hidden column headers for multiple values)booleanfalse

RowCell

object is required , default: null Function description: row header cell configuration

parameterillustratetypeDefaultsrequired
widthThe row cell width can be dynamically set according to the current row header node, and the tree structure is also applicablenumber | (rowNode: Node) => numberTiles: 96 , Trees: 120
heightRow cell height, which can be dynamically set according to the current row header nodenumber | (rowNode: Node) => number30
collapseFieldsCustom folded nodes for row headers in tree mode.
Supports two formats: id ( 'root[&] 行头维度值' ) and dimension field ( 'city' ). The priority is higher than collapseAll and expandDepth , and the priority is the lowest when it is set to null . view demo
Record<string, boolean>
collapseAllWhether to collapse all line headers by default in tree structure mode.booleanfalse
expandDepthIn the tree structure mode, the row header expands the expanded level by default (the level starts from 0), and when it is set to null , the priority is the lowestnumber
showTreeLeafNodeAlignDotIn the tree mode, whether the row header leaf nodes display the hierarchical placeholdersbooleanfalse
withByFieldSet the width of each line according to field . field corresponds to the field or column header id in s2DataConfig.fields.rows , see detailsRecord<string, number>-
heightByFieldSet the height of each row according to field .
1. Pivot table: field corresponds to field or column header id in s2DataConfig.fields.rows .
2. Detailed table: field corresponds to the row number, starting from 1 . check the details
Record<string, number>-