在我的SAPUI5应用程序中,有一个 JS片段,它不会更新我的数据。如果刷新窗口,则可以正确获取数据,但是需要使用 window.location.reload()。
您可以检查以下代码:
onInit: function () {
var oRouters = sap.ui.core.UIComponent.getRouterFor(this);
oRouters.getRoute("chartContainer").attachPatternMatched(this._onObjectMatched, this);
},
_onObjectMatched: function (oEvent) {
var sYear = oEvent.getParameter("arguments").Year;
var sMonth = oEvent.getParameter("arguments").Month;
var sGroup = oEvent.getParameter("arguments").Group;
var sCurrency = oEvent.getParameter("arguments").Currency;
if (sYear === undefined && sMonth === undefined && sGroup === undefined && sCurrency === undefined){
var oStartupParameters = this.getOwnerComponent().getComponentData().startupParameters;
sYear = oStartupParameters.Year;
sMonth = oStartupParameters.Month;
sGroup = oStartupParameters.Group;
sCurrency = oStartupParameters.Currency
}
var sServiceUrl = "/sap/opu/odata/sap/Z_TEST/";
var filterYear = new sap.ui.model.Filter("Year", sap.ui.model.FilterOperator.EQ, sYear);
var filterMonth = new sap.ui.model.Filter("Month", sap.ui.model.FilterOperator.EQ, sMonth);
var filterGroup = new sap.ui.model.Filter("Group", sap.ui.model.FilterOperator.EQ, sGroup);
var filterCurrency = new sap.ui.model.Filter("Currency", sap.ui.model.FilterOperator.EQ, sCurrency);
var oModelOdata = new sap.ui.model.odata.v2.ODataModel(sServiceUrl, {
json: true,
loadMetadataAsync: true
});
var oModel = new sap.ui.model.json.JSONModel();
oModelOdata.read("/DataSet",{
filters: [filterYear, filterMonth, filterGroup, filterCurrency],
success: function(oData, response) {
var oResults = oData.results;
oModel.setData(oData);
var oView = sap.ui.getCore().byId("__xmlview0");
if (oView !== undefined){
oView.setModel(oModel);
}else{
window.location.reload(); // It works correctly butI would like not to have to reload the whole page.
}
}
});
},
如何刷新视图而不刷新整个页面?
预先感谢您的支持 塞尔吉奥
答案 0 :(得分:1)
您应该使用refresh()
方法刷新模型:
oModelOdata.read("/DataSet",{
filters: [filterYear, filterMonth, filterGroup, filterCurrency],
success: function(oData, response) {
var oResults = oData.results;
oModel.setData(oData);
var oView = sap.ui.getCore().byId("__xmlview0");
if (oView !== undefined){
oView.setModel(oModel);
}else{
oModel.refresh(); // refresh new model data bindings
}
}
});
在进行API调用以获取数据时,您还应该使用promises
。