I need your help with this one :)
The main idea is pretty simple, I have a first dropdown, and I want to filter the values in the second dropdown depending on the first one.
I can't modify the datamodel.
My two sets of value are sent trough a request dispatcher :
[...]
List<ConfigSousType> listConfigSousType = new ArrayList();
List<ConfigType> listConfigType = new ArrayList();
[...]
String querySousType = "SELECT distinct colonne, idValeur FROM ThanactConfigType";
String queryType = "SELECT * FROM ThanactConfigType";
[...]
request.setAttribute("listConfigSousType",listConfigSousType);
request.setAttribute("listConfigType",listConfigType);
String nextJSP = "/addConfigSousType.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP)
[...]
The data model is this one :
First table Type: name, idType, value
Second table SubType : name, idType, subTypeName, idSubType
Example:
Type table
ColorCategory, 1, TheBlueColors
ColorCategory, 2, TheRedColors
FoodCategory, 1, TheTastyFood
SubType Table
ColorCategory, 1, DeepBlue, 1
ColorCategory, 1, LightBlue, 2
ColorCategory, 2, Crimson, 1
ColorCategory, 2, Scarlet, 2
FoodCategory, 1, Pizza
I'm creating the admin part allowing to add a new line in the subType Table.
I want a first dropdown with 'ColorCategory', 'FoodCategory'
Then my second dropdown on, the idType should be filtered depending on the first one.
I select "ColorCategory" on the first dropdown, the second one allow the values "TheBlueColors", "TheRedColors"
On my example
- listConfigType contains the whole table (they will be small tables, <100 lines)
- listConfigSousType contains "ColorCategory , 1", "ColorCategory , 2", "FoodCategory, 1"
In my JSP I get them like that:
<select id ='name' required name='name'><br><br>
<c:forEach items="${listConfigSousType}" var="configSousType">
<option value="${configSousType.name}">${configSousType.name}</option>
</c:forEach>
</select>
=> Two issues:
- For the first dropdown, I will get ColorCategory twice, I need a distinct someway
- How should I populate the second dropdown ?
I select ColorCategory on the first dropdown, the second should offer, "TheBlueColors", "TheRedColors"
I think I may need a javascript part with the onChange function but I don't know what should be inside.
To diplay the value of the idType on my display list I use:
<label for='value'>Valeur </label>
<input id ='value' type='text' name='value'
<c:forEach items="${listConfigType}" var="configType">
<c:if test="${configType.name == configSousType.name && configType.idType == configSousType.idType}">
value="${configType.value}"><br><br>
</c:if>
</c:forEach>
Thank you if you read this whole post, I hope you'll be able to help me :)