@@ -28,6 +28,7 @@ export interface DTableSelectableTypes {
2828 beforeSelectCells : ( this : DTableSelectable , cells : DTableCellPos [ ] ) => void | DTableCellPos [ ] ;
2929 ignoreDeselectOn : string ;
3030 markSelectRange : boolean ;
31+ copyHeader ?: boolean ;
3132 selectableHotkeys ?: {
3233 selectAll ?: boolean | string ;
3334 copy ?: boolean | string ;
@@ -487,6 +488,7 @@ function copySelections(this: DTableSelectable) {
487488 minRowIndex = Math . min ( pos . row , minRowIndex ) ;
488489 } ) ;
489490 const data : unknown [ ] [ ] = [ ] ;
491+ const selectedColIndexes = new Set < number > ( ) ;
490492 selectedCells . forEach ( ( pos ) => {
491493 const value = this . getCellDraftValue ? this . getCellDraftValue ( pos . row , pos . col ) : this . getCellValue ( pos . row , pos . col ) ;
492494 let rowData = data [ pos . row - minRowIndex ] ;
@@ -495,7 +497,20 @@ function copySelections(this: DTableSelectable) {
495497 data [ pos . row - minRowIndex ] = rowData ;
496498 }
497499 rowData [ pos . col - minColIndex ] = value ;
500+ selectedColIndexes . add ( pos . col ) ;
498501 } ) ;
502+ if ( this . options . copyHeader ) {
503+ const headerRow : unknown [ ] = [ ] ;
504+ selectedColIndexes . forEach ( ( colIndex ) => {
505+ const colInfo = this . getColInfo ( colIndex ) ;
506+ if ( ! colInfo ) {
507+ return ;
508+ }
509+ const value = this . getCellDraftValue ? this . getCellDraftValue ( - 1 , colInfo ) : this . getCellValue ( - 1 , colInfo ) ;
510+ headerRow [ colIndex - minColIndex ] = ( value === colInfo . name ) ? '' : value ;
511+ } ) ;
512+ data . unshift ( headerRow ) ;
513+ }
499514 const plainText = trimDataGrid ( data ) . map ( x => x . join ( '\t' ) ) . join ( '\n' ) ;
500515 navigator . clipboard . writeText ( plainText ) ;
501516 return true ;
@@ -550,7 +565,11 @@ const hotkeyHandlers: Record<string, (this: DTableSelectable, event: KeyboardEve
550565
551566const selectablePlugin : DTablePlugin < DTableSelectableTypes , [ DTableHotkeyTypes , DTableMousemoveTypes , DTableAutoscrollTypes , DTableDraftTypes ] > = {
552567 name : 'selectable' ,
553- defaultOptions : { selectable : true , markSelectRange : true } ,
568+ defaultOptions : {
569+ selectable : true ,
570+ copyHeader : true ,
571+ markSelectRange : true ,
572+ } ,
554573 when : options => ! ! options . selectable ,
555574 plugins : [ mousemove , hotkey ] ,
556575 state ( ) {
0 commit comments