UNPKG

vanillajs-excelike-table

Version:

A user-friendly pure JavaScript table library with Excel-like features, preset configurations, and intuitive column helpers. Vanilla JS implementation - no frameworks required!

388 lines (306 loc) โ€ข 10.4 kB
# VanillaJS ExceLikeTable > A user-friendly **pure JavaScript** table library with Excel-like features, preset configurations, and intuitive column helpers. **No frameworks required!** ![npm version](https://img.shields.io/npm/v/vanillajs-excelike-table.svg) ![License](https://img.shields.io/npm/l/vanillajs-excelike-table.svg) ![Size](https://img.shields.io/bundlephobia/minzip/vanillajs-excelike-table.svg) ## โœจ Features - ๐ŸŽฏ **User-friendly Design**: Preset configurations for quick setup - ๐Ÿ”ง **Intuitive Column Helpers**: Easy column definition with `ColumnHelpers` - โšก **Lightweight & Fast**: Pure vanilla JavaScript, no dependencies - ๐ŸŽจ **Excel-like Features**: Filtering, sorting, column pinning, resizing - ๐Ÿ’พ **Settings Persistence**: Auto-save user preferences to LocalStorage - ๐ŸŽš๏ธ **Feature Selection**: Enable only what you need - ๐Ÿ“ฑ **Responsive Design**: Works on desktop and mobile - ๐Ÿ”„ **TypeScript Support**: Full type definitions included ## ๐Ÿš€ Quick Start ### Installation ```bash npm install vanillajs-excelike-table ``` ### Basic Usage ```html <!-- Include CSS --> <link rel="stylesheet" href="node_modules/vanillajs-excelike-table/dist/excelike-table.css"> <!-- Include JavaScript --> <script src="node_modules/vanillajs-excelike-table/dist/excelike-table.js"></script> ``` ### Simple Example ```javascript // Sample data const data = [ { id: 1, name: 'John Doe', age: 30, city: 'New York' }, { id: 2, name: 'Jane Smith', age: 25, city: 'Los Angeles' }, { id: 3, name: 'Bob Johnson', age: 35, city: 'Chicago' } ]; // Easy column definition with ColumnHelpers const columns = [ ColumnHelpers.text('name', 'Name'), ColumnHelpers.number('age', 'Age'), ColumnHelpers.text('city', 'City') ]; // Create table with standard preset const table = new ExceLikeTable('#table-container', { preset: 'standard', // Enable sorting, filtering, pagination data: data, columns: columns }); ``` ## ๐Ÿ“‹ Presets Choose from predefined configurations to get started quickly: | Preset | Features | Best For | |--------|----------|----------| | `simple` | Sorting only | Basic data display | | `standard` | Sorting + Filtering + Pagination | Most common use cases | | `advanced` | Standard + Column Settings + Persistence | Power users | | `excel` | All features enabled | Full Excel-like experience | ```javascript // Use preset const table = new ExceLikeTable('#container', { preset: 'excel', data: data, columns: columns }); // Or customize features const table = new ExceLikeTable('#container', { features: ['sorting', 'filtering'], // Only enable what you need data: data, columns: columns }); ``` ## ๐Ÿ”ง Column Helpers Create columns easily with built-in helpers: ### Text Column ```javascript ColumnHelpers.text('name', 'Name', { width: 150 }) ``` ### Number Column ```javascript ColumnHelpers.number('salary', 'Salary', { currency: '$', width: 120 }) ``` ### Date Column ```javascript ColumnHelpers.date('joinDate', 'Join Date', { width: 130 }) ``` ### Status Column ```javascript ColumnHelpers.status('status', 'Status', { 'Active': '#52c41a', 'Inactive': '#d9d9d9', 'Pending': '#faad14' }) ``` ### Action Column ```javascript ColumnHelpers.actions('Actions', [ { key: 'edit', label: 'Edit' }, { key: 'delete', label: 'Delete' } ]) ``` ## ๐Ÿ“‹ API Reference ### Constructor ```javascript new ExceLikeTable(container, options) ``` #### Parameters - `container` (string | HTMLElement): CSS selector or DOM element where the table will be rendered - `options` (Object): Configuration options #### Options | Property | Type | Default | Description | |----------|------|---------|-------------| | `data` | Array | `[]` | Array of data objects | | `columns` | Array | `[]` | Column definitions | | `rowKey` | string | `'id'` | Unique identifier for each row | | `pagination` | Object | `{ pageSize: 10, showSizeChanger: true }` | Pagination configuration | | `bordered` | boolean | `true` | Show table borders | | `size` | string | `'middle'` | Table size: 'small', 'middle', 'large' | | `loading` | boolean | `false` | Show loading overlay | ### Column Definition ```javascript { key: 'columnKey', // Unique identifier title: 'Column Title', // Display name dataIndex: 'dataProperty', // Property name in data object width: 150, // Column width in pixels sortable: true, // Enable sorting filterable: true, // Enable filtering filterType: 'text', // Filter type: 'text', 'date-hierarchy', 'range' render: (value, record) => { // Custom renderer return `<strong>${value}</strong>`; }, onFilter: (value, record) => { // Custom filter function return record.property === value; }, sorter: (a, b) => { // Custom sorter function return a.property - b.property; } } ``` ### Methods #### Data Management ```javascript // Set new data table.setData(newData); // Update existing data table.updateData(newData); // Get current filters const filters = table.getFilters(); // Set filters programmatically table.setFilters({ columnKey: ['value1', 'value2'] }); // Clear all filters table.clearFilters(); ``` #### Lifecycle ```javascript // Destroy table and clean up table.destroy(); ``` ## ๐ŸŽจ Styling The library uses CSS classes that can be customized: ```css /* Main container */ .excelike-table-wrapper { } /* Table element */ .table { } /* Header cells */ .table-header { } /* Body cells */ .table-cell { } /* Pagination */ .enhanced-table-pagination { } /* Filter dropdowns */ .filter-dropdown { } /* Column settings modal */ .column-settings-modal { } ``` ## ๐Ÿ“ฑ Examples ### Basic Table ```javascript const basicTable = new ExceLikeTable('#container', { data: employees, columns: [ { key: 'name', title: 'Name', dataIndex: 'name', sortable: true, filterable: true }, { key: 'department', title: 'Department', dataIndex: 'department', filterable: true } ] }); ``` ### Advanced Features ```javascript const advancedTable = new ExceLikeTable('#container', { data: employees, columns: [ { key: 'salary', title: 'Salary', dataIndex: 'salary', sortable: true, filterable: true, render: (value) => `$${value.toLocaleString()}` }, { key: 'joinDate', title: 'Join Date', dataIndex: 'joinDate', filterType: 'date-hierarchy', sortable: true }, { key: 'performance', title: 'Performance', dataIndex: 'performance', render: (value) => { return `<div class="progress-bar"> <div class="progress-fill" style="width: ${value}%"></div> <span>${value}%</span> </div>`; } } ], pagination: { pageSize: 20, showSizeChanger: true, showTotal: (total, range) => `${range[0]}-${range[1]} of ${total} items` } }); ``` ### Column Pinning Use the column settings menu (โš™๏ธ button) to pin columns to the left side of the table. ### Settings Persistence (LocalStorage) When `persistSettings: true` is enabled, the following user preferences are automatically saved to LocalStorage and restored on page reload: #### ๐Ÿ’พ **Persisted Settings** - **ๅˆ—ๅน…** (Column Widths): User-adjusted column widths - **่กจ็คบๅˆ—่จญๅฎš** (Column Visibility): Hidden/visible column states - **ๅ›บๅฎšๅˆ—่จญๅฎš** (Column Pinning): Left-pinned column configurations - **ๆ–‡ๅญ—ใ‚ตใ‚คใ‚บ** (Font Size): User-selected text size preferences - **ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ** (Cell Padding): Cell spacing configurations - **ใƒšใƒผใ‚ธใ‚ตใ‚คใ‚บ** (Page Size): Items per page setting #### ๐Ÿ”„ **Non-Persisted Settings** - **ใ‚ฝใƒผใƒˆใƒปใƒ•ใ‚ฃใƒซใ‚ฟใฎ่จญๅฎš็Šถๆณ** (Sort & Filter State): Reset on page reload (standard behavior) #### ๐Ÿ”ง **Configuration** ```javascript const table = new ExceLikeTable('#my-table', { data: myData, columns: myColumns, persistSettings: true, // Enable LocalStorage persistence tableId: 'unique-table' // Required: Unique identifier for storage key }); ``` **Note**: The `tableId` is essential for creating unique storage keys when multiple tables exist on the same domain. ### Filter Types 1. **Text Filter**: Standard checkbox list with search 2. **Date Hierarchy**: Expandable year/month filtering 3. **Range Filter**: Numeric range with slider controls ## ๐Ÿ”ง Development ### ๐Ÿšจ **Important: Development Workflow** **ALWAYS follow this workflow when making changes:** 1. **Edit source files**: Modify `src/ExceLikeTable.js` (never edit `dist/` files directly!) 2. **Build the library**: Run `npm run build` to generate `dist/` files 3. **Test changes**: The HTML files load from `dist/`, so changes appear only after building ```bash # Example workflow: vim src/ExceLikeTable.js # 1. Edit source npm run build # 2. Build dist files # 3. Your changes are now live! ``` โš ๏ธ **Common Mistake:** - โŒ Wrong: Editing dist/excelike-table.js directly - โœ… Correct: Edit src/ExceLikeTable.js โ†’ run npm run build ### Build from Source ```bash # Install dependencies npm install # Build the library npm run build # Start development server npm run dev ``` ### File Structure ``` vanilla-excelike-table/ โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ ExceLikeTable.js # ๐Ÿ“ EDIT THIS (source files) โ”‚ โ””โ”€โ”€ ExceLikeTable.css # ๐Ÿ“ EDIT THIS (source styles) โ”œโ”€โ”€ dist/ # ๐Ÿšซ DON'T EDIT (generated files) โ”‚ โ”œโ”€โ”€ excelike-table.js # โ† Generated from src/ โ”‚ โ””โ”€โ”€ excelike-table.css # โ† Generated from src/ โ”œโ”€โ”€ examples/ # Example HTML files โ”œโ”€โ”€ scripts/ # Build scripts โ””โ”€โ”€ README.md ``` ### For AI/Human Developers - **Source of Truth**: `src/` directory contains the editable source code - **Production Files**: `dist/` directory contains built files (auto-generated) - **HTML imports**: Always reference `dist/` files in HTML - **Development rule**: src โ†’ build โ†’ dist (one-way flow) ## ๐Ÿ“„ License MIT License - feel free to use this library in your projects! ## ๐Ÿค Contributing Contributions are welcome! Please feel free to submit pull requests or open issues. ## ๐Ÿ“ž Support For questions or issues, please visit our GitHub repository or contact us directly. --- **Made with โค๏ธ for the JavaScript community**