Instances of checkboxes failing on "selected" property

I have a Perspective view where the user can select from CheckBox instances in a flex repeater. After selecting from the list a button will submit the list of names that were selected. The code is below:

	# step through the rows
	for instance in Instances:
		logger.info("Instance " + str(instance.text))	
		if instance.selected:
			Names.append(str(instance.text))
			logger.info("Name " + str(instance.text))				
	

If I choose as follows:

i get an error at "if instance.selected:" stating that there is no "selected" attribute:

If I choose as follows:

the code executes as expected, sees that the "selected" attribute on the Jill instance exists but is false, and there is no error:

How do I fix this error?

Are you iterating over the instances prop in the flex repeater? Then your syntax would need to be

if instance['selected']:

The perspective object gets a wrapper that makes it act sort of like a python dictionary, so you have to use python's syntax for accessing keys in a dict. (ie, dict['key']).

Double check the capitalization of your key/attribute(selected vs Selected), and check that selected actually exists in each instance. If you don't add that key when generating the instances to begin with, it will not be present.

As a one-liner:

names = [instance['text'] for instance in instances if instance['selected']]

Side note, you can do

logger.infof("Instance %s", str(instance['text']))

to take advantage of java's string formatting.

Edit: I'm so used to sanitizing perspective data objects I forgot you can do object.attribute on the wrapper class

I don't believe I am doing anything different in adding the last instance to the flex repeater than I am doing to the preceding instances. They should all have ".selected".

The CheckBox has params "selected" and "text"

They are bound to the Checkbox components of the same name:

The instances of the CheckBox view show up and should all have the same params:

Observations after a quick scan:
Your third screengrab (caption your images and give them numbers - it makes it much easier to talk about them) shows that each instances member has a text property but no selected property. That would explain the 'there is no "selected" attribute' error message.

Also, you've cropped your first image too much on the right so we can't see if you've set the PARAMS as Input/Output type (set by cycling through the arrow options on the right edge of the Perspective Property Editor).

1 Like

That does not explain why the script sees ".selected" for all instances except the last one. I don't believe it is required to create parameters in the instances when the object in the flex repeater already has them. The "text" parameters shown in the instances are there because they are being set by binding to a script that pulls the values from a database.

Here is the image showing the parameter types:

Screenshot 2024-12-18 at 9.27.28 AM

Make sure the "Persistent" option of the parameter is checked.

I was wrong; adding "selected" in the binding script to make it show up as a parameter in instances fixed it.

2 Likes

Good work. I couldn't think how instances.0.selected could be read if it wasn't in each of the list objects.