icfnext / cq-component-maven-plugin

Other
22 stars 35 forks source link

Load dropdown dynamically on AEM Dialog #95

Open touseef4pk opened 4 months ago

touseef4pk commented 4 months ago

I am using citytechinc cq complonent plugin to create components in AEM using Java classes. When I use following dropdown with hardcoded option, it works fine.

      `@ValueMapValue
       private String stores;

@DialogField(fieldLabel = "Stores", ranking = 10) @Selection(type = Selection.SELECT, options = {
        @Option(text = "Store1", value = "556"),
        @Option(text = "Store2 n Barrels", value = "475"),
        @Option(text = "Store3 Hive", value = "2547"),
})
public String getStores() {
    return stores;
}`

But I use following dropdown to load it dynamically using a servlet. The dropdown remains empty on the AEM dialog.

`@ValueMapValue
private String stores;

@DialogField(fieldLabel = "Stores", ranking = 10)
@Selection(type = Selection.SELECT, dataSource = "/bin/store/storeoptions.json")
public String getStores() {
    return stores;
}`

Following is my servlet

  `@Component(service = Servlet.class,
          property = {
                  "sling.servlet.methods=" + HttpConstants.METHOD_GET,
                  "sling.servlet.paths=" + "/bin/store/storeoptions"
          })
  public class StoreOptionsServlet extends SlingAllMethodsServlet {

      @Override
      protected void doGet(final SlingHttpServletRequest req,
                           final SlingHttpServletResponse resp) throws IOException {
          resp.setContentType("application/json");

          List<OptionItem> options = new ArrayList<>();
          options.add(new OptionItem("25365", "Store1"));
          options.add(new OptionItem("16726", "Store2"));
          options.add(new OptionItem("31280", "Store3"));
          // Add more options as needed

          resp.getWriter().write(new Gson().toJson(options));
      }
     }`

Any help is appriciated.

touseef4pk commented 3 months ago

Ok I have found its solution. I need to use datasource and set as attribute in request.