Autoselect value in dropdown if only one value present

25 views
Skip to first unread message

MANISH

unread,
Oct 30, 2019, 11:37:03 PM10/30/19
to Orbeon Forms
Hi Alex - I have a static dropdown whose values are populated by a service and action. If the drop down has got only one item it has to auto select that item and let the user select if the drop down has got more than one items returned from the service.

https://imgur.com/wkdXFCq - This works fine to set the first item returned as the default value.

For my scenario, In the action in addition to setting the "Selection Control Items", I set the "Control Values"  using the xpath "if (count(//us-code)=1) then /countries/country[1]/us-code else ()" .

This worked fine in 2018.3. In 2019.1 the static drop down value is shown empty when rendered to a pdf.


Thanks
Manish

Alessandro Vernet

unread,
Oct 31, 2019, 4:58:46 PM10/31/19
to orb...@googlegroups.com
Hi Manish,

I am a bit confused, as the Actions Editor dialog / Service Response Actions
tab doesn't look as in your screenshot. In 2019.1, it looks as follows:
service-resource-actions.png
<http://discuss.orbeon.com/file/t119778/service-resource-actions.png> .
Also, things work fine for me in that case. For reference, here is the
source of the test form I created: form.xml
<http://discuss.orbeon.com/file/t119778/form.xml> . Did maybe something go
wrong with your update of Orbeon Forms, and you still have some older files
in place?

‑Alex

-----
--
Follow Orbeon on Twitter: @orbeon
Follow me on Twitter: @avernet
--
Sent from: http://discuss.orbeon.com/

MANISH B

unread,
Oct 31, 2019, 5:29:57 PM10/31/19
to orb...@googlegroups.com
Hi Alex - I used an old snapshot which was on 2018.3

Was I using the right logic? Why is the dropdown value empty in that case when rendered to pdf? 

--
You received this message because you are subscribed to the Google Groups "Orbeon Forms" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orbeon+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/orbeon/1572555549382-0.post%40n4.nabble.com.

Alessandro Vernet

unread,
Oct 31, 2019, 6:56:00 PM10/31/19
to orb...@googlegroups.com
Hi Manish,

Got it about the dialog. About the value missing from the PDF, is this with
the automatic PDF? I am also not seeing this with 2019.1 (see link below). I
am missing something? Or maybe you could create a minimal example that
reproduces the problem, and attach it to your response?

automatic-pdf.pdf <http://discuss.orbeon.com/file/t119778/automatic-pdf.pdf>

MANISH

unread,
Nov 1, 2019, 2:45:04 AM11/1/19
to Orbeon Forms
Hi Alex

Attached the form definition. 

https://restcountries.eu/rest/v2/name/{name}  - - this is the API I used. 

Type 'America' in the first field and select any country from the dropdown. You can see the value is not passed when rendered to a pdf. I noticed this happens only when we select 'appears or changes value' (attached image) control action and works fine when 'changes its value' is selected.


Thanks
Manish
Dropdown test.txt
dropdown.PNG
test - 0f4a9cc7c83898f9.pdf

Alessandro Vernet

unread,
Nov 4, 2019, 1:15:21 PM11/4/19
to orb...@googlegroups.com
Hi Manish,

I reproduce the issue, but it isn't related to the PDF, and even happens if
you select a country border, save, and reload the page: then the Country
Border dropdown shows empty. This is caused by your action, and specifically
the "Set Control Value" expression. There, you essentially reset the field
if the dropdown has more than 1 item… hence the field showing empty when you
reload the page or generate the PDF. In that expression, instead of `()`, I
think you want to use something like `fr:control-string-value('control-2')`,
to keep the current value.

action.png <http://discuss.orbeon.com/file/t119778/action.png>

You'll let me know if this works for you!

MANISH

unread,
Nov 4, 2019, 8:06:18 PM11/4/19
to Orbeon Forms
Thanks Alex - this works. 

Is there a way to filter only distinct values in the dropdown even when the service is returning duplicate values.


Cheers
Manish

Alessandro Vernet

unread,
Nov 5, 2019, 2:10:10 PM11/5/19
to orb...@googlegroups.com
Hi Manish,

I'm glad not resetting the value of the control indeed works for you as
well. You can leverage `distinct-values()` for this. Try changing your
choices XPath to something like:

