Can i change a css class dynamically

Hi,
I would change a class in the ui template node dynamically? Is that somehow possible? I tried it with several options. but i can't get the solution. I'm not firm with HTML.

to my data input, I have 15 inputs with "item-gridPass", "item-gridFail" and 15 corresponding qr codes. My 15 item grids "should" change the class and the QR-codes shoult displayed in the grid (easy thing)

My Payload for example for the first input of 15

PCB1: object

status: "item-gridFail"

qrcontent: "QR-Code not detected"

My code:

<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
    grid-gap: 5px;
    justify-items: center;
    align-items: center;
}
.grid-itemPass {
    background: lime;
    text-align: center;
    border: black 5px solid;
    width: 100px;
    height: 100px;
}
.grid-itemFail {
background: red;
text-align: center;
border: black 5px solid;
width: 100px;
height: 100px;
}

</style>
</head>
<body>
<div class="grid-container">
    <div class="{{PCB1.status}}">{{PCB1.qrcontent}}</div><!-- thats what i wanted to show you-->
    <div class="grid-item1">2</div>
    <div class="grid-item1">3</div>
    <div class="grid-item1">4</div>
    <div class="grid-item1">5</div>

</div>
<div class="grid-container">
    <div class="grid-item1">1</div>
    <div class="grid-item1">2</div>
    <div class="grid-item1">3</div>
    <div class="grid-item1">4</div>
    <div class="grid-item1">5</div>

</div>
<div class="grid-container">
    <div class="grid-item1">1</div>
    <div class="grid-item1">2</div>
    <div class="grid-item1">3</div>
    <div class="grid-item1">4</div>
    <div class="grid-item1">5</div>

</div>
</body>
</html>

I hope you can help me with my Problem

best regards
Freshtronic

First it is unclear what node you are using, But in the template node <html> , <head> and <body> tags are not required as they are already added to the page by the dashboard.

As to setting css class for nodes you can send the class name you wish to use using msg.className with the class name as the value.

Maybe supply a simple example flow of what you are attempting, this may make things clearer to understand.

Hello @Freshtronic and welcome to the forum.

I always put my CSS in a separate ui-template

<style>
.grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
    grid-gap: 5px;
    justify-items: center;
    align-items: center;
}
etc
</style>

Are you passing msg.PCB1 or msg.payload.PCB1?

The ui_template does not understand mustache syntax so you need to build your HTML in a standard template and pass it to ui_template as msg.template.
An example:
image

[{"id":"73aa09230ed9400d","type":"ui_template","z":"b946eb07.fa1b98","group":"db338ac57739c80e","name":"","order":5,"width":0,"height":0,"format":"<div class=\"grid-container\">\n    <div class={{payload.PCB1.status}}>{{PCB1.qrcontent}}</div><!-- thats what i wanted to show you-->\n    <div class=\"grid-item1\">2</div>\n    <div class=\"grid-item1\">3</div>\n    <div class=\"grid-item1\">4</div>\n    <div class=\"grid-item1\">5</div>\n</div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","className":"","x":460,"y":180,"wires":[[]]},{"id":"680d2994deef7d29","type":"inject","z":"b946eb07.fa1b98","name":"","props":[{"p":"PCB1","v":"{\"status\":\"grid-ItemFail\",\"qrcontent\":\"qr here\"}","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":180,"wires":[["066d9c2ccb7e4180"]]},{"id":"066d9c2ccb7e4180","type":"template","z":"b946eb07.fa1b98","name":"","field":"template","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<div class=\"grid-container\">\n    <div class={{PCB1.status}}>{{PCB1.qrcontent}}</div><!-- thats what i wanted to show you-->\n    <div class=\"grid-item1\">2</div>\n    <div class=\"grid-item1\">3</div>\n    <div class=\"grid-item1\">4</div>\n    <div class=\"grid-item1\">5</div>\n</div>","output":"str","x":300,"y":180,"wires":[["73aa09230ed9400d"]]},{"id":"e5b3a6c0a534673a","type":"ui_template","z":"b946eb07.fa1b98","group":"d6cd152bba56e445","name":"CSS","order":5,"width":0,"height":0,"format":"<style>\n.grid-container {\n    display: grid;\n    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));\n    grid-gap: 5px;\n    justify-items: center;\n    align-items: center;\n}\n.grid-ItemPass {\n    background: lime;\n    text-align: center;\n    border: black 5px solid;\n    width: 100px;\n    height: 100px;\n}\n.grid-ItemFail {\n    background: red;\n    text-align: center;\n    border: black 5px solid;\n    width: 100px;\n    height: 100px;\n}\n</style>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"global","className":"","x":450,"y":140,"wires":[[]]},{"id":"db338ac57739c80e","type":"ui_group","name":"Default","tab":"8faf8dbf8c8fc6ca","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"d6cd152bba56e445","type":"ui_group","name":"Memory","tab":"2dde50a55cca44db","order":1,"disp":true,"width":"10","collapse":false,"className":""},{"id":"8faf8dbf8c8fc6ca","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false},{"id":"2dde50a55cca44db","type":"ui_tab","name":"Barnt Green","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

image

I think that passing msg.className is not appropriate in this instance because the template has some components with class Pass and some with class Fail

Hi,
thank you for your fast possible solution. I changed my payload to msg.PCB1.

and that´s my payload. sorry it's in german

image

ohh okay the template node has the option to make a head and there then i can put the css class
and the other one is for the body.

thank you thank you very much for your solution

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