Public
Edited
Jan 13, 2023
Insert cell
Insert cell
table = (data = [], options = {}) => {
if (data.length === 0) return html``;
const columnHeaders = [
...data.reduce((result, row) => {
Object.keys(row).forEach((key) => {
result.add(key);
});
return result;
}, new Set())
];
let columns = columnHeaders.map((columnHeader) => ({
data: columnHeader
}));
if (options.sort) {
const index = columnHeaders.indexOf(options.sort) || 0;
options.columnSorting = {
initialConfig: { column: index, sortOrder: options.sortOrder || "asc" }
};
delete options.sort;
}
if (options.columns) {
if (typeof options.columns === "function") {
columns = options.columns(columns);
} else {
columns = columns.map((column) => {
const updatedColumn = options.columns.find(
(_column) => _column.data === column.data
);
if (updatedColumn) {
return { ...updatedColumn, ...column };
}
return column;
});
}
delete options.columns;
}
return handsontable({
data,
columns,
colHeaders: columnHeaders,
columnSorting: true,
// manualColumnMove: true,
// manualColumnResize: true,
// dropdownMenu: true,
// filters: true,
...options
});
}
Insert cell
Insert cell
table([{mol:42}])
Insert cell
handsontable = (options = {}, update) => {
const height = Math.min(options.data.length * 22 + 42, 600);
const element = html`<div style="padding-bottom: 15px; ${
options.style || ""
};" />`;

const hot = Handsontable(element, {
width: "100%",
height,
licenseKey: "non-commercial-and-evaluation",
data: options.data,
outsideClickDeselects: false,
...options
});
const updateElement = () => {
element.value = hot.getSourceData() || [];
element.dispatchEvent(new CustomEvent("input"));
};
// const hooks = Object.entries(options).reduce((result, [key, value]) => {
// if (typeof value === 'function') {
// return { ...result, [key]: (...args) => value(...args, hot) };
// }
// }, {});
hot.updateSettings({
// ...hooks,
afterRender: () => {
hot.view.activeWt.wtOverlays?.updateMainScrollableElements();
},
// beforeChange: (changes, source) => {
// updateElement(changes, source);
// },
afterChange: (changes, source) => {
updateElement(changes, source);
}
// afterColumnMove: (column, target) => {
// if (options.afterColumnMove) {
// options.afterColumnMove(column, target);
// }
// updateElement();
// },
// afterColumnResize: (currentColumn, newSize, isDoubleClick) => {
// if (options.afterColumnResize) {
// options.afterColumnResize(currentColumn, newSize, isDoubleClick);
// }
// updateElement();
// },
// afterColumnSort: (currentSortConfig, destinationSortConfig) => {
// if (options.afterColumnSort) {
// options.afterColumnSort(currentSortConfig, destinationSortConfig);
// }
// updateElement();
// },
// afterFilter: conditionsStack => {
// if (options.afterFilter) {
// options.afterFilter(conditionsStack);
// }
// updateElement();
// }
});
element.hot = hot;
element.value = hot.getSourceData() || [];
invalidation.then(() => element.hot.destroy());
setTimeout(() => {
// element.hot.render();
element.hot.refreshDimensions();
}, 30);
return element;
}
Insert cell
Handsontable = {
const styles = html`<link href="https://unpkg.com/handsontable/dist/handsontable.full.min.css" rel="stylesheet" media="screen">`;
document.head.appendChild(styles);
return require('handsontable');
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more