Debian Codegraph @

www/src/content/patterns/06-advanced/02-update-other-content.mdx

raw
1 ---
2 title: "Multiple Swaps"
3 description: Update multiple elements on the page at once
4 icon: "icon-[mdi--sync]"
5 soon: true
6 ---
7 A question that often comes up when people are first working with htmx is:
8
9 > "I need to update other content on the screen. How do I do this?"
10
11 There are multiple ways to do so, and in this example we will walk you through some of them.
12
13 We'll use the following basic UI to discuss this concept: a simple table of contacts, and a form below it
14 to add new contacts to the table using [`hx-post`](/reference/attributes/hx-post).
15
16 ```html
17 <h2>Contacts</h2>
18 <table class="table">
19 <thead>
20 <tr>
21 <th>Name</th>
22 <th>Email</th>
23 <th></th>
24 </tr>
25 </thead>
26 <tbody id="contacts-table">
27 ...
28 </tbody>
29 </table>
30 <h2>Add A Contact</h2>
31 <form hx-post="/contacts">
32 <label>
33 Name
34 <input name="name" type="text">
35 </label>
36 <label>
37 Email
38 <input name="email" type="email">
39 </label>
40 </form>
41 ```
42
43 The problem here is that when you submit a new contact in the form, you want the contact table above to refresh and
44 include the contact that was just added by the form.
45
46 What solutions do we have?
47
48 ## Solution 1: Expand the Target
49
50 The easiest solution here is to "expand the target" of the form to enclose both the table *and* the form. For example,
51 you could wrap the whole thing in a `div` and then target that `div` in the form:
52
53 ```html
54 <div id="table-and-form">
55 <h2>Contacts</h2>
56 <table class="table">
57 <thead>
58 <tr>
59 <th>Name</th>
60 <th>Email</th>
61 <th></th>
62 </tr>
63 </thead>
64 <tbody id="contacts-table">
65 ...
66 </tbody>
67 </table>
68 <h2>Add A Contact</h2>
69 <form hx-post="/contacts" hx-target="#table-and-form">
70 <label>
71 Name
72 <input name="name" type="text">
73 </label>
74 <label>
75 Email
76 <input name="email" type="email">
77 </label>
78 </form>
79 </div>
80 ```
81
82 Note that we are targeting the enclosing div using the [`hx-target`](/reference/attributes/hx-target) attribute. You would need
83 to render both the table and the form in the response to the `POST` to `/contacts`.
84
85 This is a simple and reliable approach, although it might not feel particularly elegant.
86
87 ## Solution 2: Out of Band Responses
88
89 A more sophisticated approach to this problem would use [out of band swaps](/reference/attributes/hx-swap-oob) to swap in
90 updated content to the DOM.
91
92 Using this approach, the HTML doesn't need to change from the original setup at all:
93
94 ```html
95 <h2>Contacts</h2>
96 <table class="table">
97 <thead>
98 <tr>
99 <th>Name</th>
100 <th>Email</th>
101 <th></th>
102 </tr>
103 </thead>
104 <tbody id="contacts-table">
105 ...
106 </tbody>
107 </table>
108 <h2>Add A Contact</h2>
109 <form hx-post="/contacts">
110 <label>
111 Name
112 <input name="name" type="text">
113 </label>
114 <label>
115 Email
116 <input name="email" type="email">
117 </label>
118 </form>
119 ```
120
121 Instead of modifying something on the front end, in your response to the `POST` to `/contacts` you would include some additional content:
122
123 ```html
124 <tbody hx-swap-oob="beforeend:#contacts-table">
125 <tr>
126 <td>Joe Smith</td>
127 <td>[email protected]</td>
128 </tr>
129 </tbody>
130
131 <label>
132 Name
133 <input name="name" type="text">
134 </label>
135 <label>
136 Email
137 <input name="email" type="email">
138 </label>
139 ```
140
141 This content uses the [`hx-swap-oob`](/reference/attributes/hx-swap-oob) attribute to append itself to the `#contacts-table`, updating
142 the table after a contact is added successfully.
143
144 ## Solution 3: Triggering Events
145
146 An even more sophisticated approach would be to trigger a client side event when a successful contact is created and
147 then listen for that event on the table, causing the table to refresh.
148
149 ```html
150 <h2>Contacts</h2>
151 <table class="table">
152 <thead>
153 <tr>
154 <th>Name</th>
155 <th>Email</th>
156 <th></th>
157 </tr>
158 </thead>
159 <tbody id="contacts-table" hx-get="/contacts/table" hx-trigger="newContact from:body">
160 ...
161 </tbody>
162 </table>
163 <h2>Add A Contact</h2>
164 <form hx-post="/contacts">
165 <label>
166 Name
167 <input name="name" type="text">
168 </label>
169 <label>
170 Email
171 <input name="email" type="email">
172 </label>
173 </form>
174 ```
175
176 We have added a new end-point `/contacts/table` that re-renders the contacts table. Our trigger for this request
177 is a custom event, `newContact`. We listen for this event on the `body` because when it
178 is triggered by the response to the form, it will end up hitting the body due to event bubbling.
179
180 When a successful contact creation occurs during a POST to `/contacts`, the response includes
181 an [`HX-Trigger`](/reference/headers/HX-Trigger) response header that looks like this:
182
183 ```txt
184 HX-Trigger:newContact
185 ```
186
187 This will trigger the table to issue a `GET` to `/contacts/table` and this will render the newly added contact row
188 (in addition to the rest of the table.)
189
190 Very clean, event driven programming!
191
192 ## Solution 4: Using the Path Dependencies Extension
193
194 A final approach is to use REST-ful path dependencies to refresh the table. Intercooler.js, the predecessor
195 to htmx, had [path-based dependencies](https://intercoolerjs.org/docs.html#dependencies) integrated into the
196 library.
197
198 htmx dropped this as a core feature, but supports an extension, [path deps](https://github.com/bigskysoftware/htmx-extensions/blob/main/src/path-deps/README), that gives you
199 similar functionality.
200
201 Updating our example to use the extension would involve loading the extension javascript and then
202 annotating our HTML like so:
203
204 ```html
205 <h2>Contacts</h2>
206 <table class="table">
207 <thead>
208 <tr>
209 <th>Name</th>
210 <th>Email</th>
211 <th></th>
212 </tr>
213 </thead>
214 <tbody id="contacts-table" hx-get="/contacts/table" hx-ext="path-deps" hx-trigger="path-deps" path-deps="/contacts">
215 ...
216 </tbody>
217 </table>
218 <h2>Add A Contact</h2>
219 <form hx-post="/contacts">
220 <label>
221 Name
222 <input name="name" type="text">
223 </label>
224 <label>
225 Email
226 <input name="email" type="email">
227 </label>
228 </form>
229 ```
230
231 Now, when the form posts to the `/contacts` URL, the `path-deps` extension will detect that and trigger an `path-deps`
232 event on the contacts table, therefore triggering a request.
233
234 The advantage here is that you don't need to do anything fancy with response headers. The downside is that a request
235 will be issued on every `POST`, even if a contact was not successfully created.
236
237 ## Which should I use?
238
239 Generally I would recommend the first approach, expanding your target, especially if the elements that need to be
240 updated are reasonably close to one another in the DOM. It is simple and reliable.
241
242 After that, I would say it is a tossup between the custom event and an OOB swap approaches. I would lean towards the custom event
243 approach because I like event-oriented systems, but that's a personal preference. Which one you choose should be dictated by your
244 own software engineering tastes and which of the two matches up better with your server side technology of choice.
245
246 Finally, the path-deps approach is interesting, and if it fits well with your mental model and overall system architecture,
247 it can be a fun way to avoid explicit refreshing. I would look at it last, however, unless the concept really grabs
248 you.