Couple of things to add to that.
Firstly, if using uibuilder v6, please think about switching to the "new" client library. Then you no longer need to load the socket.io library nor do you need uibuilder.start()
and you get access to all the new goodies coming along. Including the improved version of uibuilder.eventSend(event)
.
Secondly, as indicated above, rather than attaching a standard uibuilder.send
, you can use eventSend which gives you extended information such as whether the user was holding down a modifier key (ctrl, shift, ...) when they pressed the button, what page name it came from, etc. Saves you a whole bunch of work.
Have you looked at the examples that are included when you install uibuilder? Worth taking a look at. Also have a look through the standard templates since some of them include buttons already pre-configured for you.
As Andy has indicated, all you need is to add an onclick
event handler that either is set to uibuilder.eventSend(event)
or set to a function of your own that calls uibuilder.send({....})
.
To do this, you need to change the button's disabled
property to true/false.
uibuilder.onChange('msg', (msg) => {
// You need something in the msg to identify what to do
if (msg.topic === 'chg-button1') {
// Assumes that your button has id="button1"
$('#button1').disabled = msg.payload
}
})
Final note - if you were to use the new client library uibuilder.iife.min.js
in place of the older uibuilderfe.min.js
, you could also make use of the low-code capabilities. In that case, you can send a special message from Node-RED without the need for any code in the front end. For example, this should disable a button.
msg._ui = [
{
"method": "update",
"components": [
{
// The type prop lets you use a CSS Selector, this will disable all buttons.
// Alternatively set to `#myid` or some more complex selector as needed.
"type": "button",
"properties": {
"disabled": true
}
}
]
}
]
Obviously, you could set that up with a simple change node set to JSONata can make the true/false part taken from payload: "disabled": payload
.