当前位置: 首页 > 知识库问答 >
问题:

取消vaadin网格中的一行vaadin 8

董翰墨
2023-03-14
Grid<ErweitertePdbSegmentInkassoTyp> inkassoGrid = new Grid<>();
inkassoGrid.setSelectionMod(SelectionMode.SINGLE);
inkassoGrid.addItemClickListener(event -> {
    event.getItem();
}); 

共有1个答案

冯沛
2023-03-14

使用Vaadin 8.5.1和这个问题作为起点,您可以使用选择监听器来完成您想要的任务:

  • 用户选择联系人时,将显示一个显示详细信息的表单。
  • 更改联系人时,显示的详细信息将更新
  • 如果未选择联系人,则隐藏表单

这种魔力发生在creategrid方法中,更具体地说是在值更改侦听器中:

    contactGrid.asSingleSelect().addValueChangeListener(event -> {
        // show or hide the form depending on the selection
        contactForm.setVisible(event.getValue() != null);
        // update displayed data
        contactForm.setClient(event.getValue());
    });
import com.vaadin.data.Binder;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.ValoTheme;

import java.util.Arrays;
import java.util.List;

public class GridWithDetailsForm extends HorizontalLayout {
    /**
     * Simulate some data
     */
    private List<Client> clients = Arrays.asList(
            new Client("Sernie", "A", "123 Test St.", "test@gmail.gov", "555-555-5554"),
            new Client("Ernie", "B", "123 Test St.", "test@gmail.com", "555-555-5555"),
            new Client("Bernie", "C", "123 Test St.", "test@gmail.net", "555-555-5556"),
            new Client("Ayy", "Lmao", "123 Test St.", "test@gmail.org", "555-555-5557"),
            new Client("Dax", "E", "123 Test St.", "test@gmail.net", "555-555-5558"),
            new Client("Avorion", "F", "123 Test St.", "test@gmail.net", "555-555-5559"),
            new Client("Xanion", "G", "123 Test St.", "test@gmail.net", "555-555-5560"),
            new Client("Trinium", "H", "123 Test St.", "test@gmail.net", "555-555-5561"),
            new Client("Naonite", "I", "123 Test St.", "test@gmail.net", "555-555-5562"),
            new Client("Squillium", "J", "123 Test St.", "test@gmail.net", "555-555-5563"),
            new Client("Picard", "K", "123 Test St.", "test@gmail.net", "555-555-5564"),
            new Client("Richard", "L", "123 Test St.", "test@gmail.net", "555-555-5565"),
            new Client("Rickard", "M", "123 Test St.", "test@gmail.net", "555-555-5566"),
            new Client("Bobby", "N", "123 Test St.", "test@gmail.net", "555-555-5567"),
            new Client("Bob", "O", "123 Test St.", "test@gmail.net", "555-555-5568"),
            new Client("Ron", "P", "123 Test St.", "test@gmail.net", "555-555-5569"),
            new Client("Bill", "Q", "123 Test St.", "test@gmail.net", "555-555-5570"),
            new Client("Greg", "R", "123 Test St.", "test@gmail.net", "555-555-5571"),
            new Client("Juan", "S", "123 Test St.", "test@gmail.net", "555-555-5572"),
            new Client("Squidward", "T", "123 Test St.", "test@gmail.net", "555-555-5573"));


    public GridWithDetailsForm() {
        ContactForm contactForm = createContactForm();
        VerticalLayout actionBarAndGrid = createActionBarAndGrid(contactForm);
        addComponents(actionBarAndGrid, contactForm);
    }

    private VerticalLayout createActionBarAndGrid(ContactForm contactForm) {
        HorizontalLayout actionBar = createTopBar(contactForm);
        Grid<Client> contactGrid = createGrid(contactForm);

        VerticalLayout actionBadAndGrid = new VerticalLayout(actionBar, contactGrid);
        actionBadAndGrid.setSizeFull();
        actionBadAndGrid.setExpandRatio(contactGrid, 1);
        return actionBadAndGrid;
    }

    private ContactForm createContactForm() {
        ContactForm contactForm = new ContactForm();
        contactForm.setVisible(false);
        return contactForm;
    }

