简体   繁体   中英

SAPUI5 and dynamic URL for OData service

I am doing sample example using sapui5. I want to pass URL service dynamically and load ODATA service, but I really new with sapui5 and I don't know how to do it. This code below is what i tried to do but it is not working. Thanks a lot for your help.

createContent : function(oController) {
    var oLayout = new sap.ui.commons.layout.AbsoluteLayout({width:"340px",height:"150px"});
    oLayout.addStyleClass("CustomStyle"); //Add some additional styling for the border


    var oLabel = new sap.ui.commons.Label({text:"Service Url"});
    var oUrlInput = new sap.ui.commons.TextField({width:"190px"});
    oLabel.setLabelFor(oUrlInput);
    oLayout.addContent(oLabel, {right:"248px",top:"20px"});
    oLayout.addContent(oUrlInput, {left:"110px",top:"20px"});


var oLabel = new sap.ui.commons.Label({text:"Service"});
    var oSvcInput = new sap.ui.commons.TextField({width:"190px"});
    oLabel.setLabelFor(oSvcInput);
    oLayout.addContent(oLabel, {right:"248px",top:"62px"});
    oLayout.addContent(oSvcInput, {left:"110px",top:"62px"});

    var loadData =new sap.ui.commons.Button({
        text : "load",
        width:"133px",
     press: function() {
                 oController.load();
                  }});

    oLayout.addContent(loadData, {left:"110px",top:"104px"});


    return oLayout;
}

// Controller

load: function(oEvent){


    var url = sap.ui.getControl("oUrlInput").getValue();
    var svc = sap.ui.getControl("oSvcInput").getValue();

    var oModel = new sap.ui.model.odata.OdataModel(url + "/" + svc ,false);
    var mylist = new sap.ui.model.ListBinding(oModel);
     return mylist;




}

// index.html

<!DOCTYPE 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="resources/sap-ui-core.js"
                id="sap-ui-bootstrap"
                data-sap-ui-libs="sap.ui.commons,sap.ui.table,sap.ui.ux3"
                data-sap-ui-theme="sap_bluecrystal">
        </script>
        <!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->

        <script>
                sap.ui.localResources("simpleform");
                var view = sap.ui.view({id:"idsimpleForm1", viewName:"simpleform.simpleForm", type:sap.ui.core.mvc.ViewType.JS});
                view.placeAt("content");
        </script>

    </head>
    <body class="sapUiBody" role="application">
        <div id="content"></div>
    </body>
</html>

You have to give your controls an id to access them in the controller. Like this:

// create controls with id
var oLabel = new sap.ui.commons.Label("oLabelId", {text:"Service Url"});
var oUrlInput = new sap.ui.commons.TextField("oUrlInputId", {width:"190px"});

// then to get reference to the control later
var oLabel = sap.ui.getCore().byId("oLabelId");
var oUrlInput = sap.ui.getCore().byId("oUrlInputId");

确保使用正确的网址:

var oModel = new sap.ui.model.odata.OdataModel("/sap/opu/odata/sap/" + svc ,false);

Make sure following.

  1. Change the case of odataModel to ODataModel
  2. Ensure the Service URL also correct
  3. Obtain the reference of the control As described by kjokinen

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM