表单上的 Vaadin 属性格式化程序
我有一个表单,其中源项目的属性需要通过自定义格式进行格式化。
源属性(我自己的 bean)是一个 Integer
,但需要格式化为类似货币的格式。
我尝试实现自己的 PropertyFormatter
,并将其设置在我的 FieldFactory.createField
中,用于此表单,但正如
TextField tf = new TextField("Price");
tf.setPropertyDataSource(new MyPriceFormatter());
return tf;
我从日志中看到的,只有 format()< /code> 方法被调用。但是
parse()
方法从未被使用,并且 setValue
从未被调用
我的代码有什么问题吗?如何为表单使用自定义PropertyFomatter?或者如何为表单字段添加自定义格式?
经过一番调查后,我发现有一些东西只是用新的 MethodProperty
数据源替换了我的格式化程序。因此,我实现了自己的 PriceField
,并重写了 setPropertyDataSource
,解决了这种情况。顺便说一句,这似乎很老套,我仍在寻找其他方法
I've a Form where an property of source Item need to be formatter by an custom format.
Source property (of my own bean) is a Integer
, but need to be formatted as a Currency-like format.
I tried to implement my own PropertyFormatter
, and setup it inside my FieldFactory.createField
for this form as
TextField tf = new TextField("Price");
tf.setPropertyDataSource(new MyPriceFormatter());
return tf;
But as I see from the logs, only format()
method is called. But parse()
method is never used, and setValue
is never called
What's wrong with my code? How to use custom PropertyFomatter for forms? Or how to add custom format for form's field?
After some investigation i found that there is something just replaces my formatter, with an new MethodProperty
data source. So i'd implemented my own PriceField
, with overrided setPropertyDataSource
, that fix this situation. btw, it seems to bee hacky, and i'm still looking for an other way
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也遇到过这个问题,并用另一种方式解决了。实际上,我还必须创建一个用货币格式化的文本字段:-)
问题是,当您在 FormFieldFactory 中创建字段时,PropertyFormatter 中的数据源为空。您可以在调用 FormFieldFactory 后在字段上设置数据源:
因此不幸的是,使用 Vaadin 您无法创建设置自己的格式化程序的 TextField。
I have also experienced this problem and solved it another way. Actually I also had to make a textfield formated with a currency :-)
The problem is that the datasource in the PropertyFormatter is null at the time you are creating the fields in the FormFieldFactory. You can instead set the datasource on your field after the FormFieldFactory has been called:
So unfortunately with Vaadin you cannot create a TextField that sets its own formatter.