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

Method createCol

handsontable/src/dataMap/dataMap.ts:463–525  ·  view source on GitHub ↗

* Creates column at the right of the data array. * * @param {number} [index] Visual index of the column before which the new column will be inserted. * @param {number} [amount=1] An amount of columns to add. * @param {object} [options] Additional options for created columns. * @param

(index: number | undefined, amount = 1,
            { source, mode = 'start' }: { source?: string; mode?: 'start' | 'end' } = {})

Source from the content-addressed store, hash-verified

461 * @returns {number} Returns number of created columns.
462 */
463 createCol(index: number | undefined, amount = 1,
464 { source, mode = 'start' }: { source?: string; mode?: 'start' | 'end' } = {}) {
465 if (!this.hot!.isColumnModificationAllowed()) {
466 throwWithCause('Cannot create new column. When data source in an object, ' +
467 // eslint-disable-next-line max-len
468 'you can only have as much columns as defined in first data row, data schema or in the \'columns\' setting.' +
469 'If you want to be able to add new columns, you have to use array datasource.');
470 }
471
472 const dataSource = this.dataSource ?? [];
473 const maxCols = this.tableMeta.maxCols;
474 const numberOfSourceCols = this.hot!.countSourceCols();
475 const numberOfVisualCols = this.hot!.countCols();
476 const numberOfSourceRows = this.hot!.countSourceRows();
477 const visualColumnIndex = (typeof index === 'number' && index <= numberOfSourceCols) ? index : numberOfVisualCols;
478
479 if (this.hot!.runHooks('beforeCreateCol', visualColumnIndex, amount, source) === false) {
480 return { delta: 0 };
481 }
482
483 const physicalColumnIndex = (visualColumnIndex < numberOfVisualCols) ?
484 this.hot!.toPhysicalColumn(visualColumnIndex) : numberOfSourceCols;
485 const firstNewPhysicalColumnIndex = (mode === 'end') ?
486 Math.min(physicalColumnIndex + 1, numberOfSourceCols) : physicalColumnIndex;
487
488 let numberOfCreatedCols = 0;
489
490 for (
491 let col = firstNewPhysicalColumnIndex;
492 numberOfCreatedCols < amount && numberOfVisualCols + numberOfCreatedCols < (maxCols ?? Infinity);
493 col++, numberOfCreatedCols++
494 ) {
495 this.#insertColumnIntoDataSource(
496 dataSource, col, visualColumnIndex, numberOfVisualCols + numberOfCreatedCols, numberOfSourceRows
497 );
498 }
499
500 if (numberOfCreatedCols > 0) {
501 if ((index === undefined || index === null)) {
502 // Creates the meta columns at the end of the columns collection without shifting the cells
503 // that were defined out of the range of the dataset.
504 this.metaManager!.createColumn(null, numberOfCreatedCols);
505
506 } else if (source !== 'auto') {
507 this.metaManager!.createColumn(firstNewPhysicalColumnIndex, amount);
508 }
509 }
510
511 this.hot!.columnIndexMapper.insertIndexes(visualColumnIndex, numberOfCreatedCols, mode);
512
513 this.hot!.runHooks(
514 'afterCreateCol',
515 this.hot!.toVisualColumn(firstNewPhysicalColumnIndex),
516 numberOfCreatedCols,
517 source
518 );
519 this.refreshDuckSchema();
520

Callers 3

alterFunction · 0.45
adjustRowsAndColsFunction · 0.45
applyChangesFunction · 0.45

Calls 12

refreshDuckSchemaMethod · 0.95
throwWithCauseFunction · 0.90
countSourceColsMethod · 0.80
countSourceRowsMethod · 0.80
toPhysicalColumnMethod · 0.80
insertIndexesMethod · 0.80
toVisualColumnMethod · 0.80
countColsMethod · 0.65
runHooksMethod · 0.65
createColumnMethod · 0.65

Tested by

no test coverage detected