下拉菜单获取对象JSON

时间:2017-04-12 18:38:23

标签: javascript jquery json sapui5

我希望在选择项目时从下拉菜单中获取对象名称。如何从事件itemSelect中获取对象?谢谢你的关注。

Dropdown menu pic

XML代码:

<core:FragmentDefinition
    xmlns="sap.m"
    xmlns:core="sap.ui.core"
    xmlns:u="sap.ui.unified">
        <u:Menu items="{kepesertaanmodel>/}" id="menuEventingKepesertaan"
            itemSelect="handleMenuItemPressKepesertaan">
            <u:MenuItem
                text="{kepesertaanmodel>name}"
                icon="{kepesertaanmodel>icon}">

                <u:Menu items="{kepesertaanmodel>sub/}" id="submenuEventingKepesertaan"
                        itemSelect="handleSubMenuItemPressKepesertaan">
                    <u:MenuItem
                        text="{kepesertaanmodel>name}"
                        icon="{kepesertaanmodel>icon}"/>
                </u:Menu>
            </u:MenuItem>
        </u:Menu>
</core:FragmentDefinition>

Javascript代码:

handleMenuItemPressKepesertaan: function(oEvent) {          
    alert(oEvent.getSource().getItems().getBindingContext().getObject().name);
}

JSON结构:

        dropdownMenu: {
            kepesertaan:[
                {
                    name: "Menu1",
                    icon: "sap-icon://crop",
                    sub: [
                        { 
                            name: "Submenu1-1",
                            icon: "sap-icon://create-session"   
                        },
                        { 
                            name: "Submenu1-2",
                            icon: "sap-icon://create-form" 
                        }
                    ]
                },
                {
                    name: "Menu2",
                    icon: "sap-icon://detail-view",
                    sub: [
                        { 
                            name: "Submenu2-1",
                            icon: "sap-icon://add-activity" 
                        },
                        { 
                            name: "Submenu2-2",
                            icon: "sap-icon://action" 
                        }
                    ]
                }           
            ]
}

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

这有用吗:

oEvent.getParameter("item").getBinding("text").getValue("name")

您还可以查看this snippet