简体   繁体   English

SAPUI5和OData服务的动态URL

[英]SAPUI5 and dynamic URL for OData service

I am doing sample example using sapui5. 我正在使用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. 我想动态传递URL服务并加载ODATA服务,但我确实是sapui5的新手,但我不知道该怎么做。 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 // 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. 您必须为控件提供一个ID,才能在控制器中访问它们。 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 将odataModel的大小写更改为ODataModel
  2. Ensure the Service URL also correct 确保服务网址也正确
  3. Obtain the reference of the control As described by kjokinen 获取控件的参考如kjokinen所述

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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