Form Design, dropdown. How to have value?

Hey folks,
In HTML I can use the following for a dropdown:

Volvo Saab Mercedes Audi

On submit, the database would store the value rather than the text.

I want to pull data from my database and use the value/data in a dropdown list. Pulling the data from the DB is easy, I have that now as a list. But how do I assign the list to a form dropdown and tell bonita that whatever option is selected to use the ‘value’ rather than the ‘text’?

Any examples/pointers?

Many thanks,
Jim

1 Like

Hi.

I think that to do that you can use a HashMap or similar Collection item.
In the HashMap there is a key/value system so you can display only the key list and when you submit the form, you get the value corresponding to the selected key.

To create the Hashmap :

HashMap<String,String> hashmap = new HashMap<String,String>();

// for each Data, you add it in the hashmap
hashmap.put(key,value);

Then in the widget you can get the key list with :

hashmap.keySet();

And when you submit you get the value with :

hashmap.get(selected_key);

I hope this will help you.

Hi Yannick,

That makes sense, so.

  1. Pull data from database in to list then in to hashmap.
  2. Load form with the select as an element using hashmap as the data.
  3. On form submit, set data to hashmap.get(slected_key)

Is that correct, I think it makes sense…

Thank you,
Jim

  1. You can pull directly your data into hashmap in your connector (using the resultSet)
  2. In select widget of your form, you need to use hashmap.keySet() as the data
  3. On form submit, set data to hashmap.get(selected_key)

Hi,

So, when I use the connector to get the data, it does not allow me to use hashmap.
It does allow me to use HashMappedList.

If I create a java.hashmap variable in the connector, once created, it does not show in the list to use in result. But, HashMappedList does. Is that ok to use?

I try it and for me it works.

I create a variable hashMap in my process (java.util.HashMap) :

![http://img4.hostingpics.net/pics/205846createHashMap.png][http://img4.hostingpics.net/pics/205846createHashMap.png]

Then I create my connector and in the last screen of the connector I choose Scripting Mode and I can choose my variable hashMap :

![http://img4.hostingpics.net/pics/938973hashMapinconnector.png][http://img4.hostingpics.net/pics/938973hashMapinconnector.png]
[http://img4.hostingpics.net/pics/205846createHashMap.png]: http://img4.hostingpics.net/pics/205846createHashMap.png
[http://img4.hostingpics.net/pics/938973hashMapinconnector.png]: http://img4.hostingpics.net/pics/938973hashMapinconnector.png

Ahh, ok. Found it.

I can now select myHashMap.

You have typed ‘aScriptThatAddDataInHashMap’…

What would this script look like if I can ask?

Thank you :slight_smile:

It looks like this :

import java.sql.ResultSetMetaData;

HashMap<String,String> hm= new HashMap<String,String>();

resultset.beforeFirst();

while (resultset.next()) {
String key = resultset.getString(‘myKey’);
String value = resultset.getString(‘myValue’);
hm.put(key,value);
}
return hm;

For example in a table ‘CAR’ I have a column ‘name’ and a column ‘description’. I want to have all the car’s names in the list and when I submit the form, I get the description of the car I select.
My sql query is “SELECT * FROM CAR”.
In the script I have :

String key = resultset.getString(‘name’);
String value = resultset.getString(‘description’);

Hello,
This works perfectly. I modified to be integer,string rather than string,string.

I have added select widget to a form and set available data to be the hashmap.

When I run the process, the integer shows in the select box. How can I make only the string show? The value not the key…

Thank you for the help, so close to being perfect :slight_smile:

If you want the string to show in the select box, the string need to be your key.
So in your script you need to create a HashMap<String, Integer>.

This is working, I now have the string in the select. Very good.

Last question on this if you could help me? :slight_smile:

On the output operation, what do I need to use for a script to take the value selected in the dropdown and return the int?

In the output of your select list you need a script that return :
hashmap.get(field_nameOfYourSelectList);

For example, if your select list widget is called “SelectList”, your script will be :
return hashmap.get(field_selectList);

Hi,

My hasmap is called ‘myHashMP’. My select is called ‘myDropdown’ So, I am doing:

retrun myHashMP.get(myDropdown)

This should be returning the id from the value. So I have set the return type to integer. I am selecting a integer variable in the pool ‘theId’ as ‘Output Operation’.

My next form uses ‘theId’ as data in a text so that I can check it, but no data is shown. Any idea?

It seems to me that you should rather use:

return myHashMap.get(field_myDropdown)

Note that widget are referenced as field_ + its name.

Hi, Yes. I have it now as field_
The process does not fail, but I do not end up with the value from the hashmap…

Hi, cracked it.

I needed to convert field to int to bring it back over to the pool as a variable that is an int.

return Integer.parseInt(field_Select1)

Thank you Haris, very helpful. Thank you too Yannick.