Hi, I'm having trouble updating an html table. It causes the page to flicker and is very unpleasant. Hope someone knows how to avoid this.
I have a table with data rows. Every half second I update a few rows if their data has changed.
My first attempt was to simply rebuild all the cells in the row (naive I know, but it's a starting point):
row = get_row (rowid) # returns a <tr> element
row.replaceChildren () # clears all child elements
for data in source :
cell = make_node ('td', data)
row.append (cell)
However this makes a noticeable flicker. Not fast enough to see a row disappear, but the entire table continually dances. Very hard on the eyes.
Ok, I thought. I'll make all the new cells first, then assign them as the row's children. So I tried this:
container = [ ]
for data in source :
cell = make_node ('td', data)
container.append (cell)
row.replaceChildren (*container)
Doesn't work, still have the flicker. What else I tried:
- assigning to .children directly doesn't work (read only in brython)
- making a new row (<tr> node) as container doesn't work. I tried replacing the original row with the new row using old_row.parent.replaceChild (new_row, old_row). So one DOM change for the entire row, instead of X replacements for each child cell. Doesn't matter. Still get the same flicker.
What's the solution for updating a table in the DOM? Am I cursed with editing each existing cell one at a time? Simple things like this shouldn't be so ridiculously complicated. Web development is a jenga tower with half the pieces already gone. 🙄
Thanks if anyone has advice. Happy holidays!
Edward