Select TWO dates from a ui_table and set payload mindate and maxdate

I have a ui_table in a dashboard with dates populated. The requirement is to select two dates from the table by clicking it one after another from same table. But with each progressive click the function should calculate minimum of the dates (last click and present click) set mindate to the lowest date and maxdate to the highest date. so for next click again it should calculate the min and max and set the payload accordingly.
i have put in a sample flow with inject nodes populating the ui_table for anyone to try this out.

image

[{"id":"8aa48dca4503e76d","type":"inject","z":"62edb5cfe5bc1272","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"Date\":\"2023-04-26\"},{\"Date\":\"2023-04-25\"},{\"Date\":\"2023-04-24\"},{\"Date\":\"2023-04-23\"},{\"Date\":\"2023-04-22\"},{\"Date\":\"2023-04-21\"},{\"Date\":\"2023-04-20\"},{\"Date\":\"2023-04-19\"},{\"Date\":\"2023-04-18\"},{\"Date\":\"2023-04-17\"},{\"Date\":\"2023-04-16\"},{\"Date\":\"2023-04-15\"},{\"Date\":\"2023-04-14\"},{\"Date\":\"2023-04-13\"},{\"Date\":\"2023-04-12\"},{\"Date\":\"2023-04-11\"},{\"Date\":\"2023-04-10\"},{\"Date\":\"2023-04-09\"},{\"Date\":\"2023-04-08\"},{\"Date\":\"2023-04-07\"}]","payloadType":"json","x":320,"y":1600,"wires":[["8cfcd8a7cc53bd4b"]]},{"id":"8cfcd8a7cc53bd4b","type":"ui_table","z":"62edb5cfe5bc1272","group":"bc03f2e4910af504","name":"Date","order":10,"width":"8","height":"11","columns":[],"outputs":1,"cts":true,"x":470,"y":1600,"wires":[["c0e7fc4eadd5b8fb"]]},{"id":"c0e7fc4eadd5b8fb","type":"debug","z":"62edb5cfe5bc1272","name":"debug 286","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":1600,"wires":[]},{"id":"bc03f2e4910af504","type":"ui_group","name":"BD_STATUS","tab":"da4cef9c6caa6145","order":1,"disp":false,"width":"42","collapse":false,"className":""},{"id":"da4cef9c6caa6145","type":"ui_tab","name":"HOME","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

Try

let date_stored = context.get("date_store") ?? [];
date_stored.unshift(msg.payload.Date);
date_stored = date_stored.slice(0,2);
context.set("date_store", date_stored);
let sort_date = RED.util.cloneMessage(date_stored).sort();
msg.payload = {
    minDate: sort_date[0],
    maxDate: sort_date[1]
}
return msg;

Or low code

[{"id":"8aa48dca4503e76d","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"Date\":\"2023-04-26\"},{\"Date\":\"2023-04-25\"},{\"Date\":\"2023-04-24\"},{\"Date\":\"2023-04-23\"},{\"Date\":\"2023-04-22\"},{\"Date\":\"2023-04-21\"},{\"Date\":\"2023-04-20\"},{\"Date\":\"2023-04-19\"},{\"Date\":\"2023-04-18\"},{\"Date\":\"2023-04-17\"},{\"Date\":\"2023-04-16\"},{\"Date\":\"2023-04-15\"},{\"Date\":\"2023-04-14\"},{\"Date\":\"2023-04-13\"},{\"Date\":\"2023-04-12\"},{\"Date\":\"2023-04-11\"},{\"Date\":\"2023-04-10\"},{\"Date\":\"2023-04-09\"},{\"Date\":\"2023-04-08\"},{\"Date\":\"2023-04-07\"}]","payloadType":"json","x":70,"y":840,"wires":[["8cfcd8a7cc53bd4b"]]},{"id":"8cfcd8a7cc53bd4b","type":"ui_table","z":"b9860b4b9de8c8da","group":"bc03f2e4910af504","name":"Date","order":10,"width":"8","height":"11","columns":[],"outputs":1,"cts":true,"x":210,"y":840,"wires":[["2458a432fb178a8e"]]},{"id":"2458a432fb178a8e","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.Date","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":880,"wires":[["53511b34ac3cdc8e"]]},{"id":"53511b34ac3cdc8e","type":"join","z":"b9860b4b9de8c8da","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":450,"y":880,"wires":[["d273555ebbdfb91f","989780d57c6ec284"]]},{"id":"989780d57c6ec284","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[1]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":800,"wires":[["53511b34ac3cdc8e"]]},{"id":"d273555ebbdfb91f","type":"sort","z":"b9860b4b9de8c8da","name":"","order":"ascending","as_num":false,"target":"payload","targetType":"msg","msgKey":"payload","msgKeyType":"elem","seqKey":"payload","seqKeyType":"msg","x":590,"y":880,"wires":[["b4b8e79a3b5054c1"]]},{"id":"b4b8e79a3b5054c1","type":"template","z":"b9860b4b9de8c8da","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n    \"minDate\":\"{{payload.0}}\",\n    \"maxDate\":\"{{payload.1}}\"\n}","output":"json","x":720,"y":880,"wires":[["ab11733af440de57","c0e7fc4eadd5b8fb"]]},{"id":"ab11733af440de57","type":"ui_text","z":"b9860b4b9de8c8da","group":"bc03f2e4910af504","order":1,"width":0,"height":0,"name":"","label":"","format":"{{msg.payload}}","layout":"row-left","className":"","x":490,"y":960,"wires":[]},{"id":"c0e7fc4eadd5b8fb","type":"debug","z":"b9860b4b9de8c8da","name":"debug 286","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":940,"wires":[]},{"id":"bc03f2e4910af504","type":"ui_group","name":"BD_STATUS","tab":"da4cef9c6caa6145","order":1,"disp":false,"width":"42","collapse":false,"className":""},{"id":"da4cef9c6caa6145","type":"ui_tab","name":"HOME","icon":"dashboard","order":2,"disabled":false,"hidden":false}]
1 Like

Works perfectly. I used the low code version since it is easy to change as per requirement.

Perfectly understood my question and gave an apt answer. Thank You.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.