|
| 1 | +## Basic Table Header Grouping and Collapsing |
| 2 | + |
| 3 | +In this tutorial, we will learn how to implement multi-level header grouping and collapsing in VTable using a tree structure to visualize complex header hierarchies. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Use Cases |
| 8 | + |
| 9 | +Header grouping and collapsing are suitable for the following scenarios: |
| 10 | + |
| 11 | +- **Multidimensional Data Analysis**: Combine related fields into logical groups (e.g., "Sales Data" includes sub-columns like "Revenue" and "Profit"). |
| 12 | +- **Complex Data Structures**: Tables with explicit hierarchical relationships (e.g., "Region-Province-City" three-level structure). |
| 13 | +- **Space Optimization**: Improve readability by collapsing non-critical columns. |
| 14 | +- **Dynamic Interaction**: Allow users to expand/collapse specific groups on demand for flexible data exploration. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Implementation Steps |
| 19 | + |
| 20 | +### 1. Configure Multi-Level Header Structure |
| 21 | + |
| 22 | +Define header hierarchies using nested structures in the `columns` configuration. Each group adds sub-columns via the `columns` field to form a tree relationship. |
| 23 | + |
| 24 | +### 2. Enable Tree-Style Collapsing |
| 25 | + |
| 26 | +Set `headerHierarchyType: 'grid-tree'` to enable interactive tree-style collapsing for headers. |
| 27 | + |
| 28 | +### 3. Set Default Expansion Level |
| 29 | + |
| 30 | +Specify the initial expansion level using `headerExpandLevel` (default: `1`, showing only the first-level groups). |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +## Example |
| 35 | + |
| 36 | +```javascript livedemo template=vtable |
| 37 | +const records = [ |
| 38 | + { region: 'North', province: 'Province A', city: 'City 1', revenue: 1000, cost: 600 }, |
| 39 | + { region: 'North', province: 'Province A', city: 'City 2', revenue: 1500, cost: 800 }, |
| 40 | + { region: 'South', province: 'Province B', city: 'City 3', revenue: 2000, cost: 1100 } |
| 41 | +]; |
| 42 | + |
| 43 | +const columns = [ |
| 44 | + { |
| 45 | + title: 'Region', |
| 46 | + field: 'region', |
| 47 | + width: 150, |
| 48 | + columns: [ |
| 49 | + { |
| 50 | + title: 'Province', |
| 51 | + field: 'province', |
| 52 | + width: 150, |
| 53 | + columns: [ |
| 54 | + { |
| 55 | + title: 'City', |
| 56 | + field: 'city', |
| 57 | + width: 150 |
| 58 | + } |
| 59 | + ] |
| 60 | + } |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + title: 'Financial Metrics', |
| 65 | + field: 'metrics', |
| 66 | + width: 180, |
| 67 | + columns: [ |
| 68 | + { title: 'Revenue', field: 'revenue', width: 150 }, |
| 69 | + { title: 'Cost', field: 'cost', width: 150 } |
| 70 | + ] |
| 71 | + } |
| 72 | +]; |
| 73 | + |
| 74 | +const option = { |
| 75 | + records, |
| 76 | + columns, |
| 77 | + headerHierarchyType: 'grid-tree', // Enable tree-style collapsing |
| 78 | + headerExpandLevel: 2, // Expand to the second level by default |
| 79 | + widthMode: 'standard', |
| 80 | + defaultRowHeight: 40 |
| 81 | +}; |
| 82 | + |
| 83 | +const tableInstance = new VTable.ListTable(document.getElementById(CONTAINER_ID), option); |
| 84 | +window.tableInstance = tableInstance; |
| 85 | +``` |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## Advanced Configuration |
| 90 | + |
| 91 | +### Listen to Collapse Events |
| 92 | + |
| 93 | +Capture user interactions and execute custom logic: |
| 94 | + |
| 95 | +```javascript |
| 96 | +tableInstance.on(VTable.ListTable.EVENT_TYPE.TREE_HIERARCHY_STATE_CHANGE, args => { |
| 97 | + if (args.cellLocation === 'columnHeader') { |
| 98 | + console.log('Header state changed:', args); |
| 99 | + } |
| 100 | +}); |
| 101 | +``` |
| 102 | + |
| 103 | +### Dynamically Update Header States |
| 104 | + |
| 105 | +Programmatically control header expansion/collapse: |
| 106 | + |
| 107 | +```javascript |
| 108 | +// Toggle the expansion state of a specific column |
| 109 | +tableInstance.toggleHierarchyState(col, row); |
| 110 | +``` |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +With these configurations, you can quickly implement structured multi-level headers with dynamic interactions, ideal for complex data analysis scenarios. |
0 commit comments