【问题标题】:How to refresh Vaadin table related with SQLContainer using ComboBox?如何使用 ComboBox 刷新与 SQLContainer 相关的 Vaadin 表?
【发布时间】:2012-06-30 20:29:35
【问题描述】:

我需要一个基于 Vaadin 的简单应用程序的帮助。

我需要一个绑定到 SQL 查询结果的表。 SQL 查询具有用户从组合框中选择的值的参数。我需要的是在用户更改组合框值时刷新表格。

这就是我所拥有的():

 Table table;
 JDBCConnectionPool pool;
 String query = "select products.product_code, products.name as product_name, clients.client_code,  clients.name as client_name from products, clients where products.client_id = clients.id";

 FreeformQuery q = new FreeformQuery(query, pool);
 SQLContainer container = new SQLContainer(q);
 table.setContainerDataSource(container);

因此,这个简单的代码从产品和客户表中选择所有数据并将其放入表中。但是,例如,如何通过从组合框中选择的 clients.client_id 添加过滤?实现下一个查询:

 select products.product_code, products.name as product_name, clients.client_code,  clients.name as client_name from products, clients where products.client_id = clients.id where client_id = ?;

感谢您的帮助。

【问题讨论】:

    标签: java sql data-binding vaadin


    【解决方案1】:

    您可以添加一个Property.ValueChangeListener 来更改您的查询参数:

    comboBox.addListener(new Property.ValueChangeListener() {
        public void valueChange(ValueChangeEvent event) {
            String customQuery = query.replace(":clientId", ((Client)(event.getProperty()).getId(), pks);
            table.setContainerDataSource(new SQLContainer(new FreeformQuery(customQuery, pool)));
        }
    });
    

    query 将保留以下值:select products.product_code, products.name as product_name, clients.client_code, clients.name as client_name from products, clients where products.client_id = clients.id where client_id = :clientId

    但是要小心query.replace,如果Id是int就没什么好担心的,但是如果是字符串,请addSlashes避免SQLInjection。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 2013-02-24
      相关资源
      最近更新 更多