FieldGroup可以直接绑定一个数据源DataSource。但如果想绑定某个值,并没有直接作为数据库中的一个字段存在。而是最后转为json串保存在数据库中。这样的话相当于key-value模式的DataSource,这里是ItemDataSource。

为FieldGroup绑定一个日期控件

//首先得在FieldGroup添加这个Item
PropertysetItem item = new PropertysetItem();
item.addItemProperty("{code_act_Date1}",new ObjectProperty(""));
FieldGroup fieldGroup = new FieldGroup(item); //然后再进行定义日期控件
WebGridLayout actGrid = componentsFactory.createComponent(WebGridLayout.class);
DateField dateField = new DateField();
dateField.setDateFormat("yyyy年MM月dd日"); //进行绑定
fieldGroup.bind(dateField, actElements.get(i).getValue()); //将日期控件添加到页面中
GridLayout tmpGgridLayout = (GridLayout) actGrid.getComponent();
tmpGgridLayout.addComponent(dateField);

PS:item.addItemProperty("{code_act_Date1}",new ObjectProperty(""));这句话为什么不是item.addItemProperty("{code_act_Date1}",new DateField());

参见:http://www.cnblogs.com/acm-bingzi/p/cubaFieldGroupDate.html

PPS:悲了个剧的,最后发现new ObjectProperty("")) 这种方法在某些服务器上会Date类型转换报错,所以这里还是应该使用 new DateField())

一种类型转换错误的情况ClassCastException

刚开始使用以下方式fieldGroup绑定

WebGridLayout actGrid = componentsFactory.createComponent(WebGridLayout.class);
WebDateField webDateField = componentsFactory.createComponent(WebDateField.class);
webDateField.setDateFormat("yyyy年MM月dd日");
fieldGroup.bind((DateField)webDateField.getComponent(), nameValueElements.get(i).getValue());
actGrid.add(webDateField);

报错:ClassCastException: com.haulmont.cuba.web.toolkit.ui.CubaDateFieldWrapper cannot be cast to com.vaadin.ui.DateField

这个错真是很奇怪,但是上面那种把WebGridLayout 转换成GridLayout 就没有问题

将FieldGroup中的数据转换成json串保存

//...
FieldGroup fieldGroup = new FieldGroup(item);
Map<String, Object> mapValues = new HashMap<>();
Collection<String> itemIds = (Collection<String>) item.getItemPropertyIds();
for (String itemId : itemIds) {
Object object = fieldGroup.getItemDataSource().getItemProperty(itemId).getValue();
String value = null;
if (object != null) {
value = fieldGroup.getItemDataSource().getItemProperty(itemId).getValue().toString();
}
mapValues.put(itemId, value);
} ObjectMapper mapper = new ObjectMapper();
//转JSON
String json = mapper.writeValueAsString(mapValues);

最后json的值的格式是:

  {"{code_10_13}":"123","{code_10_15}":"接地电阻值满足要求","{code_7_6}":"智能建筑-防雷与接地"}

再将json值保存到数据库中的一个字段

将json串中的数据转换到FieldGroup中

//简单写法:
//这个方法里的两个参数,第一个是(PropertysetItem)fieldGroup.getItemDataSource(),第二个是json值
void setSavedPropertyValues(PropertysetItem propertysetItem, String jsonValue) {
Map<String, Object> mapValues = getSavedValue(jsonValue);
if (mapValues != null) {
for (String key : mapValues.keySet()) {
if (propertysetItem.getItemProperty(key) != null) {
propertysetItem.getItemProperty(key).setValue(mapValues.get(key));
}
}
}
} //将json转换成ap类型
Map<String, Object> getSavedValue(String jsonValue) {
if (jsonValue == null || jsonValue.isEmpty()) {
return null;
}
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> mapValues = null;
try {
mapValues = objectMapper.readValue(jsonValue, Map.class);
} catch (IOException e) {
throw new IllegalArgumentException("已保持的值格式错误,无法解析成map对象");
}
return mapValues;
}

PS:如果最上边添加日期控件的时候,使用的是 item.addItemProperty("{code_act_Date1}",new DateField());

那么这段代码应该稍微增加一点,如下:

void setSavedPropertyValues(PropertysetItem propertysetItem, String jsonValue) {
Map<String, Object> mapValues = getSavedValue(jsonValue);
if (mapValues != null) {
for (String key : mapValues.keySet()) {
if (propertysetItem.getItemProperty(key) != null) {
if (propertysetItem.getItemProperty(key).getType() == Date.class) {
Date date = new Date((String) mapValues.get(key));
propertysetItem.getItemProperty(key).setValue(date);
} else {
propertysetItem.getItemProperty(key).setValue(mapValues.get(key));
}
}
}
}
}

