You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vaadin 24.3.5嵌套Grid无内容问题技术求助

Vaadin 24.3.5嵌套Grid仅显示标题无内容的问题修复

问题分析

你的嵌套Grid仅显示标题和空行,核心问题出在Binder绑定逻辑错误、组件挂载错误以及数据传递的潜在问题上:

具体问题点及修复方案

  • Binder绑定对象错误:你当前把returnReasonInstanceBinder绑定到了returnReason整个集合上,而非嵌套Grid的每一行实例。每个嵌套行都需要独立的Binder来绑定对应的数据。
  • 错误调用add(returnTypeComboBox):这个操作会把下拉框直接加到主视图中,而非嵌套Grid的单元格内,导致单元格内容丢失,必须移除该调用。
  • 嵌套Grid未设置高度:默认情况下嵌套Grid高度可能为0,导致内容无法渲染,需要显式设置高度值。
  • 数据源类型验证:确保ReflectionHelper.getGetter(returnReasonInstance, "returnReason").invoke(returnReasonInstance)返回的是Iterable类型的集合(比如List),否则Grid无法加载数据。

修正后的代码片段

Grid<Object> grid = new Grid<>(Object.class, false);
grid.setId("moduleConfiguration");
grid.setSizeFull();
grid.setItems(brand.getModuleConfigurationList());
grid.setAllRowsVisible(true);

// 第一列:User Type
grid.addComponentColumn(configurationInstance -> {
    try {
        Binder<Object> moduleConfigurationBinder = new BeanValidationBinder<>(Object.class);
        bindings.put(moduleConfigurationBinder, configurationInstance);
        moduleConfigurationBinder.setBean(configurationInstance);
        ComboBox<String> comboBox = new ComboBox<>();
        comboBox.setItems(Arrays.asList("customerServiceAssociate", "gnagno", "pino"));
        comboBox.setItemLabelGenerator(Objects::toString);

        Object finalObject = configurationInstance;
        moduleConfigurationBinder.forField(comboBox)
                .bind(ReflectionHelper.getFunctionalGetter(eReturnClass, finalObject, "userType"),
                        ReflectionHelper.getFunctionalSetter(eReturnClass, finalObject, "userType"));
        return comboBox;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}).setHeader("User Type");

// 第二列:嵌套Grid - Return Configuration
grid.addComponentColumn(returnReasonInstance -> {
    try {
        // 获取嵌套Grid数据源,强制转换为Iterable类型
        Iterable<Object> returnReasonList = (Iterable<Object>) ReflectionHelper.getGetter(returnReasonInstance, "returnReason")
                .invoke(returnReasonInstance);

        Grid<Object> reasonCodeGrid = new Grid<>(Object.class, false);
        reasonCodeGrid.setId("reasonCodeGrid");
        reasonCodeGrid.setItems(returnReasonList);
        reasonCodeGrid.setAllRowsVisible(true);
        // 设置嵌套Grid高度,确保内容显示
        reasonCodeGrid.setHeight("100px"); // 可根据需求调整为"100%"或固定值

        reasonCodeGrid.addComponentColumn(instance -> {
            // 为每个行实例创建独立的Binder
            Binder<Object> rowBinder = new BeanValidationBinder<>(Object.class);
            rowBinder.setBean(instance);
            bindings.put(rowBinder, instance);

            ComboBox<String> returnTypeComboBox = new ComboBox<>();
            returnTypeComboBox.setItems(Arrays.asList("Persona1", "customer", "Persona3"));
            returnTypeComboBox.setItemLabelGenerator(Objects::toString);

            // 绑定当前行实例的属性
            rowBinder.forField(returnTypeComboBox)
                    .bind(ReflectionHelper.getFunctionalGetter(instance, "returnType"),
                            ReflectionHelper.getFunctionalSetter(instance, "returnType"));
            
            // 移除错误的add调用
            // add(returnTypeComboBox);
            return returnTypeComboBox;
        }).setHeader("Return Type");

        return reasonCodeGrid;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}).setHeader("Return Configuration");

额外验证步骤

  1. 打印数据源大小验证数据是否存在:
    System.out.println("嵌套Grid数据量:" + ((Collection<?>) returnReasonList).size());
    
  2. 确认ReflectionHelper的getFunctionalGetter和getFunctionalSetter能正确生成属性的读写函数,避免反射调用失败。

内容的提问来源于stack exchange,提问作者Andrea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 13:16:17