Hello, I am new to node-red. so i don't know i am asking the right question or not. But what i want to do is- after getting data from sqlite database i want to access it in the script tag of template node. I have tried this code.
I had problems with that too.
I resolved that issue by generating the content of the template node in a function. Copy all contents into a string and set the string to msg.template.
For me it looked like this:
var max_width = '280px'
var htmlstart = '<div class="dropdown"><div id="myDropdownC3" class="dropdown-contentC3"> <input type="text" placeholder="Search.." id="myInputC3" onkeyup="filterFunctionC31()">';
var htmlend = '</div></div>';
var style = '<style>.dropbtn { background-color: #0094ce; color: white; padding: 10px; font-size: 16px; border: none; cursor: pointer;}.dropbtn:hover, .dropbtn:focus { background-color: #00A6E8;}#myInputC3 { border-box: box-sizing; background-image: url("https://www.w3schools.com/howto/searchicon.png"); background-position: 14px 12px; background-repeat: no-repeat; font-size: 16px; padding: 14px 20px 12px 45px; border: none; border-bottom: 1px solid #ddd;}#myInputC3:focus {outline: 3px solid #ddd;}.dropdown { position: relative; display: inline-block;}.dropdown-contentC3 { position: absolute; background-color: #f6f6f6; min-width: 100px; max-width: ' + max_width + '; overflow: auto; border: 1px solid #ddd; z-index: 1;}.dropdown-contentC3 a { color: black; padding: 12px 16px; text-decoration: none; display: block;}.dropdown a:hover {background-color: #ddd;}.show {display: block;}</style>';
var script = '<script>/* When the user clicks on the button,toggle between hiding and showing the dropdown content */function myFunctionC3() { document.getElementById(\"myDropdownC3\").classList.toggle(\"show\");}function filterFunctionC31() { var input, filter, ul, li, a, i; input = document.getElementById(\"myInputC3\"); filter = input.value.toUpperCase(); div = document.getElementById(\"myDropdownC3\"); a = div.getElementsByTagName(\"a\"); for (i = 0; i < a.length; i++) { if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) { a[i].style.display = \"\"; } else { a[i].style.display = \"none\"; } }}</script>';
var vals = "";
var length = msg.options.length;
for(var i = 0; i < length; i++){
var obj = msg.options[i];
var name = Object.keys(obj)[0];
var value = obj[Object.keys(obj)[0]];
vals += '<a ng-click="send({payload: ' + value + '})" onclick="myFunctionC3()" >'+name+'</a>';
}
msg.template = style + htmlstart + vals + htmlend+script;
return msg;
You are getting the core Template mixed up with the Node-RED Dashboard UI-Template node.
The core Template node uses the mustache syntax to generate text based on the message passed to the node. You are returning that text in response to an HTTP request. So the browser will handle that response as HTML and display it - but there is no special link between what is in that page and the flow.
The sort of syntax you are trying to use is what you would use in the Node-RED Dashboard UI Template node in order to handle messages passed to that node. This node can only be used in the context of Node-RED Dashboard - you can't use it in your own page outside of the dashboard.
If you want to generate a page that can send/receive messages from a Node-RED flow, without using Node-RED Dashboard, (or any of the other dashboard modules such as ui-builder), then you'll have to create the comms link between the page and node-red yourself, such as with a WebSocket connect.