2017-04-11 59 views
0

将dateTime对象绑定到Text控件时,日期将以当前应用程序语言打印出来。它的风格可以通过“短”,“中”和“长”的风格属性的影响。如何在没有时区的情况下使用样式为“long”的DateTime?

我想使用长版本,而它加载了一个语言环境依赖模式,该模式以适当的方式呈现日期时间对象,除了最后的时区信息。

我当然可以使用自定义模式,但这会排除本地依赖项,因为它会覆盖样式属性。

我的想法是实现它: 我有自定义的DateTimeText控件,为我处理一些东西,另外,我想添加一个格式化程序,获取由控件生成的显示字符串(或从何处)最后10个字符(这是时区信息)

我努力得到这个工作,但我不知道如何从控制中获取输出字符串来操纵它。有没有一种方法(可能是一个更清洁的,然后我的想法)来得到它的工作?

DateTimeText控制

sap.ui.define(["sap/m/Text", "sap/ui/model/type/DateTime"], function (Text, DateTime) { 
    "use strict"; 

    var DateTimeText = Text.extend("com.ui.common.DateTimeText", { 
     metadata : { 
      properties : 
      { 
       style : {type : "string", group : "Data", defaultValue : "long"} 
      } 
     }, 

     renderer : "sap.m.TextRenderer", 

     onBeforeRendering : function() { 
      if(!this._bInitialized) { 
       var oTextBinding = this.getBindingInfo("text"); 
       var sStyle = this.getStyle(); 
       oTextBinding.parts[0].type = new DateTime({style: sStyle}); 
       oTextBinding.binding.fnFormatter = this.formatLongDate(oTextBinding.binding.oValue); 
       this.bindProperty("text", oTextBinding); 
       this._bInitialized = true; 
      } 
     }, 

     formatLongDate : function(oDate) { 
      console.log(oDate); 
      var s = oDate.slice(0,-10); 
      return s; 
     } 

    }); 

    return DateTimeText; 
}, true); 

不添加格式化

oTextBinding.binding.fnFormatter = this.formatLongDate(oTextBinding.binding.oValue); 

它显示的日期风格 “长”。但添加了格式化,当我得到这个错误:

Uncaught TypeError: oDate.slice is not a function 
    at formatLongDate (DateTimeText.js:27) 
    at f.onBeforeRendering (DateTimeText.js:19) 
    at f.a._handleEvent (sap-ui-core.js:959) 
    at t (sap-ui-core.js:1087) 
    at constructor.R.renderControl (sap-ui-core.js:1089) 
    at I.renderControl (sap-ui-core.js:457) 
    at Object.G.render (GridRenderer.js:6) 
    at constructor.R.renderControl (sap-ui-core.js:1089) 
    at I.renderControl (sap-ui-core.js:457) 
    at d.a.renderContainers (ResponsiveGridLayoutRenderer.js:6) 
+0

没有一些示例代码,这真的很难说。 – evolutionxbox

+0

我添加了我的DateTimeControl的代码 –

回答

0

既然你类型的值传递的javascript日期给格式化formatLongDate。因此,传递字符串值或获取格式化程序中对象的字符串值,如下所示。

oTextBinding.binding.fnFormatter = this.formatLongDate(oTextBinding.binding.oValue.toString()); 

或者您可以在格式化程序本身中获取日期对象的字符串值。

formatLongDate : function(oDate) { 
    if (oDate instanceof Date) { 
     var stringDate = oDate.toString(); 
     console.log(stringDate); 
     var s = stringDate.slice(0,-10); 
     return s; 
    } 
} 

编辑

您可以从任一删除的日期时间的时区下面的两种方法。

方法1:

您可以正常使用sap.m.Text控件本身如下(我给了XML视图)。

<Text text="{ 
      path: '/pathInModelToDateTime', 
      type: 'sap.ui.model.type.DateTime', 
      formatOptions: { 
       style: 'long/medium' 
      } 
     }"/> 

上述代码从dateTime对象中删除时区。

方法2:

,第二种方法是通过创建一个定制的控制和我不会建议更换这一个。这只是为了表明我们可以通过以下方法达到同样的效果。

sap.ui.define(["sap/m/Text", 
      "sap/ui/model/type/DateTime"], 
      function (Text, DateTime) { 
    "use strict"; 
    var DateTimeText = Text.extend("com.naveen.test.datetime.DateTimeText", { 
     metadata : { 
      properties : { 
       style : { type : "string", group : "Data", defaultValue : "long" } 
      } 
     }, 
     renderer : "sap.m.TextRenderer", 
     onBeforeRendering : function() { 
      if(!this._bInitialized) { 
       var oTextBinding = this.getBindingInfo("text"); 
       var sStyle = this.getStyle(); 
       if (sStyle === "long") { 
        sStyle += "/medium" 
       } 
       oTextBinding.parts[0].type = new DateTime({ 
        style: sStyle 
       }); 
       this.bindProperty("text", oTextBinding); 
       this._bInitialized = true; 
      } 
     } 
    }); 
    return DateTimeText; 
}, true); 
+0

感谢您的答案!我试过了,它正确地切割了字符串。不幸的是,它不能解决整个问题,因为应用程序跳转到格式化程序,但仍显示标准输出而不是字符串。我想格式化程序被调用来处理Text控件的输入字符串,当它的类型设置为DateTime时。是否有任何其他方式来访问最终的输出字符串(可能通过生命周期方法),以便它可以被操纵? –

+0

你想要** 2017年4月12日15:07:43 **而不是** 2017年4月12日15:07:43 GMT + 05:30 **。但我有一个问题,你可以在普通的** sap.m.Text **控件中获得这个控制。如果你可以,我会给你答案。 –

+0

我已经为您的问题更新了我的解决方案。我认为这个会帮助你。 –

相关问题