Is it possible for my snippet to be sorted chronologically with the current date on top. Currently, the added row goes below the previous. Is there a way to alter that behaviour? Would it make more sense to use a list and then sort by date? If so, could someone guide me please?
{run: count=1}{repeat: for i in seq(1,count)}- {formdate: MM/DD/YYYY} - {formtext: name=details}{="\n"}{endrepeat: trim=yes}{button: count= count+ 1; label=➕}
Since my original post, here's what I've come up with so far:
{run: events=[ ]
}{formdate: MM/DD/YYYY; name=date} - {formtext: name=details}{button: #add to events list
events=merge(events, [date,details])
; label=➕}
{=events}
This has led to a different question; is there a way to merge items into the list, with keys, as an object? If so, then returning the sorted output should be fairly straight forward.
Yes, use merge to merge two lists (not to add items to a list).
Here's one solution:
{default=[{time: YYYY-MM-DD},""]}
{run: list=[default]
insert=0
}{repeat: for item in list}- {time: MMMM Do YYYY; at={=item[1]}} {=item[2]}{="\n"}{endrepeat: trim=yes}{button: insert=1
; label=➕}{if: insert=1}{formdate: YYYY-MM-DD; name=date} - {formtext: name=text}
{button: list=sort(merge(list,[[{=date},{=text}]]),(a,b)->datetimediff(a[1],b[1],"D"))
insert=0
; label=Insert}
{endif}
2 Likes
Thanks Dan. This works near perfect. The one issue I'm having is that the Default value sticks around. I'm guessing I'd have to manually fill in the first entry within the default variable.
This raises the question: Are there any plans to implement an append function for lists? Additionally, will there be a way to add values to keys within keyed lists and create individual objects?
Hi @latetotheparty,
Do you want to have something like this where I am storing all the details corresponding to a date in a keyed list, where the value is also a list? I have converted keyed list back to ordered list before sorting because keyed list doesn't support sorting.
{default=[{time: YYYY-MM-DD}:["detail1"]]}
{run: list=default
insert=0
}
{repeat: for item in sort([[k, v] for (k, v) in list], (a, b)->datetimediff(a[1],b[1],"D"))}- {time: MMMM Do YYYY; at={=item[1]}} {=join(item[2], ", ")}{="\n"}
{endrepeat: trim=yes}
{button: insert=1
; label=➕}
{if: insert=1}
{formdate: YYYY-MM-DD; name=date} - {formtext: name=text}
{button: list=merge(list,[{=date}:merge(catch(list[date], []
), [{=text}])])
insert=0
; label=Insert}
{endif}
1 Like
Thanks Ashwin. I like this approach better.