Change Script on Dropdown Value

IA Forum,
I have a change script on a Dropdown. Based on the value of the Dropdown (7, '7A' or 'All') the Perspective page will display a different Table. This is working as intended.

I also have a Button on the same page that triggers a Report Preview popup. I want the Report Preview to change based on the Dropdown Value. It currently pulls the 'All" report. If '7' is selected in the dropdown, then I want to Preview the '7' report. Likewise for the '7A' report.

How do i incorporate the existing change script to include the configured event on the Preview Report Button?

Here is my change script:

def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	if currentValue.value == 7:
		path = "Refrigeration/ChillerLog 7"
		data = system.db.runNamedQuery(path)
		self.getSibling("ChillerWOLog").props.data = data 
		
	elif currentValue.value == '7A':
		path = "Refrigeration/ChillerLog 7A"
		data = system.db.runNamedQuery(path)
		self.getSibling("ChillerWOLog").props.data = data 
		
	else:
		path = "Refrigeration/ChillerLog"
		data = system.db.runNamedQuery(path)
		self.getSibling("ChillerWOLog").props.data = data type or paste code here

OR,
Should three different buttons be configured with Visibility based on the value of the dropdown?

Make a custom property on the root container of the view, or on the view itself called selectedReport. Bidirectionally bind this custom property to the selectedValue of the dropdown.

You can then have a single button that has an if else statement check the value of the custom property and then open the appropriate popup

Create a custom property on the view's root and make a bidirectional binding on the dropdown value property to the custom prop. Then examine that in your script. (Ryan posted as I was typing.)

You can refactor your script to:

def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	if currentValue.value == 7:
		path = "Refrigeration/ChillerLog 7"
	elif currentValue.value == '7A':
		path = "Refrigeration/ChillerLog 7A"
	else:
		path = "Refrigeration/ChillerLog"

	data = system.db.runNamedQuery(path)
	self.getSibling("ChillerWOLog").props.data = data 

@ryan.white & @Transistor ,
I will give that a try. Sounds legitimate, but now it comes down to my ability.

@ryan.white ,
Where does the if else statement get scripted?

My current Configured Event is "onActionPerformed" and the "Organize Actions" runs the "Popup" of the report.

Should I add an Organize Action of a Script and make it first in the list with the if else statement?

Something like this?

def runAction(self, event):
	self.getSibling("Dropdown").props.value
	if currentValue.value == 7:
			path = "Refrigeration/ChillerLog 7"
	elif currentValue.value == '7A':
			path = "Refrigeration/ChillerLog 7A"
	else:
			path = "Refrigeration/ChillerLog"
	
	data = system.db.runNamedQuery(path)
	self.getSibling("ChillerWOLog").props.data = data

Order here does nothing, as the list execution is mostly asynchronous.

Are you trying to have the button fetch the data and open the popup? Or is the data already fetched when the dropdown changes its value?

You can script the opening of a popup via system.perspective.openPopup, so you can omit the Popup action in the action organizer.

If you need the button to only open a popup, then a script along the lines of

def runAction(self, event):
	selectedReport = self.getSibling("Dropdown").props.value
	if selectedReport == 7:
		path = "Path to report 7 popup"
	elif selectedReport == '7A':
		path = "Path to report 7A popup"
	else:
		path = "Path to overall report popup"
	
	system.perspective.openPopup("ReportPreview", path)

should be enough

If that's working, that's good.

There is a minor problem with referencing the self.getSibling("Dropdown").props.value in your script. If you ever move either the dropdown or the button, rename it, or wrap either in a container then the script will break. Using a custom property on the view to link the two items together (as I suggested above) makes the application a lot less 'brittle' as both components can read and write to it and its path will always remain the same.

1 Like

The data is already fetched since there are three different reports that can popup. So, I will try the script you sent and reply back on how it goes.

I would approach this a bit differently.

I would convert the dropdown's values to objects so that you can return multiple values for each selection. Then, you just return the path that you want as appropriate, and your script can be more generic. Something like:

def valueChanged(self,previousValue, currentValue, origin, missedEvents):
    data = system.db.runNamedQuery(currentValue.value['queryPath'])
    system.perspective.openPopup('ReportPreview', currentValue.value['popupPath'],{'data' = data})

This way, if you need to add an additional report in the future you only need to put the pertinent information in the dropdown list.

3 Likes

Here is how I was able to resolve the issue.

def runAction(self, event):
	dropDown = self.getSibling("Dropdown").props.value
	if dropDown == 7: 
		
		viewPath= "Page/Refrigeration/ChillReport7"
	elif dropDown == '7A':
		viewPath= "Page/Refrigeration/ChillReport7A"
	else: 
		viewPath = "Page/Refrigeration/ChillReport"
	
	system.perspective.openPopup(id="ChillReport", view = viewPath)

I'd get rid of the value change script entirely.
As @lrose said, configure your dropdown's values to contain the data you need.
Then use the dropdown's onActionPerformed event to open the popup based on the current value (which is accessible easily from the event).