如何使用 Vaadin 表格组件启用/禁用单元格?

发布于 2024-12-06 09:07:50 字数 1435 浏览 0 评论 0原文

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

@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;
}

谢谢

I have a table with 2 columns: a checkbox and a textfield. I want to disable the textfield depending of the respective (same row) checkbox status. If the checkbox is checked then the textfield will be cleared and be read only. Is this possible ? Here is my code:

@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;
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎言笑 2024-12-13 09:07:50

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

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

这里的更改:

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

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

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);
                }
            }
        });

希望它对你有用!

问候,埃里克

Few changes to your code made it possible.
Not the finiest way, but te simpliest.

First,you have to set your second column (Valor) to TextField.class not String.class.

Here the change :

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

Instead of keepin the variable i in the CheckBox.setData(), I suggest you to link your checkBox to the TextField of the same row, like this :

c.setData(t);

Finally I made little change to your listener :

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);
                }
            }
        });

Hope it's work for you!

Regards, Éric

半世蒼涼 2024-12-13 09:07:50
    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);
                    }
                }
            });


    }

 }
    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);
                    }
                }
            });


    }

 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文