简体   繁体   English

如何在选择模式“属性”中使用多个ID

[英]How to use multiple IDs in selection mode “Property”

I've a selection component (a combobox to be specific) and have added a SQLContainer as the ContainerDataSource. 我有一个选择组件(具体来说是一个组合框),并添加了一个SQLContainer作为ContainerDataSource。

I've set the ItemCaption via .setItemCaptionPropertyId("myID") . 我已经通过.setItemCaptionPropertyId("myID")设置了ItemCaption。 However, I need to use two properties as the caption. 但是,我需要使用两个属性作为标题。

Let's say that the properties with the ID "myID" represent a string like "foo". 假设ID为“ myID”的属性表示类似“ foo”的字符串。 There's also another property called "myCodeID" which represents a number like "23". 还有另一个名为“ myCodeID”的属性,它表示诸如“ 23”的数字。

How am I able to let my ComboBox show its item's caption as "23 foo"? 如何使ComboBox的项目标题显示为“ 23 foo”?

I'm searching for something like .setItemCaptionPropertyIds("myId", "myCodeID") . 我正在搜索.setItemCaptionPropertyIds("myId", "myCodeID")

I believe there are several approaches that you could use, but at least the following 2 work: 我相信您可以使用几种方法,但至少可以进行以下两项工作:

1) Quick and dirty 1)快速又

How about using a "fake property", meaning that you use a property which does not physically exist on the object, there's a getter with that name. 如何使用“伪属性”(fake property),这意味着您使用的属性在对象上实际上并不存在,有一个具有该名称的吸气剂。 Since Vaadin will also look for getters/setters when determining the item properties, it will find it and use it for your caption. 由于Vaadin在确定项目属性时还会寻找吸气剂/定型剂,因此它将找到并用作标题。

I know it's not the most elegant having to modify your model class, but it gets you there. 我知道修改模型类并不是最优雅的方法,但是它可以带您到那里。 Additionally depending on your implementation, you could maybe decorate your Person with a PersonCaptionGenerator which contains the getCaption() method to keep things a bit separate. 另外,根据您的实现,您可以用PersonCaptionGenerator装饰Person ,其中包含getCaption()方法以使事情保持分离。

Suppose you have a bean like: 假设您有一个像这样的bean:

public static class Person {
        private String name, surname;
        private int age;

        public Person(String name, String surname, int age) {
            this.name = name;
            this.surname = surname;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public String getSurname() {
            return surname;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setSurname(String surname) {
            this.surname = surname;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getCaption() {
            // getter to be used as caption
            return name + " " + surname;
        }
    }

Then you could write something like: 然后,您可以编写如下内容:

public class ComboBoxComponent extends VerticalLayout {
    public ComboBoxComponent() {
        BeanItemContainer<Person> dataSource = new BeanItemContainer<>(HasCaption.class);

        ComboBox comboBox = new ComboBox();
        comboBox.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY);

        // use a fake property which will get identified by the getter
        comboBox.setItemCaptionPropertyId("caption");
        addComponent(comboBox);

        comboBox.setContainerDataSource(dataSource);
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            dataSource.addBean(new Person("Name " + i, "Surname " + i, random.nextInt(99) + 1));
        }
    }
}

2) Extend the combo and add a caption generator 2)扩展组合并添加字幕生成器

If you can't modify the model and have a somewhat generic and reusable solution, you can extend the ComboBox and override the getItemCaption() method. 如果您无法修改模型并拥有某种通用且可重用的解决方案,则可以扩展ComboBox并覆盖getItemCaption()方法。 Please note that I've used a BeanItemContainer because it's easier since it holds the bean itself as the item ID, but it can probably be adjusted if a different container is needed. 请注意,我使用了BeanItemContainer因为它更容易使用,因为它将bean本身保留为项目ID,但是如果需要其他容器,则可以对其进行调整。

Starting with the same Person bean, but without the getter for the fake property. 与同一起跑线Person豆,但没有消气的属性。

Generic contract: 通用合同:

public interface CaptionComposer<T> {
    String getCaption(T item);
}

Implementation for our Person bean: 我们的Person Bean的实现:

private class PersonCaptionGenerator implements CaptionComposer<Person> {
    @Override
    public String getCaption(Person person) {
        return person.getName() + " " + person.getSurname();
    }
}

Custom combobox which defers the caption retrieval to the generator: 自定义组合框,将标题检索推迟到生成器:

public static class MyComboBox<T> extends ComboBox {
    private CaptionComposer captionComposer;

    public MyComboBox(CaptionComposer<T> captionGenerator, BeanItemContainer<T> dataSource) {
        this.captionComposer = captionGenerator;
        setContainerDataSource(dataSource);
    }

    @Override
    public String getItemCaption(Object itemId) {
        return captionComposer.getCaption(itemId);
    }
}

And lastly, add it to the UI: 最后,将其添加到UI:

public class ComboBoxComponent extends VerticalLayout {
    public ComboBoxComponent() {
        BeanItemContainer<Person> dataSource = new BeanItemContainer<>(Person.class);

        addComponent(new MyComboBox<>(new PersonCaptionGenerator(), dataSource));

        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            dataSource.addBean(new Person("Name " + i, "Surname " + i, random.nextInt(99) + 1));
        }
    }
}

In the end both will get you: 最终,两者都会使您:

结果

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

相关问题 限制在 html 中选择表格内的多个单元格,如何为表格使用用户选择属性 - Restrict selection of multiple cells inside table in html, how to use user-select property for a table 如何使用多线程处理多个ID - how to use multithreading to process multiple ids 如何启用ListView的多重选择模式? - How do I enable multiple selection mode is ListView? 选择模式单个和多个相同的DataTable - selection mode single and multiple for same DataTable Android - Java - MediaStore - 如何针对多个条件使用选择 - Android - Java - MediaStore - How to use selection for multiple conditions 如何使用数组在Java中的一行中设置多个选择条件? - How to use an array to set multiple selection critieria in one line in java? 如何使用选择来验证 java class 中的多个值? - How to use selection to validate multiple values in java class? 如何使用JCheckBoxes选择? - How to use JCheckBoxes selection for use? 如果在多个属性文件中定义了一个属性,Spring 如何选择要使用的属性值? - How does Spring pick the property value to use if a property is defined in multiple property files? 如何获取多个膨胀视图的ID? - How to get ids of multiple inflated views?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM