【问题标题】:How can I enable/disable cells using Vaadin table component?如何使用 Vaadin 表格组件启用/禁用单元格?
【发布时间】:2014-08-12 02:48:34
【问题描述】:

我有一个包含 2 列的表格:一个复选框和一个文本字段。我想根据相应的(同一行)复选框状态禁用文本字段。如果选中该复选框,则文本字段将被清除并且是只读的。这可能吗 ?这是我的代码:

@SuppressWarnings("serial")
private Table filtersTable() {
    final Table table = new Table();
    table.setPageLength(10);
    table.setSelectable(false);
    table.setImmediate(true);
    table.setSizeFull();
    // table.setMultiSelectMode(MultiSelectMode.SIMPLE) ;
    table.addContainerProperty("Tipo filtro", CheckBox.class, null);
    table.addContainerProperty("Valor", String.class, null);
    table.setEditable(true);
    for (int i = 0; i < 15; i++) {
        TextField t = new TextField();
        t.setData(i);
        t.setMaxLength(50);
        t.setValue("valor " + i);
        t.setImmediate(true);
        t.setWidth(30, UNITS_PERCENTAGE);
        CheckBox c = new CheckBox(" filtro " + i);
        c.setWidth(30, UNITS_PERCENTAGE);
        c.setData(i);
        c.setImmediate(true);
        c.addListener(new ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
              // within this, could I access the respective row ID
              // (i) then enable/disable TextField t on second column ?
              System.out.println("event.getProperty().getValue()="
                        + event.getProperty().getValue());
            }
        });
        table.addItem(new Object[] { c, t }, i);
    }
    return table;
}

谢谢

【问题讨论】:

    标签: vaadin


    【解决方案1】:

    对您的代码进行的少量更改使其成为可能。 不是最好的方法,而是最简单的方法。

    首先,您必须将第二列 (Valor) 设置为 TextField.class 而不是 String.class

    这里的变化:

    table.addContainerProperty("Valor", TextField.class, null);
    

    我建议您不要将变量 i 保留在 CheckBox.setData() 中,而是将您的复选框链接到同一行的 TextField,如下所示:

    c.setData(t);
    

    最后我对你的听众做了一点改动:

    c.addListener(new Property.ValueChangeListener() {
                public void valueChange(ValueChangeEvent event) {
    
                    CheckBox checkBox = (CheckBox)event.getProperty();
                    if((Boolean) checkBox.getValue())
                    {
                        TextField associatedTextField = (TextField)checkBox.getData();
    
                        //Do all your stuff with the TextField
                        associatedTextField.setReadOnly(true);
                    }
                }
            });
    

    希望它对你有用!

    问候,埃里克

    【讨论】:

      【解决方案2】:
          public class MyCheckBox extends CheckBox {
      
          private TextBox t;
      
          public MyCheckBox(TextBox t) {
      
           this.t = t;
          attachLsnr();
          }
      
          private void attachLsnr()
          {
           addListener(new Property.ValueChangeListener() {
                      public void valueChange(ValueChangeEvent event) {
      
                          CheckBox checkBox = (CheckBox)event.getProperty();
                          if((Boolean) checkBox.getValue())
                          {
                              t.setReadOnly(true);
                          }
                      }
                  });
      
      
          }
      
       }
      

      【讨论】:

      • 请避免只发布代码,而是解释您发布的代码,以便 OP 了解您所做的事情,并且该网站不会变成“请为我编写代码”网站。
      猜你喜欢
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多