distinct-values(/json/*/name)

And then set both the name and value to `.` (just a dot). You'll let me know

MANISH

unread,
Nov 6, 2019, 3:31:33 AM11/6/19
to Orbeon Forms
Thanks Alex - works perfect! 

It now automatically populates the value of the dropdown if the service is returning only one distinct value. How can I make this dropdown read only in this case and not read only if the drop down has got more than one items returned from the service?

Cheers
Manish

Alessandro Vernet

unread,
Nov 6, 2019, 5:57:10 PM11/6/19
to orb...@googlegroups.com
Hi Manish,

I'm glad the `distinct-values()` works. But I'm not sure what you mean by
"make this dropdown read only in this case and not read only if the drop
down has got more than one items returned from the service". Could clarify
what you meant by that, maybe with an example?

MANISH

unread,
Nov 6, 2019, 7:55:30 PM11/6/19
to Orbeon Forms
Hi Alex - Please use the same example attached before.

 If Country name is 'America' the dropdown control has got 3 choices and let's the user to select one. 

In another case where the country name is 'Russia' the dropdown has only one choice which is autofilled. I want the dropdown read only in this scenario as the user does not have to select any choice as it is autofilled with the logic you mentioned above.

Thanks

Alessandro Vernet

unread,
Nov 8, 2019, 2:16:33 PM11/8/19
to orb...@googlegroups.com
Hi Manish,

Got it! You can use an expression as follows, in the Control Settings,
Formulas, Read-only:

let
$itemsetid := @fr:itemsetid,
$itemset := /form/fr:metadata/itemset[@id = $itemsetid]
return
count($itemset/choices/item) = 1

To come up with this type of expressions, you can use the XForms Inspector
to check what the form data looks like at runtime. You enable the XForms
Inspector by setting the `oxf.epilogue.xforms.inspector` property to `true`,
and then see the form data in the `fr-form-model` model / `fr-form-instance`
instance.

And you'll let me know if this works for you,

MANISH

unread,
Nov 12, 2019, 3:49:54 AM11/12/19
to Orbeon Forms
Works perfect - thanks

Alessandro Vernet

unread,
Nov 12, 2019, 1:14:59 PM11/12/19
to orb...@googlegroups.com
Excellent Manish, and thank you for the update!

MANISH

unread,
Nov 13, 2019, 7:17:16 PM11/13/19
to Orbeon Forms
Hi Alex - Using this would keep the label and value same. What if I want to use 'countryname' as label and 'currency' as value?

Can I use something like

/json/*/

And then set both the name to `distinct-values(countryname)` and value to `distinct-values(currency)` instead as i would like to display a different

Alessandro Vernet

unread,
Nov 13, 2019, 7:56:05 PM11/13/19
to orb...@googlegroups.com
Hi Manish,

So you want users to select a country, and store the currency. But if you
have 2 countries with the same currency, next time you load the data, what
country should you show? If you stored the currency, you don't know anymore
which country was selected. So it's maybe better to store the country name,
and have the currency calculated in another field (and maybe hidden from
users, if you just need it in the data). Would that make sense?
Message has been deleted

MANISH

unread,
Nov 13, 2019, 10:36:50 PM11/13/19
to Orbeon Forms
Hi Alex - You're right but that is just an example. I have a use case where I need store something like

distinct-values(string-join((//countrycode,/json/*/countryname[not(contains(text(),'null'))]),' - ') and use (.) for both label and value. So that I can extract the country code by substring and use it to call some other API again.

Assuming I have an API returning an xml:

<countryname>Brazil</countryname>
<countrycode>111</countrycode>
<countryname>null-Brazil</countryname>
<countrycode>1123</countrycode>
<countryname>America</countryname>
<countrycode>100</countrycode>
<countryname>Brazil</countryname>
<countrycode>111</countrycode>

I must get the drop-down values as 

111 - Brazil
100 - America


and not have the duplicate values or those which contains text 'null' . . What is wrong that I'm doing here

Alessandro Vernet

unread,
Nov 14, 2019, 7:11:30 PM11/14/19
to orb...@googlegroups.com
Hi Manish,

Could you attach the actual JSON returned by your service? You're saying
that you'd like to have "111 - Brazil", "100 - America", etc shown in the
dropdown. And what would you like to be stored in the data? The same thing?

I'm asking all those details, so I can just create, test, and share with you
an example that does this.

MANISH

unread,
Nov 14, 2019, 7:34:08 PM11/14/19
to Orbeon Forms
Hi Alex - This is the snippet of the JSON

{
"success":true,
"message":null,
"staff":
{"eid":"1234",
"firstName":"John",
"lastName":"Blokes",
"positions":
[{"description":"Director - BU",
"Id":"111"},
{"description":"null - Other Services",
"Id":"222"},
{"description":"Director",
"Id":"111"},
{"description":"null- Commercial",
"Id":"333"}]
}
}

On trigger, the dropdown must have the values as "Id - description". I need all the distinct values of description not containing 'null'. Date should be same as dropdown label.

Thanks
Manish

MANISH

unread,
Nov 14, 2019, 7:43:11 PM11/14/19
to Orbeon Forms
The dropdown result must be

111 - Director - BU

Alessandro Vernet

unread,
Nov 15, 2019, 7:50:14 PM11/15/19
to orb...@googlegroups.com
Hi Manish,

Would the following work?

Choices: `/json/staff/positions/_[not(starts-with(description,
'null'))]/concat(Id, ' - ', description)`
Label: `.`
Value: `.`

With the above JSON, this will give you:

111 - Director - BU
111 - Director

Alessandro Vernet

unread,
Nov 22, 2019, 12:27:03 PM11/22/19
to orb...@googlegroups.com
Hi Manish,

Did you get a chance to try the XPath expressions in my previous message to
populate the dropdown from the result of your service? If so, is it indeed
inline with what you were looking for?

MANISH

unread,
Nov 25, 2019, 2:59:34 AM11/25/19
to Orbeon Forms
Thanks Alex - it works perfect!!

Alessandro Vernet

unread,
Nov 25, 2019, 12:16:31 PM11/25/19
to orb...@googlegroups.com
Perfect Manish, and thank you for the confirmation!
Reply all
Reply to author
Forward
0 new messages