    private HorizontalLayout createTopBar(ContactForm contactForm) {
        TextField filter = new TextField();
        filter.setPlaceholder("Search clients...");

        Button addContactButton = new Button("New Client");
        addContactButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
        addContactButton.addClickListener(event -> {
            contactForm.setVisible(true);
            contactForm.setClient(new Client());
        });

        HorizontalLayout actionBar = new HorizontalLayout(filter, addContactButton);
        actionBar.setWidth("100%");
        filter.setWidth("100%");
        actionBar.setExpandRatio(filter, 1);
        return actionBar;
    }

    private Grid<Client> createGrid(ContactForm contactForm) {
        Grid<Client> contactGrid = new Grid<>(Client.class);
        contactGrid.setSelectionMode(Grid.SelectionMode.SINGLE);
        contactGrid.setColumns("firstName", "lastName");
        contactGrid.setItems(clients);

        contactGrid.asSingleSelect().addValueChangeListener(event -> {
            // show or hide the form depending on the selection
            contactForm.setVisible(event.getValue() != null);
            // update displayed data
            contactForm.setClient(event.getValue());
        });
        return contactGrid;
    }

    /**
     * Contact details form
     */
    public static class ContactForm extends FormLayout {
        private Button save, delete, cancel;
        private TextField firstName, lastName, email, address, phoneNumber;
        private Binder<Client> binder = new Binder<>(Client.class);

        public ContactForm() {
            save = new Button("Save");
            delete = new Button("Delete");
            cancel = new Button("Cancel");

            firstName = new TextField();
            firstName.setPlaceholder("First Name");

            lastName = new TextField();
            lastName.setPlaceholder("Last Name");

            email = new TextField();
            email.setPlaceholder("Email Address");

            address = new TextField();
            address.setPlaceholder("Address");

            phoneNumber = new TextField();
            phoneNumber.setPlaceholder("Phone Number");

            HorizontalLayout actions = new HorizontalLayout(save, delete, cancel);
            actions.setSpacing(true);

            addComponents(actions, firstName, lastName, email, address, phoneNumber);
            setSizeUndefined();
            binder.bindInstanceFields(this);
        }

        public void setClient(Client client) {
            binder.setBean(client);
        }
    }

    public static class Client {
        private String firstName;
        private String lastName;
        private String address;
        private String email;
        private String phoneNumber;

        public Client(String firstName, String lastName, String address, String email, String phoneNumber) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.address = address;
            this.email = email;
            this.phoneNumber = phoneNumber;
        }

        public Client() {

        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public String getAddress() {
            return address;
        }

        public String getEmail() {
            return email;
        }

        public String getPhoneNumber() {
            return phoneNumber;
        }
    }
}
 类似资料:
  • 我正在使用vaadin制作一个CRUD应用程序我有一个带有对象人员(id,姓名,年龄)的网格,我在每行旁边制作了一个“编辑”按钮,但是当我按下按钮时,我如何获得该行的Perg值?

  • 问题内容: Vaadin 7中的和组件有什么区别? 我应该使用哪个,什么时候使用? 问题答案: Grid是一个功能更强大的新组件,它应该是Table的后继组件(请参阅Table已死,Grid长寿)。因此,应该没有任何必要偏爱表格而不是网格。 这是Vaadin从表格迁移到网格的一系列文章的第一篇:https : //vaadin.com/blog/-/blogs/mission-rip- table

  • 问题内容: 我用数据库中的数据创建简单的网格: 要编辑每一行,请创建按钮: 这会打开带有编辑表单的新窗口。接受所有更改后,我必须手动刷新整个页面才能看到Grid上的修改。我的问题是: 修改任何行条目后如何只刷新Grid?以及如何将那些修改保存到数据库中(也许beanItemContainer可以做到)? 问题答案: 这是一个错误。在基础容器中进行更改后,Grid不会更新自身,也没有任何合理的刷新方

  • 我是瓦丁开发公司的新人,我希望有人能帮助我。我刚刚用模型创建了一个网格表,一切都很好。但是现在,我想改变选定行的背景颜色。我想,我必须创造一个主题。我在瓦丁论坛上发现了这个:https://vaadin.com/forum/thread/17867059/how-to-set-selected-row-opacity-in-vaadin-grid 这是我已经做过的: 我用链接中的代码创建了一个ht

  • 但是,鼠标中键不是由Vaadin注册的。我怎么才能让这个起作用?