Hi @omarkh3,
To implement cascading dropdowns in Touch UI dialogs, you can use the Granite UI datasource approach with JavaScript listeners (using coral-change event) and a servlet to populate the dependent dropdown dynamically.
1. Define your dialog with dynamic state dropdown
Create your cq:dialog structure like below:
<country
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/select"
fieldLabel="Country"
name="./country"
id="countrySelect">
<items jcr:primaryType="nt:unstructured">
<us
jcr:primaryType="nt:unstructured"
text="USA"
value="usa"/>
<ca
jcr:primaryType="nt:unstructured"
text="Canada"
value="canada"/>
</items>
</country>
<state
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/select"
fieldLabel="State"
name="./state"
datasource="/bin/getstates"
id="stateSelect"/>
2. Create a servlet to return state options
Java servlet at /bin/getstates: (Just for your reference)
@SlingServlet(paths = "/bin/getstates", methods = "GET", extensions = "json")
public class StateDropdownServlet extends SlingAllMethodsServlet {
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
String country = request.getParameter("country");
List<JsonObject> states = new ArrayList<>();
if ("usa".equals(country)) {
states.add(new JsonObject().put("text", "California").put("value", "ca"));
states.add(new JsonObject().put("text", "Texas").put("value", "tx"));
} else if ("canada".equals(country)) {
states.add(new JsonObject().put("text", "Ontario").put("value", "on"));
states.add(new JsonObject().put("text", "Quebec").put("value", "qc"));
}
response.setContentType("application/json");
response.getWriter().write(states.toString());
}
}
3. Add a clientlib to handle dropdown change
(function(document, $) {
$(document).on("change", "#countrySelect", function() {
const selectedCountry = $(this).val();
const stateSelect = $("#stateSelect");
$.getJSON("/bin/getstates?country=" + selectedCountry, function(data) {
stateSelect.empty();
$.each(data, function(i, item) {
stateSelect.append(
$("<coral-select-item>").val(item.value).text(item.text)
);
});
});
});
})(document, Granite.$);
Include this in a clientlib (category: cq.authoring.dialog) for your component.
Hope that helps!