Domanda

ere is my dropdown widget structure,

    <chooseselect
                    jcr:primaryType="cq:Widget"
                    name="./sel"
                    options="/bin/demoslingservlet.json"
                    type="select"
                    xtype="selection">

    re</chooseselect>

Im able to hit the below servlet,and code in servlet is below:

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse   response) throws ServletException, IOException
{

JSONObject json = new JSONObject();
JSONArray jsonArray = new JSONArray();
HashMap hashMap1 = new HashMap();
HashMap hashMap2 = new HashMap();

hashMap1.put("A", 10L);
hashMap2.put("B", 20L);
jsonArray.put(hashMap1);
jsonArray.put(hashMap2);

json.put("jsonarray", jsonArray);
PrintWriter printWriter = response.getWriter();
    printWriter.print(json);
    printWriter.flush();
    }

Im able to see the output {"jsonarray":[{"A":10},{"B":20}]} in the response, but how to render this response to the dropdown in the dialog. Thanks in advance.

È stato utile?

Soluzione

The format in which you are populating the options is wrong. The options for selction should be in the following format

[
    {
        "value": 10,
        "text": "A"
    }, {
        "value": 20,
        "text": "B"
    }
] 

Try to generate the above format like this

StringWriter writer = new StringWriter();
TidyJSONWriter json = new TidyJSONWriter(writer);
json.array();
/* loop through your options and create objects as shown below */
json.object();
json.key("text");
json.value("A");
json.key("value");
json.value(10);
json.endObject();

json.object();
json.key("text");
json.value("B");
json.key("value");
json.value(20);
json.endObject();
/* end your array */
json.endArray();
response.getWriter().write(writer.toString());

Your selection widget would then display the json in the dropdown.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top