Vaadin - 如何将具有布尔值的Grid列转换为Checkbox

时间:2016-03-18 12:52:46

标签: checkbox vaadin

我正在使用Vaadin 7.6.4进行UI工作。具有以下内容: -

  • 我有一个窗口,其中包含一个包含数据的网格。这个窗口实际上是一种弹出窗口[当我的主屏幕点击设置图标时显示(此处未显示)。这工作正常(当点击主屏幕的设置图标时,让UI屏幕打开Vaadin窗口。)
  • 问题在于显示如下所述的数据。
  • 此网格将有4列,数据来自bean容器。
  • 第一列是一个布尔字段,根据bean容器中的数据显示true / false。
  • 我需要将此布尔字段列转换为一个复选框,其中true表示该字段为选中值的复选框。如果值为false,则显示未选中的复选框。
  • 我正在尝试这样做,如下面的代码所示,但我一直打印这个复选框名称。我没有看到复选框,但是那里印有“复选框”字样?
  • 此复选框应该是可编辑的。这个想法是用户应该能够选择一些复选框,所选的复选框应该显示在面板中(这里未显示)。因此,复选框必须是可编辑的。

我该如何解决这个问题?窗口的代码如下所示

package com.platform28.mybatis;

import java.util.List;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.util.GeneratedPropertyContainer;
import com.vaadin.data.util.PropertyValueGenerator;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Grid;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
@SuppressWarnings("serial")
public class ConfigPopUp extends Window {
VaadinUtils vaadinUtils = null;

public ConfigPopUp(List<TableColumnData> tblDataLst) {
    vaadinUtils = new VaadinUtils();

    // Some basic content for the window
    VerticalLayout configLayout = new VerticalLayout();
    configLayout.addComponent(new Label("Settings"));
    configLayout.setMargin(true);
    //configLayout.setWidth(null);;
    setContent(configLayout);
    //adding grid.
    List<SettingsColumnData> settingsList = vaadinUtils.processSettingsList(tblDataLst);
    final BeanItemContainer<SettingsColumnData> gridDataSource = new BeanItemContainer<SettingsColumnData>(SettingsColumnData.class, settingsList);

    //change boolean value to checkbox.
    GeneratedPropertyContainer gp = new GeneratedPropertyContainer(gridDataSource);
    gp.addGeneratedProperty("columnDisplayed", new PropertyValueGenerator<CheckBox>() {

        @Override
        public CheckBox getValue(Item item, Object itemId, Object propertyId) {
            boolean currentCheckBoxValue = (boolean) item.getItemProperty("columnDisplayed").getValue();
            CheckBox chkBox = new CheckBox();
            chkBox.setValue(currentCheckBoxValue);
            return chkBox;
        }

        @Override
        public Class<CheckBox> getType() {
            return CheckBox.class;
        }
    });



    Grid settingsGrid = new Grid(gp);
    settingsGrid.setWidth("100%");
    settingsGrid.setSizeFull();
    settingsGrid.setColumnOrder("columnDisplayed", "columnName","columnShortName","columnDescription");
    configLayout.addComponent(settingsGrid);
    //configLayout.setExpandRatio(settingsGrid, 1);

    // Disable the close button
    setClosable(false);

    HorizontalLayout hLayout = new HorizontalLayout();
    hLayout.setSpacing(true);
    hLayout.setMargin(true);

    // Trivial logic for closing the sub-window
    Button ok = new Button("Ok");
    ok.addClickListener(new ClickListener() {
        public void buttonClick(ClickEvent event) {
            close(); // Close the sub-window
        }
    });
    hLayout.addComponent(ok);

    // Trivial logic for closing the sub-window
    Button cancelBtn = new Button("Cancel");
    cancelBtn.addClickListener(new ClickListener() {
        public void buttonClick(ClickEvent event) {
            close(); // Close the sub-window
        }
    });
    hLayout.addComponent(cancelBtn);

    configLayout.addComponent(hLayout);

    // set pop up to center.
    center();
    // should be resizable
    setResizable(true);
    // should not be draggable
    setDraggable(false);
    //set it as modal window
    setModal(true);

    setWidth("50%");

    setHeight("75%");

}

}

2 个答案:

答案 0 :(得分:0)

好的,我们使用SelectionMode.MULTI来显示那里的行选择。 https://cdn.vaadin.com/vaadin-core-elements/latest/vaadin-grid/demo/selection.html

尽管如此,我还是希望了解如何完成上述问题所示的更改。

仍在寻找答案。

答案 1 :(得分:0)

使用渲染器和转换器,您不需要使用SelectionMode.MULTI

此示例已发布here