将日期时间值输出到 SmartGWT 中的 StaticTextField
我的数据集中有一个字段定义为:
DataSourceDateTimeField dateField = new DataSourceDateTimeField("date");
DynamicForm
中的 StaticTextItem
定义为
StaticTextItem dateItem = new StaticTextItem("date", "Date");
我经常使用 setDateFormatter
的不同组合,但日期时间值仍然呈现为类似 2011-08-23T20:00:00
(数据源以 json 形式接收它yyyy-MM-dd'T'HH:mm:ss.SSSZ
字段)。
有没有一些简单的方法可以将日期时间值输出到StaticTextItem
?我假设使用DynamicForm.fetchData()
。
UPD1。数据示例。
PgSQL 中的表行:
1 | "2011-09-29 12:10:05.010276+04" | "Europe"
REST 服务发送的数据:
{
"location":"Europe",
"id":1,
"date":"2011-09-29T08:10:05.010+0000"
}
SGWT 从 REST 服务获取的数据(我使用 JSON.encode(XMLTools.selectObjects(data, "/").getJavaScriptObject())
转储它我的 transformResponse()
) 实现:
{
"location":"Europe",
"id":1,
"date":"2011-09-29T08:10:05"
}
StaticTextField 中呈现的值:
2011-09-29T08:10:05
因此服务器返回的日期时间值似乎符合 标准 而且我在开发者控制台中没有任何警告。
UPD2。也许我在 transformResponse()
中做错了什么?
protected void transformResponse(DSResponse response, DSRequest request, Object data) {
String json = JSON.encode(XMLTools.selectObjects(data, "/").getJavaScriptObject());
SC.logWarn("Response received");
SC.logWarn(json);
Record[] records = jsonToRecords(json);
//safe HTML text values
for (Record rec: records) {
for (DataSourceField field: getFields()) {
if (field.getType() == FieldType.TEXT) {
String textVal = rec.getAttribute(field.getName());
if (textVal != null) {
textVal = SafeHtmlUtils.htmlEscape(textVal);
}
rec.setAttribute(field.getName(), textVal);
}
}
}
response.setData(records);
response.setStartRow(0);
response.setEndRow(records.length);
response.setTotalRows(records.length);
}
/**
* Converts JS-array into SmartGWT records
*/
public static native ListGridRecord[] jsonToRecords(String jsonString) /*-{
var json = eval(jsonString);
return @com.smartgwt.client.widgets.grid.ListGrid::convertToListGridRecordArray(Lcom/google/gwt/core/client/JavaScriptObject;)(json);
}-*/;
I have a field in my dataset defined as:
DataSourceDateTimeField dateField = new DataSourceDateTimeField("date");
and a StaticTextItem
in DynamicForm
defined as
StaticTextItem dateItem = new StaticTextItem("date", "Date");
I played a lot with different combinations of setDateFormatter
, but datetime values are still rendered as something like 2011-08-23T20:00:00
(datasource receives it in json as a yyyy-MM-dd'T'HH:mm:ss.SSSZ
field).
Is there some easy way to output datetime values to StaticTextItem
? I assume using DynamicForm.fetchData()
.
UPD1. Data example.
Table row in PgSQL:
1 | "2011-09-29 12:10:05.010276+04" | "Europe"
Data sent by REST service:
{
"location":"Europe",
"id":1,
"date":"2011-09-29T08:10:05.010+0000"
}
Data fetched by SGWT from REST service (I dumped it with JSON.encode(XMLTools.selectObjects(data, "/").getJavaScriptObject())
in my implementation of transformResponse()
) :
{
"location":"Europe",
"id":1,
"date":"2011-09-29T08:10:05"
}
Value as rendered in StaticTextField:
2011-09-29T08:10:05
So datetime values returned by server seem to conform the standard and also I have no warnings in Developer Console.
UPD2. May be I'm doing something wrong in my transformResponse()
?
protected void transformResponse(DSResponse response, DSRequest request, Object data) {
String json = JSON.encode(XMLTools.selectObjects(data, "/").getJavaScriptObject());
SC.logWarn("Response received");
SC.logWarn(json);
Record[] records = jsonToRecords(json);
//safe HTML text values
for (Record rec: records) {
for (DataSourceField field: getFields()) {
if (field.getType() == FieldType.TEXT) {
String textVal = rec.getAttribute(field.getName());
if (textVal != null) {
textVal = SafeHtmlUtils.htmlEscape(textVal);
}
rec.setAttribute(field.getName(), textVal);
}
}
}
response.setData(records);
response.setStartRow(0);
response.setEndRow(records.length);
response.setTotalRows(records.length);
}
/**
* Converts JS-array into SmartGWT records
*/
public static native ListGridRecord[] jsonToRecords(String jsonString) /*-{
var json = eval(jsonString);
return @com.smartgwt.client.widgets.grid.ListGrid::convertToListGridRecordArray(Lcom/google/gwt/core/client/JavaScriptObject;)(json);
}-*/;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题很可能是您的日期值仍然是字符串,而不是日期的实例。当收到 JSON 响应时,它应该已被解析为日期 - 如果不是,SmartGWT 开发人员控制台中会出现警告,告诉您这一点。
默认情况下,DataSource.dataFormat - XML 架构日期/日期时间格式中记录的日期值的预期格式是预期的,如果您无法使服务器生成此格式,您可以提供 FieldValueParser。
Your problem is most likely that your date value is still a String, not an instance of Date. It should have been parsed into a Date when the JSON response was received - if it was not, there will be a warning in the SmartGWT Developer Console tell you so.
The format expected for date values in documented under DataSource.dataFormat - XML Schema date/datetime format is expected by default, and you can provide a FieldValueParser if you are unable to make your server produce this format.