📈

<secure-table>

A data table component with sorting, filtering, pagination, and XSS-safe cell rendering. All content is sanitized before display.

Overview

The <secure-table> component renders tabular data with built-in XSS protection. All cell values are sanitized, preventing script injection even when displaying user-generated content.

Safe Data Display
Content like <script>alert('xss')</script> is automatically escaped and displayed as plain text.

Data Binding

Setting Data via JavaScript

Loading Data JavaScript
const table = document.querySelector('secure-table');

// Define columns
table.setColumns([
  { key: 'id', label: 'ID', sortable: true },
  { key: 'name', label: 'Name', sortable: true },
  { key: 'email', label: 'Email', sortable: true },
  { key: 'role', label: 'Role', filterable: true },
  { key: 'created', label: 'Created', type: 'date' }
]);

// Set data
table.setData([
  { id: 1, name: 'John Doe', email: 'john@example.com', role: 'Admin', created: '2024-01-15' },
  { id: 2, name: 'Jane Smith', email: 'jane@example.com', role: 'User', created: '2024-01-16' },
  // ...
]);

Loading from API

Fetch and Display JavaScript
const table = document.querySelector('secure-table');

async function loadUsers() {
  const response = await fetch('/api/users');
  const users = await response.json();

  table.setColumns([
    { key: 'id', label: 'ID' },
    { key: 'name', label: 'Name', sortable: true },
    { key: 'email', label: 'Email' },
    { key: 'status', label: 'Status', filterable: true }
  ]);

  table.setData(users);
}

loadUsers();

Features

Sorting

Enable sorting on columns by setting sortable: true in the column definition. Click column headers to sort ascending/descending.

Filtering

Enable filtering with filterable: true. The table provides a search input for text filtering and dropdown filters for enumerated values.

Pagination

Pagination Configuration HTML
<secure-table
  page-size="25"
  show-pagination="true"
  security-tier="authenticated"
></secure-table>

Action Buttons

Add action buttons to table rows using data-action attributes. The table emits a table-action event with all data-* attributes from the clicked element.

Row Actions JavaScript
const table = document.querySelector('secure-table');

// Listen for action button clicks
table.addEventListener('table-action', (e) => {
  console.log('Action:', e.detail.action);
  console.log('Data attributes:', e.detail);
  // e.g. { action: 'edit', userId: '123' }
});

API Reference

Attributes

AttributeTypeDefaultDescription
security-tierstring"critical"Security level
page-sizenumber10Rows per page
show-paginationbooleantrueShow pagination controls
show-searchbooleantrueShow search input
stripedbooleantrueAlternate row colors

Column Definition

Column Object TypeScript
interface TableColumn {
  key: string;        // Data property key
  label: string;      // Display header
  sortable?: boolean; // Enable sorting
  filterable?: boolean; // Enable filtering
  type?: 'text' | 'number' | 'date' | 'boolean';
  width?: string;     // CSS width
  align?: 'left' | 'center' | 'right';
  render?: (value: any, row: any) => string; // Custom renderer
}

Methods

setColumns(columns: TableColumn[]): void
Define table columns
setData(data: object[]): void
Set table data
getAuditLog(): AuditLogEntry[]
Get security audit log entries

Events

EventDetailDescription
table-action action, ...dataAttributes Fired when an action button inside the table is clicked
secure-audit event, data Security audit event (sort, filter, mount, action)

Examples

User Management Table

Complete Example HTML
<secure-table
  id="users-table"
  page-size="25"
  security-tier="authenticated"
></secure-table>

<script>
  const table = document.getElementById('users-table');

  table.setColumns([
    { key: 'id', label: 'ID', width: '60px' },
    { key: 'name', label: 'Name', sortable: true },
    { key: 'email', label: 'Email', sortable: true },
    { key: 'role', label: 'Role', filterable: true },
    { key: 'status', label: 'Status', filterable: true },
    { key: 'lastLogin', label: 'Last Login', type: 'date', sortable: true }
  ]);

  // Fetch and display users
  fetch('/api/users')
    .then(r => r.json())
    .then(users => table.setData(users));

  // Handle action buttons (e.g. edit, delete)
  table.addEventListener('table-action', (e) => {
    console.log('Action:', e.detail.action, 'User:', e.detail.userId);
  });
</script>

Custom Cell Rendering

Status Badge Renderer JavaScript
table.setColumns([
  { key: 'name', label: 'Name' },
  {
    key: 'status',
    label: 'Status',
    render: (value) => {
      const colors = { active: 'green', pending: 'orange', inactive: 'red' };
      // Note: return value is sanitized by the component
      return '<span class="badge badge-' + colors[value] + '">' + value + '</span>';
    }
  }
]);