Skip to main content


Simple AEM Granite Order Widget (Draggable Lists)

Olav Ahrens Rotne 4ennrbj1svk Unsplash

Recently, we required an authoring experience where an author can re-order a list of fixed tabs to their desired order. The only Granite widget that allows drag-n-drop ordering is multifield, but we don’t need a multifield. We just wanted the ability to order a fixed list.

An Existing solution

ACS Commons has this Draggable Lists Widget, but I did not want to include ACS commons just for that widget. Additionally, I looked at the implementation for that widget, and it seemed to be complex granted it might be solving a different problem. It also used Coral UI 2; we are on AEM 6.4, where Coral UI 3 is the latest Coral version.

A Simple Solution

If you look at Coral UI 3 documentation, the only component that allows drag-n-drop ordering is the Coral.Table component. This means we can create a Granite widget that uses that UI to render a list!


Here is the widget JSP impl: (Java 8)

for the purpose of this post, I’ve created /apps/widgets/orderedList/orderedList.jsp

Here, I render each item in the list as a hidden field with the same “name”.
The order of the hidden fields in the HTML is what determines the saved order in the JCR content node/prop.
This also means that we dont need ANY custom JS to make this work! just the OOTB CUI3 Table component!

  Renders a List of items for ordering purposes only
%><%@ include file="/libs/granite/ui/global.jsp" %><%
%><%@ page session="false"
        java.util.*" %>
  Ordered List
  // TODO
  .. granite:servercomponent:: widgets/orderedList

    Renders a List of items for ordering purposes only
    It has the following content structure:
    + myList
      - sling:resourceType = "widgets/orderedList"
      - name = "./myList"
      - title = "My List"
      + items
        + item1
          - text = "Item 1"
          - value = "item1"
        + item 2
          - text = "Item 2"
          - value = "item2"

  if (!cmp.getRenderCondition(resource, false).check()) {
  Tag tag = cmp.consumeTag();
  AttrBuilder attrs = tag.getAttrs();
  Config cfg = cmp.getConfig();
  String title = cfg.get("title", String.class);
  String name = cfg.get("name", String.class);
  // the easy way to do this...
  String cleanName = 
    name != null 
    ? name.replace(".", "").replace("/", "")
    : "";
  String tableId = "order-table-" + cleanName;
  String hiddenInputId = "order-input-" + cleanName;

  String[] values = cmp.getValue().getContentValue(name, new String[0]);
  List<String> valuesList = Arrays.asList(values);
  Iterator<Resource> itemsIterator = cmp.getItemDataSource().iterator();
  // server-side ordering of values based on already saved order
  List<ValueMap> items =
      Spliterators.spliteratorUnknownSize(itemsIterator, Spliterator.ORDERED),
      Comparator.comparing(item -> {
        ValueMap vm = ((Resource) item).getValueMap();
        String val = vm.get("value", "");
        return valuesList != null ? valuesList.indexOf(val) : 0;
<div <%= %>>
  <table is="coral-table" orderable id="<%=tableId%>">
      <col is="coral-table-column">
      <col is="coral-table-column" fixedwidth>
    <thead is="coral-table-head">
      <tr is="coral-table-row">
        <th is="coral-table-headercell"><%=title%></th>
        <th is="coral-table-headercell"></th>
    <tbody is="coral-table-body">
      for (ValueMap vm : items) {
        <tr is="coral-table-row">
          <td is="coral-table-cell">
            <%=vm.get("text", "") %>
            <input type="hidden" name="<%=name%>" value="<%=vm.get("value", "")%>"/>
          <td is="coral-table-cell">
            <button is="coral-button" type="button" variant="minimal" icon="dragHandle" coral-table-roworder></button>

Here is an example dialog XML that uses this widget:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling=""
  xmlns:nt="" jcr:primaryType="nt:unstructured" jcr:title="Product Tabs" sling:resourceType="cq/gui/components/authoring/dialog">
  <content jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/container">
    <items jcr:primaryType="nt:unstructured">
      <tabs jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/tabs" maximized="{Boolean}true">
        <items jcr:primaryType="nt:unstructured">
          <general jcr:primaryType="nt:unstructured" jcr:title="Properties" sling:resourceType="granite/ui/components/coral/foundation/container" margin="{Boolean}true">
            <items jcr:primaryType="nt:unstructured">
              <tabOrder jcr:primaryType="nt:unstructured"
                sling:resourceType = "widgets/orderedList"
                title="Tabs Order">
                <items jcr:primaryType="nt:unstructured">
                    text="Additiona info"

And here is a demo of the dialog:

Order List Demo

This widget stores the values as a multi-value property with the correct order:

Ordered List Jcr Content

Now you can use that order to sort your tabs in your component, or use it for another purpose entirely!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Ahmed Musallam, Adobe Technical Lead

Ahmed is an Adobe Technical Lead and expert in the Adobe Experience Cloud.

More from this Author

Follow Us