gpt4 book ai didi

javascript - SAPUI5:将 OData 与同一个表中的其他 Web 服务组合

转载 作者:行者123 更新时间:2023-11-28 17:36:38 25 4
gpt4 key购买 nike

这个问题是关于使用 SAP Web IDE 开发 SAPUI5。我想将 OData 服务与我通过 POST 方法调用的 Web 服务结合起来。所以基本上是这样的:我从 OData 服务(来自后端 SAP 系统)检索一个列表,并将其显示在表元素中。到目前为止,一切都很好。它有效。

现在,对于表中的每一行,我想使用 POST 方法调用 Web 服务来检索我也想在该行中显示的值。喜欢:

行第 1 列的值为 A,第 2 列的值为 B,我将其传递给 Web 服务,该服务告诉我“对于 A 和 B,结果是 X”。所以我想在同一行的另一列中显示 X。

如何实现这一目标?有你知道的例子吗?

最佳答案

使用 AJAX POST 响应中的数据创建 JSON 模型。将表中所需的单元格绑定(bind)到这个新的 JSON 模型。然后,如果您想根据从每个特定行的 OData 接收的值在 JSON 模型中选择数据,请使用包含部件的格式化程序函数。

这里是从 Northwind 获取数据和带有虚假数据的 JSON 模型的示例:https://jsbin.com/racenaqoki/edit?html,output

通过真正的 AJAX 调用: https://jsbin.com/jivomamuvi/edit?html,output

<html>
<head>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta charset="utf-8">

<title>MVC with XmlView</title>

<!-- Load UI5, select "blue crystal" theme and the "sap.m" control library -->
<script id='sap-ui-bootstrap'
src='https://sapui5.hana.ondemand.com/resources/sap-ui-core.js'
data-sap-ui-theme='sap_bluecrystal'
data-sap-ui-libs='sap.m'
data-sap-ui-xx-bindingSyntax='complex'></script>


<!-- DEFINE RE-USE COMPONENTS - NORMALLY DONE IN SEPARATE FILES -->

<!-- define a new (simple) View type as an XmlView
- using data binding for the Button text
- binding a controller method to the Button's "press" event
- also mixing in some plain HTML
note: typically this would be a standalone file -->

<script id="view1" type="sapui5/xmlview">
<mvc:View xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc" controllerName="my.own.controller">
<Page>
<Table items="{/Orders}" updateFinished="onTableUpdateFinished">
<columns>
<Column >
<Text text="My Value From OData"/>
</Column>
<Column>
<Text text="My Value From AJAX"/>
</Column>
</columns>
<items>
<ColumnListItem>
<cells>
<ObjectIdentifier title="{CustomerID}"/>
<ObjectIdentifier text="{parts:[{path:'CustomerID'}, {path:'myOtherModel>/'}], formatter: '.myFormatter'}"/>
</cells>
</ColumnListItem>
</items>
</Table>
</Page>
</mvc:View>
</script>


<script>
// define a new (simple) Controller type
sap.ui.controller("my.own.controller", {
onInit: function(){
var myOtherModel = new sap.ui.model.json.JSONModel();
this.getView().setModel(myOtherModel, "myOtherModel");

//Here your AJAX call to get the data from a POST request
/*
$.ajax({
type: "POST",
url: url,
data: data,
success: this.ajaxSuccess,
dataType: dataType
});
*/

//Let's simulate that there was a success receiving the following data
var data = {
VINET: {
message: "VINET Rocks!!"
},
WARTH: {
message: "WARTH is good company!!"
},
RICSU: {
message: "RICSU I don't like"
},
HANAR: {
message: "HANAR was my first customer"
}
}
this.ajaxSuccess(data);
},

ajaxSuccess: function(data){
this.getView().getModel("myOtherModel").setData(data)
},

myFormatter: function(sCustomerID, otherModelData){
// This formatter will be executed for each table row.
// In the first parameter, the value binded in the first column
// In the second parameter, the node you want of your second model (in this case the root node)

//do whatever you want here and return a string for the 'text' property in this case
if(otherModelData[sCustomerID]){
// If there is a node in your second model with a node for the given Customer ID, then return the message into it
return otherModelData[sCustomerID].message;
}
//Otherwise return empty string
return "";
}
});



/*** THIS IS THE "APPLICATION" CODE ***/
// create a Model and assign it to the View
// Using here the HEROKU Proxy to avoid a CORS issue
var uri = "https://cors-anywhere.herokuapp.com/services.odata.org/Northwind/Northwind.svc"; // local proxy for cross-domain access
var oNorthwindModel = new sap.ui.model.odata.ODataModel(uri, {
maxDataServiceVersion: "2.0"
});
// instantiate the View
var myView = sap.ui.xmlview({viewContent:jQuery('#view1').html()}); // accessing the HTML inside the script tag above
// Set the OData Model
myView.setModel(oNorthwindModel);


myView.placeAt('content');
</script>

</head>
<body id='content' class='sapUiBody'>
</body>

关于javascript - SAPUI5:将 OData 与同一个表中的其他 Web 服务组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49006775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com