How to get selected instance name in flex repeater

Hi guys,

Any idea how to get the selected instance name in Flex repeater?

image

Like when I select Instance[25] then get the role name in the custom parameter.

Thanks,
Priyanka

What are the array items 24 - 28? Are they flex repeater instance view parameters? (I'm confused because when I create a flex repeater each instance has instanceStyle and instancePosition properties and yours does not. Did you create the array by script?)

I have created a flex repeater instance using a named query. Named query return format in JSON.

You don't need either of these, I usually delete them if I don't use them

These params will get pass automatically into your embedded Views as parameters. So if you have role and selected params in your embedded View, then the values will appear in them

If you want the main view to know which repeater view has been selected then I would use system.perspective.sendMessage in an onClick event on the root of the embedded view.

def runAction(self, event):
	role = self.view.params.role
	system.perspective.sendMessage("repeaterViewClicked", {"role": role}, scope = "page")

Then use a message handler on the component in the main view that needs the information. Be sure to set the listen scope to "Page".

1 Like

Resoved using the script below. I have binding the script below in custom property.

image

May I suggest a slight rework of this script ?
Because frankly this is a weird and convoluted way of doing it.

return next(instance.role for instance in value if instance.get(selected))
1 Like

For some reason this made me remember the words of an old mentor. He would say: "Where were you when the page was blank!"

:rofl::rofl::rofl:

I didn't have much time earlier, but now I'd like to expand a bit.

There's a slight difference in functionality between both our scripts. It stems from an assumption I made, which is that only one instance should be selected at a time.
So, my script returns the role associated with the first selected instance encountered.
Yours will return the last one.
Which is equivalent if only one is selected.
If you DO want the last one, then you can use the script I provided, with one small change: reverse the order of value:

return next(instance.role for instance in reverse(value) if instance.get(selected))

Now, why do I think your script is weird and convoluted ?
You want to return ONE thing, but you're building a list. A list from which you pick the last element, every time you append something.
There are a few things that are really, really unnecessary about this.

  1. You're appending an element to a list, then picking the last element from the list.
    => The list is completely useless here. why not just do val = instance.role and get rid of the list ?
  2. You're returning the last element of a list, but picking it every time the list is modified
    => You could take this step out of the loop, and use return rolelist[-1] instead
  3. Either there's only one instance selected, or you want to return the role associated with the last one. In both cases, there's a better way to do it:
    => If there's only one, why build a list ? You could just return the role when you find the selected instance: remove the list and replace the append with return instance.role
    => if you want the last one, start from the end of the instances list and return the first one you find
2 Likes

While you may have "resolved" the issue with the post currently marked as a solution, this post is the correct way to manage this: How to get selected instance name in flex repeater - #5 by Transistor

With the code provided and a configured Message Handler listening for repeaterViewClicked you'll be able to configure a script to execute any time a user clicks an instance.

The code provided in this post is NOT a good way to go about this as you'll always get the instance with the highest index, even if it was not the most recent instance selected.

2 Likes