-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect OOB swapping of second table in a two tables page #3203
Comments
Hey, table elements such as See the "troublesome tables" section of the docs about that. Could you try encapsulating, in your server response's HTML, your second table (the oob swapped one), in a
<template>
<tr id="message" hx-swap-oob="true"><td>Joe</td><td>Smith</td></tr>
</template> Hope this helps! |
Hi,
As a matter of fact, I did try to wrap the entire table in a template but it's not working: the table is not rendered at all. When I looked inside htmx code, the fragment processed by makeFragment function is something like this:
It starts from |
There is a critical difference, the trigger is inside the header of first table:
|
Thanks for the details. Even with the following setup, I get the same result as in my previous message: <table id="myTable">
<thead>
<tr>
<th>
<input type="text" hx-get="/test" hx-trigger="input changed delay:500ms" hx-target="#tablebody" hx-swap="outerHTML"></input>
</th>
</tr>
</thead>
<tbody id="tablebody"><tr><td>Initial</td></tr></tbody>
</table>
<table id="myOobTable">
<thead><tr><th>Test OOB</th></tr></thead>
<tbody><tr><td>Initial OOB</td></tr></tbody>
</table> Server response: same as before So there's likely something else going on. May I ask again:
Thanks |
It doesn't work for me. And yes, I'm using 2.0.4. Also I'm using Edge. Initial page sent from server: Initial page rendered in browser: Server response after first input: Html rendered after first server response: Server response after second input (fully identical with first server response): Html rendered after second server response: Also, here is a screenshot with the browser after second input: |
Thanks, this helps a lot! So, if I use your initial HTML ( However, if I wrap the oob table in a <tbody id="tablebody"><tr><td>Test</td></tr></tbody>
<template>
<table id="myOobTable" hx-swap-oob="true">
<thead><tr><th> Oob header</th></tr></thead>
<tbody><tr><td> Oob col</td></tr></tbody>
</table>
</template> Notice how I wrapped Hope this helps! |
Yes, that works, thanks. |
Yeah usually we have people swapping a table element (tr td etc.) as OOB, which then works fine wrapped in a template. If you are interested in improving the docs btw, PRs are always welcome 😄 |
The cause of this issue is that the contents of the whole page including the main swap and all oob swaps has to be valid content that can be parsed by the browser. htmx takes this data and converts it into a document fragment so that it can process it without having to do regex and manual text manipulation so it just uses standard browser functions to parse the html content. But this means the content as a whole has to be processable. in your partial response you had a tbody tag which by the html spec can only live inside a table and have other siblings that are also the same. So when it gets to the table tag with the oob swap it is unable to process this tag as you can't have a table next to a tbody and be valid html. So it has to skip over the table tag and this is why it then finds the tables contents and because these are valid sibling content it inserts this into the destination table. This is why we have instructions under troublesome tables to wrap the oob content in template tags as template tags override the html placment rules. I think where the documentation is a little confusing as it would seem that you do this because the items being wrapped for oob are the problem if they are things like table sub elements but it is actually the sibling relationship between the main content nodes and the one or more oob nodes that is the real cause of the issues. So you need to wrap oob tags in template tags both if the oob tags are problem types but also if the main content nodes above or below them are problem node types. Maybe we could change the documentation to something like: Troublesome Tables and listsNote that you can use a Here is an example with an out-of-band swap of a table row being encapsulated in this way: <div>
...
</div>
<template>
<tr id="row" hx-swap-oob="true">
...
</tr>
</template> Note that these template tags will be removed from the final content of the page. |
I have two tables, I swap regularly the tbody of the first one and OOB swap the whole second table.
After swap the inner html of the second table (with the new content) is included in the first table and the second table is not touched. Any additional swap will add the inner html of the second table again into the first table.
OOB swapping works normal if I use something else instead of second table.
Additional info (and I think here is the problem): the trigger for swap is inside the first table, more exactly in an input field in one header. I think your function makeFragment is not working correctly in this case.
Before swap:
After first swap:
After second swap:
The text was updated successfully, but these errors were encountered: