I have two windows in my project, Window A and Window B. In Window A, I have a button that opens Window B and passes a parameter value to it. In Window B, I have a Template Repeater (Template Repeater 1) with a text field inside it, and I want to display the parameter value in the text field.
However, I encountered an issue where the first time I click the button in Window A to open Window B, I get an "IndexError: index out of range: 0" error. To debug this issue, I added the following script in the WindowOpened event of Window B:
I observed that when I first open Window B, the length of templates is zero, which is likely causing the IndexError. However, when I close Window B and open it again, the length of templates is now 1, and the parameter is displayed correctly in the text field.
I would like to get some guidance on how to resolve this issue, so the Template Repeater loads the templates correctly when Window B is opened for the first time.
To troubleshoot it since the issue doesn't seems obvious at first sight to me, i woud try and add a print statement to the script that opens Window B that print outs the parameter being passed to see if it's actually being passed. Then i would also try and add a label component or something similar (depending on what is the paremeter being passed) on Window B and bind it's text to the parameter that should've been received to see if actually shows up on the first time you open the window.
If you dedice to follow these steps, please show us the outcome.
Sounds like a design flaw. You are pushing the parameter to the template. Instead, supply the parameter in the template repeater's dataset, allowing the template to pull the value as it initializes.
In order to debug this issue and eliminate other interferences, I created two new windows: windowA and windowB. WindowA contains a button, and clicking it will close WindowA and open windowB. Inside windowB, there is a label and a Template Repeater, which also contains a label. I need to set the initial text of these two labels to test_initial_String = "Set default value success".
When I click the button in windowA for the first time to open windowB, I receive an error.
WindowB opened script
test_initial_String = "Set default value success"
system.gui.getParentWindow(event).getComponentForPath('Root Container.Label').text = test_initial_String
repeater = system.gui.getParentWindow(event).getComponentForPath('Root Container.Template Repeater')
#Store the list of templates in another variable
templateList = repeater.getLoadedTemplates()
print "============================================================= "
print "len(templateList)= " + str(len(templateList))
#Iterate through the list
for template in templateList:
#find a component named "Label" in the instance,
#and print the value of the text property
template.getComponent('Label').text = test_initial_String
print "============================================================= "
print "template.getComponent('Label').text : " + template.getComponent('Label').text
print "templateList[0] ================================================"
print templateList[0]
=============================================================
len(templateList)= 0
templateList[0] ================================================
10:55:02.466 [AWT-EventQueue-0] ERROR com.inductiveautomation.ignition.client.util.gui.ErrorUtil - <HTML>Error executing script for event: <code><b>visionWindowOpened</b></code><BR>on component: <code><b>TestWindowB</b></code>.
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
File "<event:visionWindowOpened>", line 26, in <module>
IndexError: index out of range: 0
at org.python.core.Py.IndexError(Py.java:251)
at org.python.core.SequenceIndexDelegate.checkIdxAndGetItem(SequenceIndexDelegate.java:63)
at org.python.core.PySequence.seq___getitem__(PySequence.java:378)
at org.python.core.PySequence.__getitem__(PySequence.java:374)
at org.python.pycode._pyx11.f$0(<event:visionWindowOpened>:26)
at org.python.pycode._pyx11.call_function(<event:visionWindowOpened>)
at org.python.core.PyTableCode.call(PyTableCode.java:173)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1687)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:800)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.runActions(ActionAdapter.java:206)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.invoke(ActionAdapter.java:299)
at com.inductiveautomation.factorypmi.application.binding.action.RelayInvocationHandler.invoke(RelayInvocationHandler.java:57)
at com.sun.proxy.$Proxy42.visionWindowOpened(Unknown Source)
at com.inductiveautomation.factorypmi.application.FPMIWindow.fireVisionWindowOpened(FPMIWindow.java:719)
at com.inductiveautomation.factorypmi.application.VisionDesktop$RuntimeWindowOpener.openWindow(VisionDesktop.java:549)
at com.inductiveautomation.factorypmi.application.VisionDesktop.openWindow(VisionDesktop.java:192)
at com.inductiveautomation.factorypmi.application.script.builtin.NavUtilities.lambda$openWindowImpl$5(NavUtilities.java:393)
at com.inductiveautomation.factorypmi.application.script.builtin.WindowUtilities.doGUIAction(WindowUtilities.java:808)
at com.inductiveautomation.factorypmi.application.script.builtin.NavUtilities.openWindowImpl(NavUtilities.java:336)
at com.inductiveautomation.factorypmi.application.script.builtin.NavUtilities.openWindow(NavUtilities.java:326)
at com.inductiveautomation.factorypmi.application.script.builtin.NavUtilitiesDispatcher.openWindow(NavUtilitiesDispatcher.java:143)
at jdk.internal.reflect.GeneratedMethodAccessor39.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:190)
at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction.__call__(ScriptManager.java:549)
at org.python.core.PyObject.__call__(PyObject.java:461)
at org.python.core.PyObject.__call__(PyObject.java:465)
at org.python.pycode._pyx13.f$0(<event:actionPerformed>:7)
at org.python.pycode._pyx13.call_function(<event:actionPerformed>)
at org.python.core.PyTableCode.call(PyTableCode.java:173)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1687)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:800)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.runActions(ActionAdapter.java:206)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.invoke(ActionAdapter.java:299)
at com.inductiveautomation.factorypmi.application.binding.action.RelayInvocationHandler.invoke(RelayInvocationHandler.java:57)
at com.sun.proxy.$Proxy40.actionPerformed(Unknown Source)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.desktop/java.awt.Component.processMouseEvent(Unknown Source)
at java.desktop/javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.desktop/java.awt.Component.processEvent(Unknown Source)
at java.desktop/java.awt.Container.processEvent(Unknown Source)
at java.desktop/java.awt.Component.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Window.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.desktop/java.awt.EventQueue$5.run(Unknown Source)
at java.desktop/java.awt.EventQueue$5.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.python.core.PyException: IndexError: index out of range: 0
... 74 common frames omitted
From message I print, it shows , len(templateList)= 0
And because of this, it does not execute, the loop , for template in templateList
Which event handler is it running under exactly?
If it's "visionWindowOpened" maybe this is messing things up?
What happens when you copy it to "internalFrameActivated"?
Or even a clicked event on the template repeater(with some slight modifications to the paths) so you can be sure it's actually retrieving the data you need.
I've tried what i said and it doesn't seem to work on any of the window opened scripts. It works if you place a similar script on a click event of any component of the window, probably because everything has been loaded by that point.
What seems to be happening on your first post is that when you first open your window and the script fires, the templates aren't loaded yet, but when you close it, the window gets cached and the second time it opens, the templates are already loaded so your script works as intended.
So, what @pturmel seems not only the best but the only way, which i believe would mean that you must pass your string parameter from Window A to B in the script, something like this:
#Assuming WindowB has a custom parameter in it's root container called "String2Pass"
window = system.nav.openWindow('Window/Main WindowB', {'String2Pass' : 'String sent from A to B'})
system.nav.centerWindow(window)
Then, along with the custom parameter on the root container, Window B will need a template repeater with it's "repeatBehavior" property set to dataset and it's templateParams property's dataset with a column that will correspond to the template property that will be passed to the templates, add a row and bind it's value to the root container's custom parameter, the dataset and it's binding will look something like this:
Then, don't forget that your template needs a Template Parameter with a name that matches the name of the column on the template repeater's dataset, like this:
This will work as a demonstration only, so you'll have to change it's properties to match your project if you decide to use this method, but you can have more than one template property be dynamic and if you need a dynamic number of rows for the template repeater, you'll be better off removing the cell update binding and building yourself the rows of the dataset in a script.