HTMX
HTMX brings the power of modern web applications to SØAD without complex JavaScript frameworks. This recipe shows you how to create dynamic, interactive web pages using HTMX's declarative approach to AJAX, CSS transitions, and WebSockets.
Dynamic Todo List with HTMX
| htmx_todo.html | |
|---|---|
| |
CREATE TABLE task (
id int NOT NULL AUTO_INCREMENT,
text varchar(500) NOT NULL,
completed boolean DEFAULT false,
created_at timestamp DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY idx_completed (completed),
KEY idx_created (created_at)
);
INSERT INTO task (text, completed) VALUES
('Learn HTMX basics', false),
('Build dynamic todo list', false),
('Implement partial page updates', true),
('Add smooth animations', false),
('Test user interactions', false);
HTMX Features Demonstrated
- Partial Updates: Only update specific parts of the page
- Form Handling: Submit forms without page refresh
- Loading Indicators: Visual feedback during requests
- Inline Editing: Edit tasks without leaving the page
- Confirmations: Built-in confirmation dialogs
Live Search with HTMX
| htmx_search.html | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
CREATE TABLE user (
id int NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
email varchar(150) NOT NULL,
department varchar(50),
phone varchar(20),
location varchar(100),
avatar varchar(255),
created_at timestamp DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY idx_name (name),
KEY idx_email (email),
KEY idx_department (department),
FULLTEXT KEY idx_search (name, email)
);
INSERT INTO user (name, email, department, phone, location, avatar) VALUES
('Alice Johnson', '[email protected]', 'Engineering', '+1-555-0101', 'San Francisco, CA', 'https://i.pravatar.cc/150?img=1'),
('Bob Smith', '[email protected]', 'Marketing', '+1-555-0102', 'New York, NY', 'https://i.pravatar.cc/150?img=2'),
('Carol Davis', '[email protected]', 'Design', '+1-555-0103', 'Los Angeles, CA', 'https://i.pravatar.cc/150?img=3'),
('David Wilson', '[email protected]', 'Engineering', '+1-555-0104', 'Seattle, WA', 'https://i.pravatar.cc/150?img=4'),
('Emma Brown', '[email protected]', 'Product', '+1-555-0105', 'Austin, TX', 'https://i.pravatar.cc/150?img=5'),
('Frank Miller', '[email protected]', 'Sales', '+1-555-0106', 'Chicago, IL', 'https://i.pravatar.cc/150?img=6'),
('Grace Lee', '[email protected]', 'Engineering', '+1-555-0107', 'Boston, MA', 'https://i.pravatar.cc/150?img=7'),
('Henry Taylor', '[email protected]', 'HR', '+1-555-0108', 'Denver, CO', 'https://i.pravatar.cc/150?img=8'),
('Iris Chen', '[email protected]', 'Design', '+1-555-0109', 'Portland, OR', 'https://i.pravatar.cc/150?img=9'),
('Jack Anderson', '[email protected]', 'Marketing', '+1-555-0110', 'Miami, FL', 'https://i.pravatar.cc/150?img=10');
Live Search Features
- Real-time Search: Results update as you type with debouncing
- Loading Indicators: Visual feedback during search requests
- Modal Details: Click users to view details in a modal
- Smooth Animations: CSS transitions for better UX
- Auto-focus: Search input is automatically focused
HTMX Key Benefits
No JavaScript Framework: HTMX works with HTML attributes
Partial Updates: Only update what changes, not the whole page
Progressive Enhancement: Works even if JavaScript is disabled
Server-Side Rendering: Keep your logic in familiar SØAD transactions
Easy Integration: Add dynamic behavior to existing HTML forms