FieldGroup绑定ItemDataSource的更多相关文章

  1. FieldGroup绑定的日期类型存储格式的问题

    问题 日期存储的时候,当前数据库中存储格式为 "2017-9-5 0:00:00", 而我实现了以后,看到数据库的存储格式为 "Mon Sep 04 00:00:00 C ...

  2. Windows Store App JavaScript 开发:模板绑定

    WinJS库模板提供了一种格式化显示多条数据的便捷方式,通过这种方式可以将模板与ListView或FlipView等控件结合使用以控制数据的显示格式.定义一个WinJS库模板的方法与定义WinJS库控 ...

  3. Win10系列:JavaScript 模板绑定

    WinJS库模板提供了一种格式化显示多条数据的便捷方式,通过这种方式可以将模板与ListView或FlipView等控件结合使用以控制数据的显示格式.定义一个WinJS库模板的方法与定义WinJS库控 ...

  4. ASP.NET Core MVC/WebAPi 模型绑定探索

    前言 相信一直关注我的园友都知道,我写的博文都没有特别枯燥理论性的东西,主要是当每开启一门新的技术之旅时,刚开始就直接去看底层实现原理,第一会感觉索然无味,第二也不明白到底为何要这样做,所以只有当你用 ...

  5. MVVM设计模式和WPF中的实现(四)事件绑定

    MVVM设计模式和在WPF中的实现(四) 事件绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  6. MVVM模式解析和在WPF中的实现(三)命令绑定

    MVVM模式解析和在WPF中的实现(三) 命令绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  7. 冒泡,setinterval,背景图的div绑定事件,匿名函数问题

    1.会冒泡到兄弟元素么? $(function(){ $("#a").click(function(){alert("a")}) $("#b" ...

  8. Xamarin+Prism开发详解二:Xaml文件如何简单绑定Resources资源文件内容

    我们知道在UWP里面有Resources文件xxx.resx,在Android里面有String.Xml文件等.那跨平台如何统一这些类别不一的资源文件以及Xaml设计文件如何绑定这些资源?应用支持多国 ...

  9. 数据的双向绑定 Angular JS

    接触AngularJS许了,时常问自己一些问题,如果是我实现它,会在哪些方面选择跟它相同的道路,哪些方面不同.为此,记录了一些思考,给自己回顾,也供他人参考. 初步大致有以下几个方面: 数据双向绑定 ...

随机推荐

  1. VS2017上执行VS2013项目错误MSB802之解决方案

    进行想把我编写的数字图像处理软件MagicHouse更新到最新的VS2017开发环境下,原来的开发环境是VS2013.但是用VS2017打开项目并编译时,系统报错误MSB802,如下图所示. 其实Vi ...

  2. SPOJ33&POJ1934 Trip LCS

    题目传送门:https://www.luogu.org/problemnew/show/SP33 题目大意:给出两个字符串,求其LCS(最长公共子序列)的长度与具体方案(相同的串算作同一方案).数据组 ...

  3. zookeepeer使用zkCli.sh命令

    一.连接服务器端 [root@sxl132 zookeepeer]# ./bin/zkCli. Connecting to -- ::, [myid:] - INFO [main:Environmen ...

  4. [Spark][Python]DataFrame select 操作例子

    [Spark][Python]DataFrame中取出有限个记录的例子 的 继续 In [4]: peopleDF.select("age")Out[4]: DataFrame[a ...

  5. Python 学习 第九篇:模块

    模块是把程序代码和数据封装的Python文件,也就是说,每一个以扩展名py结尾的Python源代码文件都是一个模块.每一个模块文件就是一个独立的命名空间,用于封装顶层变量名:在一个模块文件的顶层定义的 ...

  6. Flutter - 创建自适应的Android app 图标

    上一篇文章说到  Flutter - 自动生成Android & iOS图标 通过flutter_launcher_icons 可以一键生成所有的Icon 到此基本什么问题也没有,如果你用io ...

  7. copy constructor

    copy constructor也分为trivial和nontrivial两种 如果class展现出bitwise copy semantics(按位拷贝语义),则不会构造出 copy constru ...

  8. Java各厂对外的优质博客

    1.美团:https://tech.meituan.com/ 2.极客学院:http://wiki.jikexueyuan.com/list/java/

  9. js控制css时注意

    font-size:10px--------e.style.fontSize="10px " 属性名:font-size--------fontSize; 属性值:10px---- ...

  10. Dijkstra及其堆优化

    朴素Dijkstra #include<bits/stdc++.h> using namespace std; const int inf=9999999; bool book[105]; ...