@@ -36,7 +36,7 @@ import { computeColWidth } from './scenegraph/layout/compute-col-width';
3636import { computeRowHeight } from './scenegraph/layout/compute-row-height' ;
3737import { defaultOrderFn } from './tools/util' ;
3838import type { IEditor } from '@visactor/vtable-editors' ;
39- import type { ColumnData , ColumnDefine } from './ts-types/list-table/layout-map/api' ;
39+ import type { ColumnData , ColumnDefine , HeaderData } from './ts-types/list-table/layout-map/api' ;
4040import { getCellRadioState , setCellRadioState } from './state/radio/radio' ;
4141import { cloneDeepSpec } from '@visactor/vutils-extension' ;
4242import { getGroupCheckboxState , setCellCheckboxState } from './state/checkbox/checkbox' ;
@@ -839,6 +839,9 @@ export class ListTable extends BaseTable implements ListTableAPI {
839839 * @returns
840840 */
841841 getHierarchyState ( col : number , row : number ) {
842+ if ( this . isHeader ( col , row ) ) {
843+ return ( this . _getHeaderLayoutMap ( col , row ) as HeaderData ) ?. hierarchyState ;
844+ }
842845 if ( ! this . options . groupBy || ( isArray ( this . options . groupBy ) && this . options . groupBy . length === 0 ) ) {
843846 const define = this . getBodyColumnDefine ( col , row ) as ColumnDefine ;
844847 if ( ! define . tree ) {
@@ -857,6 +860,34 @@ export class ListTable extends BaseTable implements ListTableAPI {
857860 toggleHierarchyState ( col : number , row : number , recalculateColWidths : boolean = true ) {
858861 this . stateManager . updateHoverIcon ( col , row , undefined , undefined ) ;
859862 const hierarchyState = this . getHierarchyState ( col , row ) ;
863+ if ( this . isHeader ( col , row ) ) {
864+ // 表头的展开和收起
865+ const headerTreeNode = this . internalProps . layoutMap . getHeader ( col , row ) as any ;
866+ const { hierarchyState : rawHierarchyState , define : columnDefine } = headerTreeNode ;
867+ if ( ! [ HierarchyState . collapse , HierarchyState . expand ] . includes ( rawHierarchyState ) || ! columnDefine ) {
868+ return ;
869+ }
870+ const children = columnDefine . columns ;
871+ // 有子节点才需要自动展开和折叠
872+ if ( ! ! Array . isArray ( children ) && children . length > 0 ) {
873+ const hierarchyState =
874+ rawHierarchyState === HierarchyState . expand ? HierarchyState . collapse : HierarchyState . expand ;
875+ headerTreeNode . hierarchyState = hierarchyState ;
876+ headerTreeNode . define . hierarchyState = hierarchyState ;
877+ // 全量更新
878+ this . updateColumns ( this . internalProps . columns ) ;
879+ }
880+
881+ this . fireListeners ( TABLE_EVENT_TYPE . TREE_HIERARCHY_STATE_CHANGE , {
882+ col,
883+ row,
884+ hierarchyState,
885+ originData : headerTreeNode ,
886+ cellLocation : this . getCellLocation ( col , row )
887+ } ) ;
888+ return ;
889+ }
890+
860891 if ( hierarchyState === HierarchyState . expand ) {
861892 this . _refreshHierarchyState ( col , row , recalculateColWidths ) ;
862893 this . fireListeners ( TABLE_EVENT_TYPE . TREE_HIERARCHY_STATE_CHANGE , {
0 commit comments