📊 sb-datagrid

A lightweight, customizable, and feature-rich JavaScript data grid component with resizable columns, sorting, drag-and-drop column reordering, and more.

🚀 Quick Start: <script src="https://snadboy.github.io/sb-datagrid/js/datagrid.js"></script>

📚 CDN: <link rel="stylesheet" href="https://snadboy.github.io/sb-datagrid/css/datagrid.css">

Features

Basic Example

A simple data grid with employee information. Try:

Code:

const basicGrid = new DataGrid('#basic-grid', {
    columns: [
        { field: 'id', header: 'ID', width: '80px' },
        { field: 'name', header: 'Name' },
        { field: 'position', header: 'Position' },
        { field: 'department', header: 'Department' },
        { field: 'salary', header: 'Salary' }
    ],
    data: employeeData
});

Advanced Example with All Features

This example demonstrates sorting, selection, custom renderers, and event callbacks.

Selection:

Selection: No rows selected

Custom Cell Renderers

Use custom renderers to display rich content in cells.

API Documentation

Constructor

new DataGrid(container, options)

Options

{
    columns: [],           // Array of column definitions
    data: [],             // Array of data objects
    resizable: true,      // Enable column resizing
    sortable: true,       // Enable column sorting
    selectable: false,    // Enable row selection
    reorderable: true,    // Enable column reordering
    theme: '',           // Theme class (dark, compact, bordered)
    minColumnWidth: 50,   // Minimum column width in pixels
    maxColumnWidth: null, // Maximum column width in pixels
    onSort: function(column, direction) {},      // Sort callback
    onSelect: function(selectedRows) {},         // Selection callback
    onResize: function(columnIndex, newWidth) {}, // Resize callback
    onReorder: function(fromIndex, toIndex, columns) {}, // Reorder callback
    onCellClick: function(row, column, cell) {},  // Cell click callback
    onCellDoubleClick: function(row, column, cell) {} // Cell double-click callback
}

Column Definition

{
    field: 'fieldName',        // Data field name
    header: 'Column Header',   // Display header text
    width: '150px',           // Initial width (px or fr)
    sortable: true,           // Enable sorting for this column
    resizable: true,          // Enable resizing for this column
    renderer: function(value, row, column) {}, // Custom cell renderer
    sorter: function(a, b, direction) {}      // Custom sort function
}

Methods

grid.setData(data)           // Update grid data
grid.addRow(row)            // Add a new row
grid.removeRow(index)        // Remove row by index
grid.updateRow(index, row)   // Update row data
grid.getSelectedRows()       // Get selected row data
grid.clearSelection()        // Clear all selections
grid.autoSizeColumn(index)   // Auto-size a specific column
grid.setTheme(theme)        // Change grid theme dynamically
grid.showSelectedOnly()     // Filter to show only selected rows
grid.showAll()              // Show all rows (clear filter)
grid.isFiltered()           // Check if grid is filtered
grid.refresh()              // Refresh the grid
grid.destroy()              // Destroy the grid instance