Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to set datasource to ValueMapResourceWrapper.

Avatar

Level 2

I want use ValueMapResourceWrapper to create a Select field. How to set options by DataSource.

public class TestModel extends WCMUsePojo {
    @Override
    public void activate() throws Exception {
      ValueMapResourceWrapper wrapper = new ValueMapResourceWrapper(getResource(), "granite/ui/components/coral/foundation/form/select");
      wrapperProperties = wrapper.adaptTo(ValueMap.class);
      wrapperProperties.put("name", "fieldName");
      wrapperProperties.put("fieldLabel", "Label");

      List<Resource> itemValues = new ArrayList<>();
      for (a loop) {
        ValueMap vm = new ValueMapDecorator(new HashMap<String, Object>());
        vm.put("text", "text1");
        vm.put("value", "value1");
      }
      DataSource ds = new SimpleDataSource(itemValues.iterator());
      // How to set datasource
      

      getSlingScriptHelper().include(wrapper);
    }
}
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

can you try with below, I am not sure if this is gonna work or not

request.setAttribute(DataSource.class.getName(), ds);



Arun Patidar

View solution in original post

7 Replies

Avatar

Level 2

Dear,

Thanks for your idea.

If the select field has resource and [datasource] node, it can work. But the select is created by [ValueMapResourceWrapper]. It is no resource, actual node and [datasource] node. I have try to below source code, but not work too.

 

wrapperProperties.put("datasource", ds);

Avatar

Community Advisor

@LiYanfei 

Could you please elaborate the need/use case for programmatic creation of select resource 

Avatar

Level 2

It use for page properties, and reference from CF Model.

eg: add a select field to CF Model,that be added on the page properties.

Avatar

Community Advisor

Dropdown field is available in CF model via the field named "Enumeration".

Did you try that already ?

Avatar

Correct answer by
Community Advisor

can you try with below, I am not sure if this is gonna work or not

request.setAttribute(DataSource.class.getName(), ds);



Arun Patidar