简体   繁体   English

GWT RadioButton分组

[英]GWT RadioButton Grouping

In my application, I have URN-identified data coming in from the server. 在我的应用程序中,我有来自服务器的URN识别数据。 I'm in the process of abstracting as far as possible so there is very little to no logical code in my views, and I'm using a generic presenter that wraps those views. 我正在尽可能抽象,因此我的视图中几乎没有逻辑代码,我正在使用包装这些视图的通用演示者。 All widgets have URNs, making it super easy to map incoming data to a specific widget (until now, a 1 to 1 relationship). 所有小部件都有URN,这使得将传入数据映射到特定小部件变得非常容易(直到现在,这是1对1的关系)。 This has worked well for pretty much every widget, and now I've reached a point where I'm tripped up. 这对于几乎每个小部件都很有效,现在我已经达到了我被绊倒的程度。

Assume I have (just for simplicity's sake) two RadioButton elements on a view. 假设我(为了简单起见)在视图上有两个RadioButton元素。 These buttons belong to a "group" (just by setting their name values to the same thing), but obviously they're 2 distinct elements. 这些按钮属于“组”(只需将它们的name值设置为相同的东西),但显然它们是2个不同的元素。 I can't map my URN-identified data to a single widget as in every other case because, in this case, it is two widgets. 我无法像其他情况一样将我的URN标识数据映射到单个小部件,因为在这种情况下,它是两个小部件。

Here's an example of what I mean: 这是我的意思的一个例子:

小样

Utility Company is a ListBox , so just one widget there. Utility Company是一个ListBox ,所以只有一个小部件。 I map each item in the list to a specific Enum value. 我将列表中的每个项目映射到特定的枚举值。

Utility Rate is a TextBox , so again just one widget to map. 效用率是一个TextBox ,因此只需要一个要映射的小部件。

For Energy Usage, they can select to use either an average for the year or input 12 monthly values. 对于能源使用,他们可以选择使用年度平均值或输入12个月值。 I'm stuck here. 我被困在这里 I can't map to just one of the RadioButton elements, because then I'd need some extra logic in the view to handle the behavior appropriately. 我无法映射到其中一个RadioButton元素,因为那时我需要在视图中使用一些额外的逻辑来适当地处理行为。

Am I stuck mapping to just one widget and sticking (unwanted) logic in my view to determine what the state of all of the elements should be based on the value that came in for the one widget that is mapped? 上午我坚持映射到只有一个小部件,并坚持(不想要的)逻辑在我看来,以确定所有元素的状态应该是基于这样排了所映射的一个插件的价值?

How should I handle this case? 我该如何处理这个案子?

Edit (Solution): Following the concepts of jusio's answer, I came up with a workable solution. 编辑(解决方案):按照jusio的回答概念,我提出了一个可行的解决方案。 Because I didn't want to go sticking special case handling through my logic to take care of a non-widget, I created a RadioButtonSet faux widget ( public class RadioButtonSet <T extends Enum<?> & HasDisplayText> extends Widget implements HasValueChangeHandlers<T>, HasValue<T> ), into which I manually pass the radios I intend to group. 因为我不想通过我的逻辑来处理特殊情况处理来处理非小部件,所以我创建了一个RadioButtonSet虚拟小部件( public class RadioButtonSet <T extends Enum<?> & HasDisplayText> extends Widget implements HasValueChangeHandlers<T>, HasValue<T> ),我手动传递我打算分组的无线电。 Having done that, I can get or set its value and have it fire appropriate events when the user changes the selection. 完成后,我可以获取或设置其值,并在用户更改选择时触发相应的事件。 Then mapping the collection of radios is no different than doing so for a listbox. 然后映射无线电集合与对列表框这样做没有什么不同。 Thanks jusio. 谢谢jusio。

I believe in your case you shouldn't treat radio buttons as two separate widgets, basically in your case you can treat the radio button group as combo box, because behavior is almost the same (the only problem is that you have additional master detail). 我相信在你的情况下,你不应该将单选按钮视为两个单独的小部件,基本上在你的情况下,你可以将单选按钮组视为组合框,因为行为几乎相同(唯一的问题是你有额外的主细节) 。 So basically what you will have to do is to wrap real BO objects into some kind of RadioButtonGroupModel, and give it to view, view can take this model and generate radio buttons (with some editors or whatever else). 所以基本上你要做的就是将真正的BO对象包装成某种RadioButtonGroupModel,并让它进行查看,查看可以采用这个模型并生成单选按钮(带有一些编辑器或其他任何东西)。 I remember running into this problem when i was extending databinding FW for JFace, and this was the best way I could find to solve this problem. 我记得当我为JFace扩展数据绑定FW时遇到这个问题,这是我找到解决这个问题的最好方法。

If I understood correctly the problem, there are 2 possible solutions: 如果我理解正确的问题,有两种可能的解决方案:

    • Give each RadioButton a unique URN (ex: oldURN_1 , oldURN_2) 为每个RadioButton提供唯一的URN(例如:oldURN_1,oldURN_2)
    • When you send data for a URN, disable the other one 发送URN的数据时,请禁用另一个
  1. Keep the same Name for each RadioButton but add a number variable in the data the server sends indicating which radioButton it is supposed to use (ex: 0 for Average and 1 for Monthly) 为每个RadioButton保留相同的名称,但在服务器发送的数据中添加一个数字变量,指示它应该使用哪个radioButton(例如: 0表示平均值, 1表示每月)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM