Dynamic Drop in Content Fragment | Community
Skip to main content
Level 1
February 26, 2026
Question

Dynamic Drop in Content Fragment

  • February 26, 2026
  • 1 reply
  • 11 views

I have 6 tabs in CF with each tab consists of 2 drop downs and 1 text field. Based on the servlet I fetch the tag data on CF load for first time and stored in the 1st dropdown of all the tabs. Now based on my selection of the tag value in the first dropdown. I need to populate the values in the second dropdown and the data for that is again fetched by servlet. (Fragments List using the tag selected in the first dropdown).  Based on the selection of the fragment from second dropdown i need to populate the CF path of that fragment in the path field. 
Now im able to achieve this scenario for the first time. My Script calls the servlet on CF load and fetches the tags data for first dropdown and based on the tag selection i get the fragments list in second dropdown. But when i save CF and reopen the CF, I don't see the selected values from the dropdown but i see the path which is populated from the selection of fragment from second dropdown. But the values are stored in CRXDE.
I have tried with manual options from CF Model. those were selected even after i open the CF again. But dynamic values are causing an issue.
Any suggestions what can be done in order to retain the selection.?

 

    1 reply

    AmitVishwakarma
    Community Advisor
    Community Advisor
    February 26, 2026

    Hi ​@Saiteja-09 ,
    Your servlet populates the dropdowns at editor load.

    • On re‑open, your script rebuilds the dropdown options, but:
      • It does not re‑apply the previously saved value as the selected option
      • The rebuilt options don’t contain an item whose value exactly matches the stored JCR value.
    • The path text field works because you are not overriding its widget, it just shows whatever is in JCR.

    Result: Values are in CRXDE, but the Coral <coral-select> has no matching option marked as selected, so the UI looks empty.

    Assuming you’re in the classic CF editor / Touch UI with a servlet:

    1. Do not replace the field or its name: Keep the original field from the CF model (e.g. ./field1, ./field2). Only change its options, not the underlying input.
    2. Before clearing options, cache the persisted value
      function populateFirstDropdown(selectEl) {
      // 1) Persisted value that CF editor already loaded from JCR
      var storedValue = selectEl.value || "";

      // 2) Fetch options from servlet
      $.getJSON("/bin/myTagsServlet", function(items) {
      // Clear existing options
      selectEl.items.clear(); // Coral API

      // Rebuild options
      items.forEach(function(item) {
      var option = new Coral.Select.Item();
      option.value = item.value; // must match JCR value
      option.content.textContent = item.text;
      selectEl.items.add(option);
      });

      // 3) Re‑apply saved value, if still valid
      if (storedValue) {
      selectEl.value = storedValue; // selects the right option
      }
      });
      }
    3. On CF editor load, run population and re‑select
      $(window).on("foundation-contentloaded", function () {
      $("coral-select.my-first-dd").each(function () {
      populateFirstDropdown(this);

      // If first dropdown already has a value, also repopulate the second
      var stored = this.value;
      if (stored) {
      populateSecondDropdownForTab(this); // use same function as your change handler
      }
      });

      $("coral-select.my-second-dd").each(function () {
      var selectEl = this;
      var storedValue = selectEl.value || "";
      if (storedValue) {
      populateSecondDropdown(selectEl, storedValue); // repopulate + select saved fragment
      }
      });
      });
    4. Second dropdown & path field
      • Use the same pattern: when you refill the second dropdown based on the first dropdown’s value, first read its persisted value and set selectEl.value after adding the new options.
      • The path text field can stay as is; it will automatically show the stored fragment path.
    5. Make sure option value matches JCR
      • Whatever gets stored (e.g. myTagId or /content/dam/.../myFragment) must be exactly the option.value you set when rebuilding, otherwise Coral can’t show it as selected.

    If you’re on AEM as a Cloud Service with the new CF editor:

    The official, future‑proof way to do dynamic dropdowns is via a CF Editor UI Extension, not an old‑style Granite datasource. See the sample custom field implementation: https://experienceleague.adobe.com/en/docs/experience-manager-learn/cloud-service/developing/extensibility/ui/content-fragments/examples/editor-custom-field

    In that setup, as long as your custom field writes a stable string value back to the fragment, the new editor will persist and restore the selection correctly.

     

    Thanks,
    Amit