简体   繁体   English

Vaadin 7组合框 - 如何在键入文本时填充和下拉?

[英]Vaadin 7 Combo Box - how to populate and dropdown when text is typed?

I have an app that has editors for various database records. 我有一个应用程序,其中包含各种数据库记录的编辑器。 These records are referencing each other. 这些记录是相互引用的。 I'm developing a new custom data-aware component that is based on ComboBox: 我正在开发一个基于ComboBox的新的自定义数据感知组件:

public abstract class CustomDbLookupField<T> extends CustomField<T> implements Field<T> 

Here is what I want: 这就是我想要的:

  • the component looks like a combo box (I could do this easily) 该组件看起来像一个组合框(我可以轻松地做到这一点)
  • when the corresponding field has a row id value, then the component displays the textual representation of that referenced record (I could also do this) 当相应的字段有行id值时,组件显示该引用记录的文本表示(我也可以这样做)
  • when the user starts typing text in the combobox, then I want to search for keywords in the database, and populate the combobox with those items, and let then 当用户开始在组合框中键入文本时,我想在数据库中搜索关键字,并用这些项填充组合框,然后让
  • drop down the combo box for him and et him select from the results 为他下拉组合框,然后从结果中选择

I cannot seem to do the last two parts. 我似乎无法完成最后两部分。 I wanted to bind listener to the "text typed into combo box" event, but I cannot find any method for that. 我想将监听器绑定到“文本键入组合框”事件,但我找不到任何方法。 Moreover, I don't know how to get the text that was just typed in. I was trying this: 此外,我不知道如何获取刚刚输入的文本。我正在尝试这样做:

cmb.addListener(com.vaadin.event.FieldEvents.TextChangeEvent.class,target, method);

but I don't know what to pass for parameters target and method. 但我不知道参数target和method要传递什么。

That's a good question, and one that cannot be answered in a few sentences. 这是一个很好的问题,也是一个无法用几句话回答的问题。 I'll try to give you a short answer upfront and will try to elaborate more on this later. 我会先尝试给你一个简短的答案,然后再尝试详细说明。 I'm currently hacking a small example to demonstrate the feature you want to achieve. 我目前正在攻击一个小例子来演示你想要实现的功能。 But I'll need some more time. 但我需要更多时间。 I'll update my answer as soon as I have some results. 我会在得到一些结果后立即更新我的答案。

Until then, the short answer to your question is that if you register a listener for TextChangeEvents on a component, you'll need someone to actually fire these events. 在此之前,对您的问题的简短回答是,如果您在组件上注册TextChangeEvents的侦听器,则需要有人实际触发这些事件。 This typically happens on the client side, for example in a TextField . 这通常发生在客户端,例如在TextField When you derive from CustomField you still need to provide some client side implemenation. CustomField派生时,您仍需要提供一些客户端实现。 CustomField is only a base implementation for your custom field implementation without an awful lot of ready-to-go client side functionality. CustomField只是自定义字段实现的基础实现,没有大量现成的客户端功能。 So, in your case, the listener for TextChangeEvents will actually never be invoked. 因此,在您的情况下,实际上永远不会调用TextChangeEvents的侦听器。

You can do a lot easier by deriving from ComboBox. 通过从ComboBox派生,您可以轻松完成。 ComboBox already has built-in support for prefix filtering when typing in text into the ComboBox. 当在ComboBox中键入文本时,ComboBox已经内置了对前缀过滤的支持。 The actual filter functionality is implemented in the Container which is data source to the ComboBox. 实际的过滤器功能在Container中实现, Container是ComboBox的数据源。 The trick is to overwrite protected method ComboBox#getOptionsWithFilter(boolean) to filter in the database and use a custom Container implementation which provides the filtered items from the database. 诀窍是覆盖受保护的方法ComboBox#getOptionsWithFilter(boolean)以在数据库中过滤并使用自定义Container实现,该实现提供来自数据库的过滤项。

Update : As promised, I have written a small demo application which shows how you can write a custom ComboBox whose items are dynamically populated from the database as the user types in text. 更新 :正如所承诺的,我编写了一个小型演示应用程序,该应用程序显示了如何编写自定义ComboBox,当用户在文本中键入时,其项目将从数据库中动态填充。 This can be achieved with a simple hack by deriving from ComboBox and overwriting method com.vaadin.ui.ComboBox.buildFilter(String, FilteringMode) . 这可以通过从ComboBox派生并通过覆盖方法com.vaadin.ui.ComboBox.buildFilter(String, FilteringMode) The overridden method will need to return a custom Container.Filter implementation that simply carries over the current filter String to a custom com.vaadin.data.Container implementation. 重写的方法将需要返回一个自定义的Container.Filter实现,该实现只是将当前过滤器String转移到自定义的com.vaadin.data.Container实现。 The container is then responsible for fetching the filtered items from the database and populating itself with the query result. 然后,容器负责从数据库中提取过滤的项目并使用查询结果填充自身。 In my example, I derive from BeanItemContainer where I do the custom database filtering in the overridden method de.oio.vaadin.SuggestingContainer.addFilter(Filter) 在我的示例中,我派生自BeanItemContainer ,我在重写方法中执行自定义数据库筛选de.oio.vaadin.SuggestingContainer.addFilter(Filter)

You can check out the demo at my GitHub project . 您可以在我的GitHub项目中查看演示。 Don't hesitate to ask if there is anything unclear. 不要犹豫,询问是否有任何不清楚的事情。

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

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