MCPcopy Create free account
hub / github.com/handsontable/handsontable / removeCol

Method removeCol

handsontable/src/dataMap/dataMap.ts:626–668  ·  view source on GitHub ↗

* Removes column from the data array. * * @fires Hooks#beforeRemoveCol * @fires Hooks#afterRemoveCol * @param {number} [index] Visual index of the column to be removed. If not provided, the last column will be removed. * @param {number} [amount=1] Amount of the columns to be removed.

(index: number, amount = 1, source: string)

Source from the content-addressed store, hash-verified

624 * @returns {boolean} Returns `false` when action was cancelled, otherwise `true`.
625 */
626 removeCol(index: number, amount = 1, source: string) {
627 if (this.hot!.dataType === 'object' || this.tableMeta.columns) {
628 throwWithCause('cannot remove column with object data source or columns option specified');
629 }
630 let columnIndex = typeof index !== 'number' ? -amount : index;
631
632 columnIndex = (this.hot!.countCols() + columnIndex) % this.hot!.countCols();
633
634 const removedPhysicalIndexes = this.visualColumnsToPhysical(columnIndex, amount);
635 const descendingPhysicalColumns = removedPhysicalIndexes.slice(0).sort((a, b) => b - a);
636 const actionWasNotCancelled = this.hot!.runHooks(
637 'beforeRemoveCol', columnIndex, amount, removedPhysicalIndexes, source
638 );
639
640 if (actionWasNotCancelled === false) {
641 return false;
642 }
643
644 let isTableUniform = true;
645 const removedColumnsCount = descendingPhysicalColumns.length;
646 const data = this.dataSource ?? [];
647
648 for (let c = 0; c < removedColumnsCount; c++) {
649 if (isTableUniform && removedPhysicalIndexes[0] !== removedPhysicalIndexes[c] - c) {
650 isTableUniform = false;
651 }
652 }
653
654 this.#spliceRemovedColumns(data, isTableUniform, removedPhysicalIndexes, descendingPhysicalColumns, amount);
655
656 if (columnIndex < this.hot!.countCols()) {
657 this.hot!.columnIndexMapper.removeIndexes(removedPhysicalIndexes);
658
659 if (!this.tableMeta.rowHeaders && this.hot!.columnIndexMapper.getNotTrimmedIndexesLength() === 0) {
660 this.hot!.rowIndexMapper.setIndexesSequence([]);
661 }
662 }
663
664 this.hot!.runHooks('afterRemoveCol', columnIndex, amount, removedPhysicalIndexes, source);
665 this.refreshDuckSchema();
666
667 return true;
668 }
669
670 /**
671 * Splices the removed physical columns from each row in the data source,

Callers 1

removeColFunction · 0.80

Calls 11

#spliceRemovedColumnsMethod · 0.95
refreshDuckSchemaMethod · 0.95
throwWithCauseFunction · 0.90
removeIndexesMethod · 0.80
setIndexesSequenceMethod · 0.80
countColsMethod · 0.65
runHooksMethod · 0.65
sortMethod · 0.45
sliceMethod · 0.45

Tested by

no test coverage detected