MCPcopy
hub / github.com/handsontable/handsontable / replaceData

Function replaceData

handsontable/src/dataMap/replaceData.ts:85–174  ·  view source on GitHub ↗

* Loads new data to Handsontable. * * @private * @param {Array} data Array of arrays or array of objects containing data. * @param {Function} setDataMapFunction Function that updates the datamap instance. * @param {Function} callbackFunction Function that takes care of updating Handsontable to

(
  data: unknown[],
  setDataMapFunction: (dataMap: DataMap) => void,
  callbackFunction: (dataMap: DataMap) => void,
  config: ReplaceDataConfig
)

Source from the content-addressed store, hash-verified

83 * @fires Hooks#afterChange
84 */
85function replaceData(
86 data: unknown[],
87 setDataMapFunction: (dataMap: DataMap) => void,
88 callbackFunction: (dataMap: DataMap) => void,
89 config: ReplaceDataConfig
90) {
91 const {
92 hotInstance,
93 dataMap,
94 dataSource,
95 internalSource,
96 source,
97 metaManager,
98 firstRun
99 } = config;
100 const capitalizedInternalSource = toUpperCaseFirst(internalSource);
101 const tableMeta = hotInstance.getSettings();
102
103 if (Array.isArray(tableMeta.dataSchema)) {
104 hotInstance.dataType = 'array';
105 } else if (isFunction(tableMeta.dataSchema)) {
106 hotInstance.dataType = 'function';
107 } else {
108 hotInstance.dataType = 'object';
109 }
110
111 if (dataMap) {
112 dataMap.destroy();
113 }
114
115 data = hotInstance.runHooks(`before${capitalizedInternalSource}`, data, firstRun, source) as unknown[];
116
117 const newDataMap = new DataMap(hotInstance, data as (Record<string, unknown> | unknown[])[], metaManager);
118
119 // We need to apply the new dataMap immediately, because of some asynchronous logic in the
120 // `autoRowSize`/`autoColumnSize` plugins.
121 setDataMapFunction(newDataMap);
122
123 if (typeof data === 'object' && data !== null) {
124 if (!((data as unknown[]).push && (data as unknown[]).splice)) { // check if data is array. Must use duck-type check so Backbone Collections also pass it
125 // when data is not an array, attempt to make a single-row array of it
126 // eslint-disable-next-line no-param-reassign
127 data = [data];
128 }
129
130 } else if (data === null) {
131 // eslint-disable-next-line no-param-reassign
132 data = buildNullData(hotInstance, newDataMap, tableMeta);
133
134 } else {
135 throwWithCause(`${internalSource} only accepts array of objects or array of arrays (${typeof data} given)`);
136 }
137
138 if (Array.isArray((data as unknown[])[0])) {
139 hotInstance.dataType = 'array';
140 }
141
142 tableMeta.data = data as unknown[][] | object[];

Callers 1

CoreFunction · 0.90

Calls 15

colToPropMethod · 0.95
propToColMethod · 0.95
toUpperCaseFirstFunction · 0.90
isFunctionFunction · 0.90
throwWithCauseFunction · 0.90
runSourceDataValidatorsFunction · 0.90
setAttributeFunction · 0.90
A11Y_ROWCOUNTFunction · 0.90
A11Y_COLCOUNTFunction · 0.90
buildNullDataFunction · 0.85
getSettingsMethod · 0.65
destroyMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…