{"id":22961,"date":"2015-07-20T21:36:23","date_gmt":"2015-07-20T16:06:23","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=22961"},"modified":"2016-08-29T17:44:32","modified_gmt":"2016-08-29T12:14:32","slug":"aem-ways-to-populate-a-selection-widget","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/aem-ways-to-populate-a-selection-widget\/","title":{"rendered":"AEM: Ways to populate a Selection Widget"},"content":{"rendered":"<div class=\"entry-content\">\n<p>One of the powerful features that <a title=\"AEM Consulting and Development\" href=\"http:\/\/www.tothenew.com\/wcm\/cq-aem-development-consulting\">AEM offers<\/a> is its authoring capabilities. It provides easy to use GUI for authors to create and edit content. When it comes to authoring the content, dialog plays a vital role in that. A dialog accepts inputs via <strong>Widgets<\/strong> and then makes that input available for further use. One of the most commonly used widget is the Selection Widget. If you have a nice handy JSON data source which is conveniently organised into key\/value pairs, you can use this to populate the values for the selection widget (dropdown, combo box, radio buttons and checkboxes) in the dialog. In this blog post, we&#8217;ll talk about the ways to populate the selection widget.<\/p>\n<p>There are commonly two ways to provide the options for a selection widget:<br \/>\n<b><br \/>\n1) Static<\/b><\/p>\n<p><b>2) Dynamic<\/b><\/p>\n<\/div>\n<p><b>1) Static:<\/b>\u00a0When the options are limited in number and are generally fixed, we use this. Making them static will result in a faster response and better performance.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-23003\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/07\/Selection_023.png\" alt=\"Selection_023\" width=\"1204\" height=\"216\" \/><\/p>\n<p>The dialog.xml file that contain static options will look like this:<\/p>\n<pre>&lt;selection jcr:primaryType=\"cq:Widget\"\r\n    fieldLabel=\"Select the author name\"\r\n    fieldDescription=\"Please select\"\r\n    name=\".\/selection\"\r\n    type=\"select\"\r\n    xtype=\"selection\"&gt;\r\n    &lt;options jcr:primaryType=\"cq:WidgetCollection\"&gt;\r\n        &lt;option1\r\n            jcr:primaryType=\"nt:unstructured\"\r\n            text=\"Laura\"\r\n            value=\"Laura\"\/&gt;\r\n        &lt;option2\r\n            jcr:primaryType=\"nt:unstructured\"\r\n            text=\"Olive\"\r\n            value=\"Olive\"\/&gt;    \r\n        &lt;option3\r\n            jcr:primaryType=\"nt:unstructured\"\r\n            text=\"Larry\"\r\n            value=\"Larry\"\/&gt;    \r\n    &lt;\/options&gt;\r\n&lt;\/selection&gt;<\/pre>\n<p><b>Please note that the JSON format for options of selection widget should be of the following format<\/b><\/p>\n<pre class=\"lang-java prettyprint prettyprinted\"><code><span class=\"pun\">[<\/span>\r\n    <span class=\"pun\">{<\/span>\r\n        <span class=\"str\">\"value\"<\/span><span class=\"pun\">:<\/span> <span class=\"lit\">10<\/span><span class=\"pun\">,<\/span>\r\n        <span class=\"str\">\"text\"<\/span><span class=\"pun\">:<\/span> <span class=\"str\">\"A\"<\/span>\r\n    <span class=\"pun\">},<\/span> <span class=\"pun\">{<\/span>\r\n        <span class=\"str\">\"value\"<\/span><span class=\"pun\">:<\/span> <span class=\"lit\">20<\/span><span class=\"pun\">,<\/span>\r\n        <span class=\"str\">\"text\"<\/span><span class=\"pun\">:<\/span> <span class=\"str\">\"B\"<\/span>\r\n    <span class=\"pun\">}<\/span>\r\n<span class=\"pun\">]<\/span> <\/code><\/pre>\n<p>Here <b>text<\/b>\u00a0denotes string that will appear in the dialog dropdown and <b>value<\/b> is used for background processing in the application logic.<\/p>\n<p><b>2) Dynamic: <\/b>When\u00a0we have to calculate our options based on some parameters, or they are provided by some third party service, we use the dynamic way of populating it. Dynamic options can be generated using any file that will output JSON data. The file could be a static file that contains the JSON data or can be some OSGi service or OSGi configuration that will generate a dynamic JSON data. Here dynamic options are fetched using <a href=\"http:\/\/www.tothenew.com\/blog\/creating-osgi-factory-configurations-in-aem\/\"> OSGi Factory<\/a> Configuration. This post uses a Java Servlet which generates the options for dropdown, and creates a JSON from the configuration values received the Factory Service. Here the OSGI factory configuration is FelixConfig and the service that uses this \u00a0FactoryConfig Component\/Service to add configuration and accept values of properties is FelixConfigInterface. The OSGi Factory Configuration used is:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-23088\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/07\/Selection_026.png\" alt=\"Selection_026\" width=\"1151\" height=\"268\" \/><\/p>\n<p>The first step is to create a servlet that will return the dynamic JSON data in the above specified format:<\/p>\n<p>[java]<br \/>\n@SlingServlet(paths = &quot;\/bin\/service\/felix&quot;, methods = &quot;GET&quot;)<br \/>\npublic class FelixConfigServlet extends SlingSafeMethodsServlet {<\/p>\n<p>@Reference<br \/>\nFelixConfigInterface felixConfigInterface;<\/p>\n<p>@Override<br \/>\nprotected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {<br \/>\nJSONArray jsonArray = new JSONArray();<br \/>\nresponse.setContentType(&quot;application\/json&quot;);<br \/>\nresponse.setCharacterEncoding(&quot;UTF-8&quot;);<br \/>\nList&amp;lt;String&amp;gt; list = felixConfigInterface.getFactoryConfigs();<br \/>\nJSONObject json = new JSONObject();<br \/>\nint count = 0;<br \/>\nfor (String name : list) {<br \/>\nJSONObject jsonObject = null;<br \/>\ncount++;<br \/>\ntry {<br \/>\njsonObject = new JSONObject();<br \/>\njsonObject.put(&quot;text&quot;, name);<br \/>\njsonObject.put(&quot;value&quot;, count);<br \/>\n} catch (JSONException e) {<br \/>\ne.printStackTrace();<br \/>\n}<br \/>\njsonArray.put(jsonObject);<br \/>\n}<br \/>\nresponse.getWriter().write(jsonArray.toString());<br \/>\n}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Next step is to create a component in the &#8216;\/apps&#8217; directory in the usual way\u00a0and create the dialog which will have a selection widget which uses the above-created servlet to populate its values.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/07\/Selection_0191.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-22969 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/07\/Selection_0191.png\" alt=\"Selection_019\" width=\"1208\" height=\"169\" \/><\/a><\/p>\n<p>In the dialog,instead of having a static options array we have a Servlet path, which will return a\u00a0JSON. Values will get populated from this JSON.<\/p>\n<p>In dialog,\u00a0 create a dropdown widget (xtype: selection, type:select) and add a property as below:<\/p>\n<p>Property name: \u201c<strong>options<\/strong>\u201d<\/p>\n<p>Property value:\u00a0 \u201c<strong>\/bin\/service\/felix<\/strong>\u201d\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (path of the servlet)<\/p>\n<p>Save the changes and the dropdown will get \u00a0populated by values provided in the OSGi Factory Configuration<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-23087\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/07\/Selection_025.png\" alt=\"Selection_025\" width=\"967\" height=\"357\" \/><\/p>\n<p>In the similar way, we can also create Checkboxes and Radio buttons with dynamic options.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the powerful features that AEM offers is its authoring capabilities. It provides easy to use GUI for authors to create and edit content. When it comes to authoring the content, dialog plays a vital role in that. A dialog accepts inputs via Widgets and then makes that input available for further use. One [&hellip;]<\/p>\n","protected":false},"author":202,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":45},"categories":[1],"tags":[1235,4847,1581,2002,49,4850],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22961"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/202"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=22961"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22961\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=22961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=22961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=22961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}