Notifications
Clear all

JAVASCRIPT functions

5 Posts
2 Users
0 Reactions
1,873 Views
(@manoj-kalekar)
Posts: 8
Member
Topic starter
 

Based on the value I have to show the text message.  I can do this using this in rules / conditions.  But the same is required in many pages. To avoid this , I wanted to make a function and pass the value to show the message.

can you help me to create global JS function or provide a link of similiar example.

Simple example:   jsVal2Word(0) = "Zero" ,  (1)="One",    (2)="Two"

 
Posted : 06/02/2025 6:11 a.m. CET
MarAlz-RW
(@maralz)
Posts: 15
Member
 

Do you mean a pop-up message or a value that is displayed in a label / text box, for etc.?

 
Posted : 10/02/2025 3:30 p.m. CET
(@manoj-kalekar)
Posts: 8
Member
Topic starter
 

Yes,  label

 
Posted : 13/02/2025 10:47 a.m. CET
MarAlz-RW
(@maralz)
Posts: 15
Member
 

Oh, that's what you mean.

One way is described here in an example project in the old help:

The project:
"Statuswert in Nachricht umwandeln" has an example of this.

https://classicdocs.quickhmi.com/help_quickhmi/actual/online/de/example_projects.html

As an advanced tip that you don't have to call a single Events.onVarChanged function for each variable is that you can also use regex for the names: Here is the code adapted from the example project:

 

//Example variables:
ds.state1
ds.state2
ds.state3
ds.state10
ds.mode1
ds.mode2
ds.mode3
ds.mode10
Datenquelle Intern:
Intern.state1
Intern.state2
Intern.state3
Intern.state10
Intern.mode1
Intern.mode2
Intern.mode3
Intern.mode10

 

//Javascript:
//Messages for a state this is either not numeric, or not starting at index 0.
let stateMessages = {
	4711: "Message for state 4711",
	"4712": "Message for state 4712",
	"NO_CONN": "Message for state 'NO_CONN'",
	"ABC123": "Message for state 'ABC123'"
};

function processState(args) {
	//Get the message based on the variables value.
	let message = stateMessages[args.variable.value];
	let _varname = args.variable.name;
	
	//Write the message to the internal variable "stateMessage" that is used in the user interface.
	mainInterface.setVariable("Intern." + _varname, typeof message === "undefined" ? "Invalid state value" : message);
}

//Link variables to the handler functions above.
//This can alternatively done by using the "Scripting" -> "JavaScript" -> "Assignment" page.

Events.onVarChanged(/ds.state\d+|ds.mode\d+/, processState);

 

 
Posted : 14/02/2025 1:35 p.m. CET
(@manoj-kalekar)
Posts: 8
Member
Topic starter
 

Thanks, I will try and let you know

 
Posted : 15/02/2025 6:26 a.m. CET