How can I send a message from a ui_button inside an HTML template?

Hey!

I am trying to combine node-red, html and javascript.
I have created the below button with html.

<div>
     <button type="button" class="button save" data-action="save">Save</button>
</div>

If Ι press the html button then I can call the below script.

<script id="test-js">  

var saveButton = wrapper.querySelector("[data-action=save]");

saveButton.addEventListener("click", function (event) {
  if (canvas.isEmpty()) {
   ............
  } else {
   .............
  }
});

 </script>

I would like to stop using the html button and replace it with a ui_button.
Is it possible?

Not able to check right now but I'm fairly certain the built in help on the ui_template node gives to an example of sending a msg from html

Also, search the forum and the flows

1 Like

node-red-dashbaord has their own controller. the easiest way is to use ng-click. The send() command was already bind in the scope by node-red-dashboard. Let's try.

<button type="button" class="button save" ng-click="send({topic:'click', payload:'button was clicked'})">

Actually, I normally use below code in my ui dashboard

<md-button class="button save" ng-click="send({topic:'clicked', payload:'button was clicked'})">SAVE</md-button>

tbdltee thanks about the ng-click properties.

However I don't understand how to move the data-action=save from html button at the saveButton variable with ng-click.

I try this

<md-button class="button save" ng-click="send({topic:'clicked', payload:'button was clicked', data-action:'save'})">SAVE</md-button>

but it does not work :roll_eyes:

Are you sure it's not working? Have you attached a debug node to the output of the ui_template? Do that, deploy, click the button, see what happens.

Thanks for your help!!!

In the below code the two html buttons show a pop up message, different for every button.

[{"id":"5f4b8b13.af3a84","type":"ui_template","z":"5087a524.121224","group":"4e5b45cb.163f3c","name":"","order":5,"width":0,"height":0,"format":"<!DOCTYPE html>\n<html>\n<body>\n    \n\n    <div>\n          <button id=\"btn1\" type=\"button\" class=\"button clear\" data-action=\"clear\">Clear</button>\n          <button id=\"btn2\" type=\"button\" class=\"button save\"  data-action=\"save\">Save</button>\n    </div>\n\n<md-button  id=\"btn1\" class=\"button save\" ng-click=\"send({topic:'clicked', payload:'Clear'})\">Clear</md-button>\n<md-button  id=\"btn2\" class=\"button save\" ng-click=\"send({topic:'clicked', payload:'Save'})\">Save</md-button>\n  \n  \n<script>\nvar x = document.getElementById(\"btn1\");\nx.addEventListener(\"click\", Function1);\n\nvar x = document.getElementById(\"btn2\");\nx.addEventListener(\"click\", Function2);\n\nfunction Function1() {\nalert (\"Clear\");\n}\n\n\nfunction Function2() {\nalert (\"Save\");\n}\n\n</script>\n\n</body>\n\n</html>","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":1020,"y":760,"wires":[["27560da1.b027c2"]]},{"id":"72244df.a895eb4","type":"ui_button","z":"5087a524.121224","name":"","group":"4e5b45cb.163f3c","order":6,"width":"2","height":"2","passthru":false,"label":"CLEAR","tooltip":"","color":"","bgcolor":"","icon":"","payload":"clear","payloadType":"str","topic":"","x":760,"y":760,"wires":[["5f4b8b13.af3a84"]]},{"id":"709bca52.73b784","type":"ui_button","z":"5087a524.121224","name":"","group":"4e5b45cb.163f3c","order":6,"width":"2","height":"2","passthru":false,"label":"SAVE","tooltip":"","color":"","bgcolor":"","icon":"","payload":"save","payloadType":"str","topic":"","x":750,"y":800,"wires":[["5f4b8b13.af3a84"]]},{"id":"27560da1.b027c2","type":"debug","z":"5087a524.121224","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1170,"y":760,"wires":[]},{"id":"4e5b45cb.163f3c","type":"ui_group","z":"","name":"Logo","tab":"2df54972.e9d336","order":1,"disp":false,"width":"4","collapse":false},{"id":"2df54972.e9d336","type":"ui_tab","z":"","name":"EVEXIA","icon":"dashboard","order":4,"disabled":false,"hidden":false}]

How can I activate the same operation (show a popup message) with the ui_buttons and md-button through the Function1 and Function2 of ui_template?

the data-action key contain special character, i.e. - you need to put qoute char (') to make it work. It will be

<md-button class="button save" ng-click="send({topic:'clicked', payload:'button was clicked', 'data-action':'save'})">SAVE</md-button>