Search code examples
sapui5

Dynamic link in MessagePopover fragment not showing


I've been developing a UI5 app that uses a model to store notifications. I want to display these notifications inside a MessagePopover that can be triggered using the footer.

The data binding works perfectly. I can see that all the properties are set and match the data inside the model. But the link in the details page is not showing up.

When I use a static Link (i.e. a static link to google.com) that is not using data binding, the link is rendered.

Does anyone of you came across the same problem and has a solution for it?

Here is my code:

sap.ui.define([
  "sap/ui/core/mvc/Controller"
], function(BaseController) {
  return BaseController.extend("test.Test", {
        onShowNotificationPopover: function(oEvent){
            if (!this._notificationPopover) {
                var oMessageTemplate = new sap.m.MessageItem({
                    type : '{data>type}',
                    title : '{data>title}',
                    subtitle : "{data>subtitle}",
                    description : '{data>description}',
                    markupDescription : "{data>markupDescription}",
                    groupName : "{data>groupName}"
                });
                var oLink = new sap.m.Link({
                    text : "{data>link/text}",
                    href : "{data>link/url}",
                    target : "_blank"
                });
                /* Working with static */
                // oLink = new sap.m.Link({text: "Google", href: "http://google.com", target: "_blank"})
                oMessageTemplate.setLink(oLink);

                var oPopover = new sap.m.MessagePopover();
                oPopover.bindAggregation("items",{
                    template: oMessageTemplate,
                    path: "data>/notifications"
                });
                this._notificationPopover = oPopover; 
                this.getView().addDependent(this._notificationPopover);
            }
            this._notificationPopover.toggle(oEvent.getSource());
        }
    });

});

The view contains the following:

<mvc:View xmlns:mvc="sap.ui.core.mvc" xmlns:f="sap.f" xmlns="sap.m" controllerName="test.Test">
    <f:DynamicPage showFooter="true">
        <f:footer>
            <OverflowToolbar>
                <Button icon="sap-icon://message-popup" type="Emphasized"
                    press="onShowNotificationPopover" />
            </OverflowToolbar>
        </f:footer>
    </f:DynamicPage>
</mvc:View>

And the index.html

<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta http-equiv='Content-Type' content='text/html;charset=UTF-8'/>

    <script src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
            id="sap-ui-bootstrap"
            data-sap-ui-libs="sap.m, sap.f"
            data-sap-ui-theme="sap_belize"
            data-sap-ui-bindingSyntax="complex"
            data-sap-ui-resourceroots='{ 
                "test": "."
            }'
            >
    </script>
    <script>
        sap.ui.getCore().attachInit(function() {
            var oModel = new sap.ui.model.json.JSONModel();
            oModel.setData({
                notifications: [{
                    type: "Error",
                    title: "Title1",
                    subtitle: "Subtitle",
                    description: "This is a description and below you should see a link",
                    markupDescription: false,
                    groupName: "notification",
                    link: {
                        text: "Click here",
                        url: "http://www.google.com"
                    }
                }]
            });
            var oView = sap.ui.xmlview("test.Test");
            oView.setModel(oModel, "data");
            oView.placeAt("content");
        });
    </script>
</head>
<body class="sapUiBody" role="application">
    <div id="content"></div>
</body>
</html>

Example: Popover with dynamic link

Example: Popover with static link


Solution

  • I found a solution for my problem: the event itemPress of MessagePopover.

    The event handler inside the controller:

    onNotificationItemSelect: function(oEvent){
        var oItem = oEvent.getParameter("item"), oBindingContext = oItem.getBindingContext("data");
        var oData = oBindingContext.getModel().getProperty(oBindingContext.getPath());
        oItem.getLink().setText(oData.link.text);
        oItem.getLink().setHref(oData.link.url);
    }
    

    And you have to register it:

    var oPopover = new sap.m.MessagePopover({
        itemSelect: this.onNotificationItemSelect
    });
    

    Additional Information

    When you display the content of the popover using

    console.log(oPopover.getItems()[0].getLink())
    

    the correct properties are shown. see here

    But when you search the DOM you can see that the link is copied and does not contain the required binding. see here