Active Table Web Component
Active Table is a framework-agnostic web component, currently at stable version 1.1.8, designed to provide a highly customizable and editable table experience. Built with Lit Element, it offers a robust feature set including adding, removing, moving, and editing rows/columns, text validation, sorting, pagination, filtering, and various column types (Text, Number, Currency, Select, Label, Date, Checkbox). It also provides an API for building custom column types, and capabilities for importing, exporting, pasting, and drag-and-dropping CSV, XLS, XLSX, ODS, and TXT files. The component supports programmatic cell updates, reactive dimensions, and handles overflow. Releases are frequent, addressing bugs and introducing new features like column/row dragging, indicating active development. Its key differentiator is being a standalone web component, allowing seamless integration into any JavaScript framework or vanilla HTML, and shipping with comprehensive TypeScript types.
Common errors
-
TypeError: Cannot read properties of null (reading 'offsetWidth')
cause This error occurs when the component tries to access the `offsetWidth` property of a DOM element that is `null` or hasn't been rendered yet, often during window resizing.fixUpgrade to `active-table` version 1.1.6 or newer. This issue was specifically addressed in that release. -
SyntaxError: Cannot use import statement outside a module
cause This issue, commonly appearing in React environments before v1.1.5, arises when a CommonJS-expecting bundler or runtime attempts to load `active-table` (which uses ES modules) without proper configuration.fixUpgrade to `active-table` version 1.1.5 or later, as this release fixed bundling issues using Vite. Ensure your project's build setup correctly handles ES modules. For React, consider `active-table-react`. -
Property 'content' does not exist on type 'ActiveTable'
cause Attempting to use the `content` property for data initialization or updates in TypeScript after it was renamed.fixReplace all instances of `content` with `data`. For example, change `tableElement.content = [...]` to `tableElement.data = [...]` or `tableElement.setAttribute('data', '...');`.
Warnings
- breaking The `content` property was renamed to `data` to avoid conflicts with existing HTML attributes and resolve TypeScript errors.
- gotcha When integrating with React, older versions (prior to 1.1.5) could encounter module resolution errors related to ESM (`"type": "module"`) bundling.
- gotcha Versions prior to 1.1.6 had a `TypeError: Cannot read properties of null (reading 'offsetWidth')` when resizing the webpage width, indicating a rendering issue with responsive layouts.
- gotcha Data pasted into empty cells in versions prior to 1.1.4 might not have been registered correctly, leading to data not being returned by `getData` or exported.
- gotcha Version 1.0.16 migrated to Lit Element version 3. While often seamless, this could introduce compatibility issues for projects with deep or custom Lit Element integrations.
Install
-
npm install active-table -
yarn add active-table -
pnpm add active-table
Imports
- active-table
import 'active-table';
- ActiveTable
import type { ActiveTable } from 'active-table'; - ActiveTableComponent
const ActiveTableComponent = require('active-table').ActiveTableComponent;import { ActiveTableComponent } from 'active-table';
Quickstart
import 'active-table'; // Registers the <active-table> custom element
import type { ActiveTable } from 'active-table'; // For TypeScript type safety
document.addEventListener('DOMContentLoaded', () => {
const tableElement = document.createElement('active-table') as ActiveTable;
tableElement.setAttribute('data', JSON.stringify([
['Product', 'Price', 'Stock'],
['Laptop', 1200, 50],
['Mouse', 25, 200],
['Keyboard', 75, 100],
]));
// You can also set properties directly:
tableElement.pagination = true;
tableElement.enterKeyMoveDown = true;
document.body.appendChild(tableElement);
// Example of programmatic interaction after the table is rendered
setTimeout(() => {
const activeTableInstance = document.querySelector('active-table') as ActiveTable | null;
if (activeTableInstance) {
console.log('Current table data:', activeTableInstance.getData());
// Programmatically add a row
activeTableInstance.addRow(['Monitor', 300, 75]);
console.log('Data after adding row:', activeTableInstance.getData());
}
}, 1000);
});