Getting a field value from a custom control when there are multiple instances of the custom control

Go To StackoverFlow.com

0

I have an xPage with multiple instances of the same custom control. How do I get to the value of a field on a specific custom control from a button on my xPage.

Normally I would do something like:

ctlName = "radioGroupCMBUAction"; var changeType = getComponent(ctlName).getValue();

If there was only one instance of the control. How would I do this with multiple instances of the control?

2012-04-03 21:09
by Bruce Stemplewski
Bruce, can you add some example code so that we can see how you assign ids to the individual custom controls - Per Henrik Lausten 2012-04-06 06:57
Per, That is my question. To be able to assign a unique id to each control with a unique ID. Folks helped me here with the binding http://stackoverflow.com/questions/9913331/dynamic-data-binding.

But I would rather not dynamically create an ID. I can't believe this is this hard to do. You would think I could just reference the custom control id to get to the individual controls - Bruce Stemplewski 2012-04-06 11:39



0

Without the code I'm not sure if .getValue() will work as in order for this to happen it will have to pull out a field where as a custom control could have a repeat inside it etc.

Assuming that it will work if each of the customControl instances have an id that is unique from each other it should work fine as each instance should have its own vriables inside it.

Other ideas however could be to pass in a string and use this string as the name of a scoped variable inside the custom control that can be pulled out from anywhere. I've done this before where I created a property on the custom control for a String and inside the custom control use

viewScope[compositeData.customProperty] = value.

then outside the context of the custom control I am aware of the string I passed in so I would be able to pull this value back out. Passing it in means multiple instances won't over write each other.

Or you could write the field in he custom control to a document, either to get it out or as the end solution to your problem of saving it.

2012-04-04 07:42
by Simon McLoughlin
How do you give a unique Id to each control? Can and id be computed? I did notice that the control seems to have an id with the cc name associated with it. Example MyCC1:MyField1. I tried using that in getComponent but it did not work. I would really like to get to the field itself because if I ever get it working I want to be able to go to a field if a validation fails - Bruce Stemplewski 2012-04-04 12:14
If you define the ID for a control this will ALWAYS be converted to a unique ID generated by the xpages environment. ( something like view:controlid:fieldid - jjtbsomhorst 2012-04-04 12:29
yes as jjtbsomhorst said you don't have to create a full unique id. On the control simply add an id property and XPages will add the rest.

eg.

<inputText id="sample">

then this will be turned into something like view:panel_34:control:sample

you can get at it by calling getComponent("sample");

If you need to interact with the field, my first thought is maybe more of this code should be inside the custom control its self, failing that I would do what I mentioned with the scoped variable and have that hold the id of the field so you could do getComponent(viewScope["control1"]) etc - Simon McLoughlin 2012-04-04 15:24

or if you gave every control inside the custom control an id you could probably work out the id to the field by getting the custom control id and adding to it, such customControlId+"mainDiv:secondDiv:quanityField" etc.

this can be very messy though, generally custom controls are best when there self containing, try putting validation inside it and anything else you need to make it so it can just be dropped on a page and pointed to a domino doc field or something like that, will be a lot easie - Simon McLoughlin 2012-04-04 15:28



0

Not sure if this can help you..? A while back, I wrote a tip regarding "private" scoped variables:

http://dontpanic82.blogspot.com/2010/03/xpages-tip-regarding-private-scoped.html

2012-04-04 13:38
by Tommy Valand
Yes thanks, I realize I can use scope variables. What I want to do is get to the control itself so I can set focus (if I ever get that working) - Bruce Stemplewski 2012-04-04 15:46