In the brower (i.e. via the JS backend) I need to generate a HTML table in the DOM from a JSON structure which I load into a Nim table structure. Does a library exist for the conversion to a DOM table?
Thanks for any pointers!
Example using karax, jstable.nim:
import tables
include karax / prelude
type
MyRow = tuple[a, b, c: int]
MyTable = seq[MyRow]
proc createDom(): VNode =
let t = @[(1, 2, 3).MyRow, (4, 5, 6), (7, 8, 9)]
result = buildHtml(tdiv):
table:
tr:
for k, _ in t[0].fieldPairs:
th text k
for r in t:
tr:
for c in r.fields:
td text $(c)
setRenderer createDom
Compile and run with karun -r jstable.
I tried doing something like
collect(<>table()):
for row in data:
collect(<>tr()):
for item in row:
<>td(newText(item))
but Nim didn't like it :(
sugar needs to be extended to handle prefix calls, but it works like this:
import htmlgen,xmltree,sugar
let data = @[@[1,2,3],@[4,5,6],@[7,8,9]]
let y = collect(`<>`(table())):
for row in data:
collect(`<>`(tr())):
for item in row:
<>td(newText($item))
echo y