Semi OT: Is it legit for an ajax call to return th...
# cfml-general
d
Semi OT: Is it legit for an ajax call to return the html of a set of table rows, only, with no enclosing table or tbody elements?
b
that's a trick question, right? I mean... It IS 2024, yes? That sounds like something I would have done in a previous life - like in 2023... 😉
d
Well maybe, but not the way you're thinking. I have a window that's full screen, the full screen element being an html table. A pseudo-dialog opens where the user can update some stuff, and when they save, I need to refresh the table. If I just reload the page, it gets kicked out full screen mode. Same if ajax replaces the whole table. What does work is replacing the tbody of that table. It appears to work perfectly in fact, so maybe I'm worrying about nothing. What do I mean by "legit" anyway? Is it only an issue if the 2024 Best Practices Police come knocking? What would they suggest I do instead? That's the actual question I suppose.
Suppose I wanted to update a single row in the table? Would I need to return json data and replace the content of each cell one by one? Really don't want to do that for the whole tbody, esp when this appears to work very well.
m
If i'm writing it for myself and it worked already, I would likely just go with it, and wouldn't lose any sleep over it. I usually won't send back a row as html though, because now 2 places need to know the structure of the table. i usually send back something that has a struct in it it, and loop over that object within the success handler, and matching up the keys to some data- attribute on the td.
🎯 1
a
This is exactly how things like Livewire and Alpine.js work
☝️ 3
d
Which "this" is how Livewire and Alpine.js work? Returning html dom fragments, or returning data and building the dom elements manually?
a
Sorry. I mean that the AJAX request returns HTML. The JS then does smart diff to work out which elements to update in the DOM.
👍 1
d
@Matt Jones Actually in my case it's just one piece of code. The full page calls an include to render that part of the page, including the table. That partial renders the tbody inside cfsavecontent, and in normal operation, outputs the captured html at the right location in the parent table the partial renders. The ajax call calls that same partial, with a URL parameter that says to just return the tbody html and exit.
m
HTMX works like this. You call a page which returns full DOM fragments, prerendered on the server. It is how I build most of my apps (not that I build many these days)
👍 1
d
Yeah but htmx sucks ;)
m
THEMS FIGHTIN WORDS SIR Luckily I have no state management so I'll be fine in a moment lol
d
Isn't that what Carson says?
m
haha it is actually 🙂
But it is absolute the BEST meme framework in the entire world ❤️
j
Tagging on to what @aliaspooryorik said, if you are interested in a Livewire/AlpineJS solution, you should really check out CBWire. @Grant Copley has put a ton of time in to it and and you can author everything in CFML without custom JS: https://cbwire.ortusbooks.com
🙏 1
👍 1
🙂 1
☝️ 1