Debian Codegraph @

www/src/content/patterns/03-records/01-bulk-actions.md

raw symbols SCIP
1 ---
2 includeMockServer: true
3 title: "Bulk Actions"
4 description: Perform actions on multiple records
5 icon: "icon-[mdi--checkbox-multiple-marked]"
6 ---
7
8 <script>
9 var _dataStore = (() => {
10 const data = {
11 "[email protected]": { name: "Joe Smith", status: "Active" },
12 "[email protected]": { name: "Angie MacDowell", status: "Active" },
13 "[email protected]": { name: "Fuqua Tarkenton", status: "Active" },
14 "[email protected]": { name: "Kim Yee", status: "Inactive" },
15 };
16
17 return {
18 all() { return data; },
19 activate(emails) {
20 let n = 0;
21 for (const e of emails) { if (data[e] && data[e].status !== "Active") { data[e].status = "Active"; n++; } }
22 return n;
23 },
24 deactivate(emails) {
25 let n = 0;
26 for (const e of emails) { if (data[e] && data[e].status !== "Inactive") { data[e].status = "Inactive"; n++; } }
27 return n;
28 },
29 remove(emails) {
30 let n = 0;
31 for (const e of emails) { if (data[e]) { delete data[e]; n++; } }
32 return n;
33 },
34 };
35 })();
36
37 function renderList(message) {
38 const contacts = _dataStore.all();
39 return `<form hx-target:inherited="this" hx-swap="outerHTML">
40 <div id="action-bar" class="hidden items-center gap-2 px-3 py-2.5 mb-3 rounded-md bg-neutral-50 dark:bg-neutral-900 border border-neutral-150 dark:border-neutral-800">
41 <span class="text-xs font-medium text-neutral-500 dark:text-neutral-400 mr-1">With selected:</span>
42 <button hx-post="/activate" class="px-2.5 py-1 text-xs font-medium rounded cursor-pointer border border-neutral-200 dark:border-neutral-700 text-neutral-600 dark:text-neutral-300 hover:bg-neutral-100 dark:hover:bg-neutral-800 active:scale-[0.98] transition">Activate</button>
43 <button hx-post="/deactivate" class="px-2.5 py-1 text-xs font-medium rounded cursor-pointer border border-neutral-200 dark:border-neutral-700 text-neutral-600 dark:text-neutral-300 hover:bg-neutral-100 dark:hover:bg-neutral-800 active:scale-[0.98] transition">Deactivate</button>
44 <button hx-post="/delete" class="px-2.5 py-1 text-xs font-medium rounded cursor-pointer border border-red-200 dark:border-red-900 text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-950 active:scale-[0.98] transition">Delete</button>
45 </div>
46 <table class="w-full border-collapse">
47 <thead>
48 <tr>
49 <th class="w-10 px-3 py-2 text-center border-b-2 border-neutral-100 dark:border-neutral-850">
50 <input type="checkbox" class="size-4 cursor-pointer accent-neutral-800 dark:accent-neutral-300"
51 _="on change set checked to my.checked then for cb in <input[name='selected']/> in closest <form/> set cb.checked to checked then send checkChange to closest <form/>">
52 </th>
53 <th class="text-left px-3 py-2 text-neutral-450 dark:text-neutral-400 font-semibold text-xs uppercase tracking-wide border-b-2 border-neutral-100 dark:border-neutral-850">Name</th>
54 <th class="text-left px-3 py-2 text-neutral-450 dark:text-neutral-400 font-semibold text-xs uppercase tracking-wide border-b-2 border-neutral-100 dark:border-neutral-850">Email</th>
55 <th class="text-left px-3 py-2 text-neutral-450 dark:text-neutral-400 font-semibold text-xs uppercase tracking-wide border-b-2 border-neutral-100 dark:border-neutral-850">Status</th>
56 </tr>
57 </thead>
58 <tbody class="[&>tr:last-child>td]:border-b-0"
59 _="on change from <input[name='selected']/> or checkChange
60 if (<input[name='selected']:checked/> in closest <form/>).length > 0
61 remove .hidden from #action-bar then add .flex to #action-bar
62 else
63 add .hidden to #action-bar then remove .flex from #action-bar
64 end">
65 ${Object.entries(contacts).map(([email, c], i) => `
66 <tr class="starting:opacity-0 transition-opacity duration-300 ease-out cursor-pointer hover:bg-neutral-50 dark:hover:bg-neutral-900 has-[input:checked]:bg-neutral-50 dark:has-[input:checked]:bg-neutral-900" style="transition-delay:${i * 60}ms"
67 _="on click if event.target.tagName !== 'INPUT' then toggle @checked on the <input[name='selected']/> in me then send change to the <input[name='selected']/> in me">
68 <td class="px-3 py-2.5 border-b border-neutral-100 dark:border-neutral-850 text-sm text-center">
69 <input type="checkbox" name="selected" value="${email}" class="size-4 cursor-pointer accent-neutral-800 dark:accent-neutral-300">
70 </td>
71 <td class="px-3 py-2.5 border-b border-neutral-100 dark:border-neutral-850 text-sm text-neutral-600 dark:text-neutral-300">${c.name}</td>
72 <td class="px-3 py-2.5 border-b border-neutral-100 dark:border-neutral-850 text-sm text-neutral-600 dark:text-neutral-300">${email}</td>
73 <td class="px-3 py-2.5 border-b border-neutral-100 dark:border-neutral-850 text-sm text-neutral-600 dark:text-neutral-300">
74 <span class="text-xs font-medium px-2 py-0.5 rounded-full ${c.status === "Active" ? "bg-green-50 dark:bg-green-950 text-green-700 dark:text-green-400" : "bg-neutral-100 dark:bg-neutral-800 text-neutral-500 dark:text-neutral-400"}">${c.status}</span>
75 </td>
76 </tr>`).join("")}
77 </tbody>
78 </table>
79 ${message ? `<output class="block mt-3 px-3 py-2 rounded-md bg-neutral-100 dark:bg-neutral-800 text-sm text-neutral-700 dark:text-neutral-300">${message}</output>` : ""}
80 </form>`;
81 }
82
83 server.get("/demo", () => renderList());
84
85 server.post("/activate", (req) => {
86 const emails = [].concat(req.params.selected || []);
87 const n = _dataStore.activate(emails);
88 return renderList(`Activated ${n} user${n !== 1 ? "s" : ""}`);
89 });
90
91 server.post("/deactivate", (req) => {
92 const emails = [].concat(req.params.selected || []);
93 const n = _dataStore.deactivate(emails);
94 return renderList(`Deactivated ${n} user${n !== 1 ? "s" : ""}`);
95 });
96
97 server.post("/delete", (req) => {
98 const emails = [].concat(req.params.selected || []);
99 const n = _dataStore.remove(emails);
100 return renderList(`Deleted ${n} user${n !== 1 ? "s" : ""}`);
101 });
102
103 server.start("/demo");
104 </script>
105
106 <div id="demo-content" class="not-prose demo-container min-h-[340px]"></div>
107
108 ## How it works
109
110 Wrap a table in a `<form>`. Each row has a checkbox, and an action bar appears when any are checked. Clicking a row toggles its checkbox.
111
112 ```html
113 <form hx-target:inherited="this" hx-swap="outerHTML">
114
115 <div id="action-bar" class="hidden">
116 <span>With selected:</span>
117 <button hx-post="/activate">Activate</button>
118 <button hx-post="/deactivate">Deactivate</button>
119 <button hx-post="/delete">Delete</button>
120 </div>
121
122 <table>
123 <thead>
124 <tr>
125 <th><input type="checkbox" class="select-all"></th>
126 <th>Name</th>
127 <th>Email</th>
128 <th>Status</th>
129 </tr>
130 </thead>
131 <tbody>
132 <tr>
133 <td><input type="checkbox" name="selected" value="[email protected]"></td>
134 <td>Joe Smith</td>
135 <td>[email protected]</td>
136 <td>Active</td>
137 </tr>
138 ...
139 </tbody>
140 </table>
141 </form>
142 ```
143
144 - [`hx-target`](/reference/attributes/hx-target)=`"this"` and [`hx-swap`](/reference/attributes/hx-swap)=`"outerHTML"` on the form mean every action replaces the entire form with the server's response.
145 - Each action button uses [`hx-post`](/reference/attributes/hx-post) to a different endpoint. Only checked `name="selected"` values are submitted.
146 - Use [`hx-target:inherited`](/docs#attribute-inheritance) to avoid repeating target declarations on each action button.
147
148 ### Clickable rows
149
150 Make the whole row a click target so users don't have to aim for the small checkbox:
151
152 ```html
153 <tr _="on click
154 if event.target.tagName !== 'INPUT'
155 toggle @checked on the <input[name='selected']/> in me
156 then send change to the <input[name='selected']/> in me">
157 <td><input type="checkbox" name="selected" value="[email protected]"></td>
158 ...
159 </tr>
160 ```
161
162 The `if event.target.tagName !== 'INPUT'` guard prevents double-toggling when clicking directly on the checkbox. Highlighting the selected row is pure CSS:
163
164 ```css
165 tr:has(input:checked) {
166 background: var(--selected-bg);
167 }
168 ```
169
170 ### Conditional action bar
171
172 Show the action bar only when at least one checkbox is checked. With CSS `:has()`:
173
174 ```css
175 .action-bar { display: none; }
176 form:has(input[name="selected"]:checked) .action-bar { display: flex; }
177 ```
178
179 Or with hyperscript on the `<tbody>` (for browsers without `:has()` support):
180
181 ```html
182 <tbody _="on change from <input[name='selected']/>
183 if (<input[name='selected']:checked/> in closest <form/>).length > 0
184 show #action-bar
185 else
186 hide #action-bar
187 end">
188 ```
189
190 ### Select all
191
192 A checkbox in the header toggles all row checkboxes:
193
194 ```html
195 <input type="checkbox"
196 _="on change
197 set checked to my.checked
198 then for cb in <input[name='selected']/> in closest <form/>
199 set cb.checked to checked
200 then send checkChange to closest <form/>">
201 ```
202
203 ### Server response
204
205 The server processes the selected emails, performs the bulk action, and re-renders the full table. Selections are cleared and statuses reflect the update:
206
207 ```html
208 <!-- POST /activate with [email protected]&[email protected] -->
209 <form hx-target:inherited="this" hx-swap="outerHTML">
210 <!-- ...updated table rows... -->
211 <output>Activated 1 user</output>
212 </form>
213 ```