Access objects in template node

I have created a Template node which contains a number of checkboxes, like this (all have unique names):
<input style="margin-left:0px; margin-right:0px; text-align: left; vertical-align: middle;" type="checkbox" name="chkBox1" >

Is there a way I can access the 'checked' property of each of these from a Function node? On a typical html page with Javascript, I might use document.getElementByID or document.getElementsByName, but it seems I can't use those in NR. I tried flow.get() with the checkbox name, but that didn't work - I didn't think it would.

Ideally, I'd like to use two-way binding for the checked property, but I'm having trouble with that too.

Thanks.
Don

You cannot directly access the template page as this is on the client, not the NR server.

You need to send the checkbox states to NR, and can send back updates as required.

I quickly grabbed some of my code and chopped out the relevant bits.

I'm not expert but but this should point you in the right direction.

[
    {
        "id": "6b3a8eb28379d138",
        "type": "inject",
        "z": "73a94477.15d104",
        "name": "zones",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": "0.5",
        "topic": "",
        "payload": "[{\"name\":\"Front-PIR\",\"value\":false},{\"name\":\"Doorbell\",\"value\":false},{\"name\":\"Porch-Door\",\"value\":false},{\"name\":\"Bell-Motion\",\"value\":false},{\"name\":\"Rear-PIR\",\"value\":false},{\"name\":\"Pub-PIR\",\"value\":false},{\"name\":\"Shed-Door\",\"value\":false},{\"name\":\"Hottub-PIR\",\"value\":false}]",
        "payloadType": "json",
        "x": 865,
        "y": 4515,
        "wires": [
            [
                "fa5a840b837f149c"
            ]
        ]
    },
    {
        "id": "fa5a840b837f149c",
        "type": "ui_template",
        "z": "73a94477.15d104",
        "group": "a605595c.ac6378",
        "name": "",
        "order": 3,
        "width": "6",
        "height": "5",
        "format": "<style>\n    md-checkbox .md-label {\n        top: unset;\n    }\n\n    .nr-dashboard-theme .nr-dashboard-template .md-button:not(:first-of-type) {\n        margin-top: unset;\n    }\n</style>\n\n<form>\n\n    <div layout=\"row\" layout-wrap>\n        <div flex=\"45\" ng-repeat=\"item in msg.payload\">\n            <md-checkbox ng-model=\"item.value\" aria-label=\"checkbox\"> {{item.name}}</md-checkbox>\n\n        </div>\n    </div>\n\n    <md-button ng-click=\"send({select:'all'})\"> All </md-button>\n    <md-button ng-click=\"send({select:'none'})\"> None </md-button>\n   \n</form>\n\n",
        "storeOutMessages": false,
        "fwdInMessages": false,
        "resendOnRefresh": true,
        "templateScope": "local",
        "className": "",
        "x": 1055,
        "y": 4515,
        "wires": [
            [
                "71616dffe2fa7764",
                "7abbf722448f09f4"
            ]
        ],
        "info": "<style>\r\n    md-checkbox .md-label {\r\n        top: unset;\r\n    }\r\n\r\n    .nr-dashboard-theme .nr-dashboard-template .md-button:not(:first-of-type) {\r\n        margin-top: unset;\r\n    }\r\n</style>\r\n\r\n<form>\r\n\r\n    <div layout=\"row\" layout-wrap>\r\n        <div flex=\"45\" ng-repeat=\"item in msg.payload\">\r\n            <md-checkbox ng-model=\"item.value\" aria-label=\"checkbox\"> {{item.name}}</md-checkbox>\r\n\r\n        </div>\r\n    </div>\r\n\r\n    <md-button ng-click=\"send(msg)\"> Refresh </md-button>\r\n    <md-button ng-click=\"send({select:'all'})\"> All </md-button>\r\n    <md-button ng-click=\"send({select:'none'})\"> None </md-button>\r\n    <md-button ng-click=\"send({select:'delete'})\"> delete </md-button>\r\n\r\n</form>\r\n\r\n"
    },
    {
        "id": "71616dffe2fa7764",
        "type": "function",
        "z": "73a94477.15d104",
        "name": "get records",
        "func": "\nif (msg.select == \"all\") {\n    let msg1 = {\n        \"payload\": [\n            {\n                \"name\": \"Front-PIR\",\n                \"value\": true\n            },\n            {\n                \"name\": \"Doorbell\",\n                \"value\": true\n            },\n            {\n                \"name\": \"Porch-Door\",\n                \"value\": true\n            },\n            {\n                \"name\": \"Bell-Motion\",\n                \"value\": true\n            },\n            {\n                \"name\": \"Rear-PIR\",\n                \"value\": true\n            },\n            {\n                \"name\": \"Pub-PIR\",\n                \"value\": true\n            },\n            {\n                \"name\": \"Shed-Door\",\n                \"value\": true\n            },\n            {\n                \"name\": \"Hottub-PIR\",\n                \"value\": true\n            }]\n        , \"socketid\": msg.socketid\n    }\n    return msg1\n}\nif (msg.select == \"none\") {\n    let msg1 = {\n        \"payload\": [\n            {\n                \"name\": \"Front-PIR\",\n                \"value\": false\n            },\n            {\n                \"name\": \"Doorbell\",\n                \"value\": false\n            },\n            {\n                \"name\": \"Porch-Door\",\n                \"value\": false\n            },\n            {\n                \"name\": \"Bell-Motion\",\n                \"value\": false\n            },\n            {\n                \"name\": \"Rear-PIR\",\n                \"value\": false\n            },\n            {\n                \"name\": \"Pub-PIR\",\n                \"value\": false\n            },\n            {\n                \"name\": \"Shed-Door\",\n                \"value\": false\n            },\n            {\n                \"name\": \"Hottub-PIR\",\n                \"value\": false\n            }]\n        , \"socketid\": msg.socketid\n    }\n    return  msg1\n\n}\n\n\n\nreturn msg;\n\n\n",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1040,
        "y": 4395,
        "wires": [
            [
                "fa5a840b837f149c"
            ]
        ],
        "info": "if (msg.select == \"delete\") {\r\n\r\n    msg.payload = \"Delete the selected \"+flow.get(\"records\") +\" Alarms ?\" \r\n    return [null, msg]\r\n}\r\n\r\nif (msg.select == \"all\") {\r\n    let msg1 = {\r\n        \"payload\": [\r\n            {\r\n                \"name\": \"Front-PIR\",\r\n                \"value\": true\r\n            },\r\n            {\r\n                \"name\": \"Doorbell\",\r\n                \"value\": true\r\n            },\r\n            {\r\n                \"name\": \"Porch-Door\",\r\n                \"value\": true\r\n            },\r\n            {\r\n                \"name\": \"Bell-Motion\",\r\n                \"value\": true\r\n            },\r\n            {\r\n                \"name\": \"Rear-PIR\",\r\n                \"value\": true\r\n            },\r\n            {\r\n                \"name\": \"Pub-PIR\",\r\n                \"value\": true\r\n            },\r\n            {\r\n                \"name\": \"Shed-Door\",\r\n                \"value\": true\r\n            },\r\n            {\r\n                \"name\": \"Hottub-PIR\",\r\n                \"value\": true\r\n            }]\r\n            , \"socketid\": msg.socketid\r\n    }\r\n    return [null, null, msg1]\r\n}\r\nif (msg.select == \"none\") {\r\n    let msg1 = {\r\n        \"payload\": [\r\n            {\r\n                \"name\": \"Front-PIR\",\r\n                \"value\": false\r\n            },\r\n            {\r\n                \"name\": \"Doorbell\",\r\n                \"value\": false\r\n            },\r\n            {\r\n                \"name\": \"Porch-Door\",\r\n                \"value\": false\r\n            },\r\n            {\r\n                \"name\": \"Bell-Motion\",\r\n                \"value\": false\r\n            },\r\n            {\r\n                \"name\": \"Rear-PIR\",\r\n                \"value\": false\r\n            },\r\n            {\r\n                \"name\": \"Pub-PIR\",\r\n                \"value\": false\r\n            },\r\n            {\r\n                \"name\": \"Shed-Door\",\r\n                \"value\": false\r\n            },\r\n            {\r\n                \"name\": \"Hottub-PIR\",\r\n                \"value\": false\r\n            }]\r\n            , \"socketid\": msg.socketid\r\n    }\r\n    return [null, null, msg1]\r\n\r\n}\r\n\r\nlet msg3={records:\"working..\"}\r\nnode.send (msg3)\r\n\r\nlet events = flow.get(\"motionEvents\")\r\nlet startDate = flow.get(\"startDate\") || new Date()\r\nlet endDate = flow.get(\"endDate\") || new Date()\r\nvar list = []\r\nvar items = []\r\n\r\nmsg.payload.forEach(function (index) {\r\n    if (index.value == true) {\r\n        items.push(index.name);\r\n    }\r\n});\r\n\r\nnode.status({ text: items.toString() })\r\nevents = events.filter(el => items.includes(el.zone))\r\n\r\nevents = events.filter(el => el.time >= startDate && el.time <= endDate)\r\n\r\n\r\nconst dayNames = [\"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\"];\r\nconst month = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"];\r\nfor (let index = 0; index < events.length; index++) {\r\n    var date = new Date(events[index].time)\r\n    var h = date.getHours();\r\n    var m = date.getMinutes();\r\n    var s = date.getSeconds();\r\n    var d = date.getDate();\r\n    let day = (dayNames[date.getDay()]);\r\n    let mnth = month[date.getMonth()];\r\n    if (m < 10) { var z = m.toString(); m = \"0\" + z; }\r\n    if (h < 10) { var z = h.toString(); h = \"0\" + z; }\r\n    if (s < 10) { var z = s.toString(); s = \"0\" + z; }\r\n    let time = day + \" \" + d + \" \" + mnth + \" \" + h + \":\" + m + \":\" + s;\r\n    //const row = { title: time + \" > \" + events[index].zone, description: events[index].time };\r\n    const row = { title: time + \" > \" + events[index].zone, description: events[index].time + \"_\" + events[index].zone + \".jpg\" };\r\n    list.push(row);\r\n}\r\n\r\nmsg.records = list.length\r\nmsg.payload = list\r\nflow.set(\"list\", list)\r\nflow.set(\"records\", msg.records)\r\n\r\n\r\nreturn msg;\r\n\r\n\r\n"
    },
    {
        "id": "7abbf722448f09f4",
        "type": "debug",
        "z": "73a94477.15d104",
        "name": "debug 323",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1255,
        "y": 4515,
        "wires": []
    },
    {
        "id": "a605595c.ac6378",
        "type": "ui_group",
        "name": "Default",
        "tab": "78387f0f.22098",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "78387f0f.22098",
        "type": "ui_tab",
        "name": "Homex",
        "icon": "dashboard",
        "order": 25,
        "disabled": false,
        "hidden": false
    }
]

@donsay Can you give this flow a try and see if it helps you ?

Thanks. I ran that, and I think it will be helpful.
I have to figure how it works though :).

Don

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