[SalesForce] How to get all selected items of ui:menu

I am following example given in auradocs and lightning developer guide
where v.childMenuItems attribute of menuList components should return list checkboxMenuItem or radioMenuItem but instead I am getting undefined in Lightning Experience. Attribute v.childMenuItems is probably private thats why I am not access it. Is there any other way to get all selected options?

In myCmp.cmp

<ui:menu>
<ui:menuTriggerLink class="checkboxMenuLabel" aura:id="checkboxMenuLabel" label="Multiple selection"/>
   <ui:menuList aura:id="checkboxMenu" class="checkboxMenu">
    <aura:iteration items="{!v.status}" var="s">
        <ui:checkboxMenuItem label="{!s}"/>
        </aura:iteration>
    </ui:menuList>
</ui:menu>

and In myCmpController.js

getMenuSelected: function(cmp) {
    var menuCmp = cmp.find("checkboxMenu");
    var menuItems = menuCmp.get("v.childMenuItems");
    var values = [];
    for (var i = 0; i < menuItems.length; i++) {
        var c = menuItems[i];
        if (c.get("v.selected") === true) {
            values.push(c.get("v.label"));
        }
    }
    var resultCmp = cmp.find("result");
    resultCmp.set("v.value", values.join(","));
},

Best Answer

You're correct that v.childMenuItems is private (only available in open-source Aura) and this is a doc bug in the Dev Guide. A quick fix to this is to declare an aura:id to your ui:checkboxMenuItem tag.

<ui:checkboxMenuItem aura:id="item" label="{!s}"/>

In your client-side-controller, you can retrieve the menu items as an array.

 var menuItems = cmp.find("item");
Related Topic