瓦丁组合框
我想创建 Vaadin 下拉菜单,其中有 2 个分隔符。我找不到实现该方法的方法,任何人都可以帮助我解决这个问题吗?
这就是我想要显示下拉列表的方式:
- 选项 1
- 选项 2
- ------------;
- 选择1
- 选择2
- ------------;
- 第 1 组
我该怎么做?
I want to create Vaadin drop down with 2 separators in it. I couldn't find a way to implement that, can anyone help me to solve this issue?
This is the way I want to display my drop down:
- Option 1
- Option 2
- ------------;
- select 1
- select 2
- -----------;
- group 1
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
没有内置方法可以向选择添加分隔符。我能想到的唯一方法是添加一个带有所需分隔符作为标题的项目。例如,如果您使用默认标题(项目 ID)
select.addItem("-----");
就足够了。这应该适用于 ComboBox 和 NativeSelect。There is no built-in way to add separators to selects. The only way I can think of is to add an item with the desired separator as its caption. For example if you use the default caption (item id)
select.addItem("-----");
should be enough. This should work for both ComboBoxes and NativeSelects.您可以实现一个新的 Vaadin 组件,包括客户端行为,但这不是一个简单的解决方案。此页面https://vaadin.com/book/-/page/gwt.html 可以为此提供帮助。
此外,使用现有组件创建您自己的组件是另一种解决方案。您可以实现一个特殊的组合框,它采用字符串或组件数组的值。实现此目的的方法是使用具有大小和位置的 Vaadin 面板、布局和窗口以及单击侦听器。
You can implement a new Vaadin component including the client behaviour, but this is not an easy solution. This page https://vaadin.com/book/-/page/gwt.html of "Book of Vaadin" and Vaadin forum can help for that.
Also, creating your own component using existing components is another solution. You can implement a special combobox which takes values of String or Component arrays. The way of doing this is using Vaadin panels, layouts and windows with size and locations and click listeners.
我自己还没有尝试过,但可以尝试一下 NativeSelection 下拉菜单。
I haven't tried it myself but give a go at NativeSelection dropdown.
你总是可以做
{select.addItem("-----");}
有一次我也想做类似的事情,但 Vaadin 没有正确的方法来做到这一点。我实际上创建了一个 Vaadin 小部件来扩展组合框。在 Vaadin 的客户端小部件中,他们在将项目添加到列表之前过滤掉 HTML 内容。因此,使用客户端代码,我覆盖该功能并使用 HTML 标记“
”来添加该行。
You can always do
{select.addItem("-----");}
Once I also wanted a do something like that but there was no proper way to do that with Vaadin. I actually created a Vaadin widget extending the Combo Box. In the client side widget of Vaadin they filter out the HTML content before adding items to the list. So Using the client side code I override that functionality and use HTML tag "
" to add the line.
看起来是最好的方法,我不知道其他一些
顺便说一句,如果您正在从某个列表中读取项目,您可以将其与一些项目计数器和
(itemsCount%n)==0
运算符结合起来,以在之后设置分隔符已插入“n”项:)looks like the best way, I dont know about some other
Btw if you are reading items from some list you can combine that with some item counter and
(itemsCount%n)==0
operator to set separator after 'n' items inserted :)您可以将项目添加到所选内容(如前所述),然后使用一些 javascript 禁用分隔符:
将项目添加到选择内容。
cb.addItem("分隔符");
cb.setItemCaption("separator", "-------------");
执行 JavaScript
最终字符串 javascript = ""
Page.getCurrent().getJavaScript().execute(javascript);
You can add the item to the selected (as mentioned before) and then disable the separators with some javascript:
add the item to the select.
cb.addItem("separator");
cb.setItemCaption("separator", "-------------");
execute the javascript
final String javascript = ""
Page.getCurrent().getJavaScript().execute(javascript);
您使用 ComboBox 而不是 Select 是否有原因,因为通过 select 您可以做到这一点。
或者您可以使用菜单栏,但它看起来与组合框非常不同。
Is there a reason that you use the ComboBox instead of the Select, because with the select you can do that.
Or you can use a MenuBar but it looks very diferent that the ComboBox.