OData Edm.DateTime - 如何仅显示日期 [英] OData Edm.DateTime - How to Display Date Only

查看:33
本文介绍了OData Edm.DateTime - 如何仅显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 UI5 中,我的 OData 以以下格式返回日期:

<块引用>

2018 年 4 月 9 日星期一 01:00:00 GMT+0100(GMT 夏令时)

我想将其格式化为:09/04/2018

我尝试了以下没有任何结果:

我尝试过的另一个例子,它也不起作用:

创建模型的控制器如下所示:

onInit: function() {var sServiceUrl = "http://myurl/myservice";var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl,true);var oJsonModel = new sap.ui.model.json.JSONModel();oModel.read("/MyEntitySet", null, null, true, function(oData, response) {oJsonModel.setData(oData);});this.getView().setModel(oJsonModel, "MyEntitySet");},

解决方案

如果您使用 OData 协议,则需要使用适当的 数据类型(而不是简单类型).

查看您的服务元数据并查找 EDM 类型 分配给实体属性.例如:

给定

OData V2 实体属性

财产绑定(仅限日期)

  • 约束 displayFormat 使我们能够仅显示 DateTime 值的日期部分.<块引用>

    可能是日期",在这种情况下只使用日期部分,时间部分始终为 00:00:00,时区为 UTC,以避免与时区相关的问题.[来源]

  • 还有 EDM 类型 Date,但它仅用于 OData V4 服务.<块引用>

    对于 OData V2 服务,使用 sap.ui.model.odata.type.DateTime 和约束 displayFormat: "Date" 以仅显示日期.[来源]

  • 大多数情况下,无需更改 formatOptions 中的 pattern,因为 相应地已经检测到用户并且无相关的日期ref176f176f4d1014b6dd926db0e91070__section_6CAF7D95C27C487CB201C487CB201CE830"> r
<小时>

不工作"部分

(仅针对问题作者)

正如问题中所证明的那样,不工作"实际上是指数据根本不在 UI 中,而不是数据格式不正确.

感谢第 6 次修订,我们可以看到主要原因在于MVC 中的 MonInit 中处理.由于有几个问题,让我们一一解决..:

  1. 为了首先显示数据,模型名称(设置模型时一个和定义绑定路径时一个)必须彼此相同.但是,在您的代码中并非如此:

    <块引用>

    this.getView().setModel(/*...*/, "MyEntitySet");

    <块引用>

    [API] 指的是绑定路径中使用的名称.由于没有名为 "MyoDataService" 的模型集,框架不知道它应该从 "MyEntitySet" 模型检索数据.有关更多信息,请阅读文档主题绑定语法和基础主题绑定路径彻底.

  2. 不确定你从哪里得到的片段,但是 sap.ui.model.odata.ODataModel[API] 是一个古老的模块,它已经已弃用好久了.它还同步发送许多请求,这会由于 UI 线程的阻塞行为而降低 UX.

    <块引用>

    工作人员之外的同步 XMLHttpRequest 正在从网络平台中删除,因为它对最终用户的体验有不利影响.[来源]

    请使用v2.ODataModel 代替.

  3. 不需要中间的 JSONModel 来处理后端数据.JSONModel 是一个客户端模型,不打算用于处理 OData 相关的东西.否则,您将再次发明轮子,因为 ODataModel 及其绑定已经支持高效发送优化请求,而无需下载整个实体集.这是一个示例:https://embed.plnkr.co/lBreXFg9ozLcvMCN.该示例还显示了各种绑定类型(上下文绑定、列表绑定、属性绑定等)及其语法.在 OData V4 中,您甚至无法直接调用 .read.习惯直接在视图中绑定 OData 就好了.

如需进一步了解和最佳实践,请仔细按照演练步骤操作并尝试理解那里的每一个句子.

In UI5, my OData is returning the date in the following format:

Mon Apr 09 2018 01:00:00 GMT+0100 (GMT Daylight Time)

I would like to format it to: 09/04/2018

I have tried the following without any results:

<Text text="{                            
  path: 'MyoDataService>ExecuteDay',
  type: 'sap.ui.model.type.Date',
  formatOptions: {
    pattern: 'yyyy/MM/dd'
  }
}"/>

Another example I have tried and It is not working neither:

<Text text="{
  path:'MyoDataService>ExecuteDay',
  type: 'sap.ui.model.type.Date',
  formatOptions: {
    source: {
      pattern: 'yyyy-MM-ddTHH:mm:ss'
    },
    pattern: 'dd MMM yyyy'
  }
}" />

The controller where the model gets created looks like this:

onInit: function() {
  var sServiceUrl = "http://myurl/myservice";
  var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl,true);
  var oJsonModel = new sap.ui.model.json.JSONModel();
  oModel.read("/MyEntitySet", null, null, true, function(oData, response) {
    oJsonModel.setData(oData);
  });
  this.getView().setModel(oJsonModel, "MyEntitySet");
},

解决方案

If you are using OData protocol, you'll need to use the appropriate data type as well (instead of simple types).

Take a look at your service metadata and look for the EDM type that is assigned to the entity property. For example:

Given

<Property Name="BirthDate" Type="Edm.DateTime"/>

OData V2 entity property

Property Binding (Date only)

<Text text="{                            
  path: 'myODataModel>BirthDate',
  type: 'sap.ui.model.odata.type.DateTime',
  constraints: {
    displayFormat: 'Date'
  }
}"/>

  • The constraint displayFormat enables us to display only the date part of the DateTime value.

    may be "Date", in this case only the date part is used, the time part is always 00:00:00 and the time zone is UTC to avoid time-zone-related problems. [source]

  • There is also the EDM type Date but it's intended to be used for OData V4 services only.

    For an OData V2 service, use sap.ui.model.odata.type.DateTime with the constraint displayFormat: "Date" to display only a date. [source]

  • Most of the times, there is no need to alter the pattern within formatOptions since the framework already detects the user locale and displays the date accordingly.

The "not working" part

(For question author only)

As it turned out in the question, "not working" was actually referring to the data not being in the UI at all rather than data being improperly formatted.

Thanks to the 6th revision, we can see that the main cause lies in the way of how the M in MVC was handled in onInit. As there are several issues, let's tackle them one by one..:

  1. In order to display the data in the first place, the model names (one when setting the model and one when defining the binding path) must be identical to each other. This was, however, not the case in your code:

    this.getView().setModel(/*...*/, "MyEntitySet");
    

    <Text text="{
     path: 'MyoDataService>...'
    

    The second argument in setModel[API] refers to the name that is used in the binding path. Since there was no model set with the name "MyoDataService", the framework couldn't know that it should have retrieved the data from "MyEntitySet" model. For more information, please read the documentation topic Binding Syntax and the underlying topic Binding Path thoroughly.

  2. Not sure where you got the snippet from but sap.ui.model.odata.ODataModel[API] is an ancient module which has been deprecated for a long time. It also sends many requests synchronously which degrades UX due to the blocking behavior of the UI thread.

    Synchronous XMLHttpRequest outside of workers is in the process of being removed from the web platform as it has detrimental effects to the end user’s experience. [source]

    Please, use v2.ODataModel instead.

  3. There is no need for an intermediate JSONModel to handle backend data. JSONModel is a client-side model which is not intended to be used for handling OData related stuff. Otherwise, you're going to invent the wheel again since the ODataModel and its binding already support sending optimized requests efficiently without having to download the whole set of entities. Here is an example: https://embed.plnkr.co/lBreXFg9ozLcvMCN. The example also shows various binding types (Context binding, List Binding, Property Binding, etc..) and their syntax. In OData V4, you won't be even able to call .read directly. It's good to get used to binding OData in the view directly.

For further understanding and best practices, please follow the Walkthrough steps carefully and try to understand every single sentence there.

这篇关于OData Edm.DateTime - 如何仅显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