【问题标题】:Vaadin Grid always selects last rowVaadin Grid 总是选择最后一行
【发布时间】:2018-04-16 10:51:42
【问题描述】:

我需要帮助调试以下问题: 当我单击具有 3 行的 Vaadin Grid 时,它会忽略对除最后一行之外的任何内容的单击,并始终选择第 3 行。

代码如下:

    // declare Grid objects
public final Grid<SoapKPIContainer> soapKPIOverviewGridDisplay;

..
public SoapKPIOverviewView(SoapKPIRepository soapKPIRepository, Navigator navigator,
                           BpspSoapCheckCommunications bpspSoapCheckCommunications,
                           UIMessageByLocaleService messageByLocaleService) {
    ..

    this.soapKPIOverviewGridDisplay = new Grid<>(SoapKPIContainer.class);
    this.soapKPIOverviewGridDisplay.setColumns();

    Grid.Column<SoapKPIContainer, ?> lastAlarmStatusIconColumn = this.soapKPIOverviewGridDisplay.addColumn("lastAlarmStatusIcon", new ImageRenderer<>());
    lastAlarmStatusIconColumn.setSortable(false);
    lastAlarmStatusIconColumn.setResizable(false);
    lastAlarmStatusIconColumn.setWidth(80.0f);
    lastAlarmStatusIconColumn.setCaption(messageByLocaleService.getMessage("label.lastalarmstatus"));

    Grid.Column<SoapKPIContainer, ?> activationIconColumn = this.soapKPIOverviewGridDisplay.addColumn("activationIcon", new ImageRenderer<>());
    activationIconColumn.setSortable(false);
    activationIconColumn.setResizable(false);

    this.soapKPIOverviewGridDisplay.getDefaultHeaderRow().getCell("lastAlarmStatusIcon").setHtml(messageByLocaleService.getMessage("label.lastalarmstatus"));
    this.soapKPIOverviewGridDisplay.getDefaultHeaderRow().getCell("activationIcon").setHtml(messageByLocaleService.getMessage("label.activationicon"));

    this.soapKPIOverviewGridDisplay.addColumn(SoapKPIContainer::getKpiName).
    setCaption(messageByLocaleService.getMessage("header.kpiName"));

    ..

    this.setSizeFull();
    this.soapKPIOverviewGridDisplay.setSizeFull();       
    this.soapKPIOverviewGridDisplay.setHeight(400, Unit.PIXELS);

    this.soapKPIService = new SoapKPIService(soapKPIRepository);

    this.soapKPIOverviewGridDisplay.setItems( soapKPIService.toContainer( soapKPIService.findAllSoapKPI() ) );

    ..  
    // adding Listener for the Grid to enable for the user to select single KPIs
    soapKPIOverviewGridDisplay.setSelectionMode(SelectionMode.SINGLE);
    soapKPIOverviewGridDisplay.asSingleSelect().addValueChangeListener(e -> {
        log.debug("asSingleSelect().addValueChangeListener " + e.getValue());           
        if ( e.getValue() != null) {

            log.debug("asSingleSelect().addValueChangeListener findSoapKPIById #" + e.getValue().getKpiId());   

            testKPIDefView.setEnabled(true);
            soapKPI = soapKPIService.findSoapKPIById( e.getValue().getKpiId() );

            changeEnabled.setVisible(true);

            if( soapKPI.getEnabled() == 1)
                changeEnabled.setCaption(messageByLocaleService.getMessage("button.disable"));

            else
                changeEnabled.setCaption(messageByLocaleService.getMessage("button.enable"));

        }

        else {

            testKPIDefView.setEnabled(false);
            changeEnabled.setVisible(false);
            soapKPI = null;
        }
    });
    ..
    soapKPIOverviewLayout.addComponent(soapKPIOverviewGridDisplay);

【问题讨论】:

    标签: select grid vaadin


    【解决方案1】:

    我仍然不确定代码中的哪一行导致它中断,但我发现重写 row 对象的 equals 方法会使问题消失:

    @Override
    public boolean equals(Object o) {
    
        if (o == this) return true;
        if (!(o instanceof SoapKPIContainer)) {
            return false;
        }
    
        SoapKPIContainer container = (SoapKPIContainer) o;
    
        return this.getKpiId() == container.getKpiId();
    }
    

    【讨论】:

    • 这是我的第一个想法,它可能是您的网格项目类的equals 的问题。当覆盖equals 时,请确保您也实现了hashCode(),这样您的对象就可以在HashSet 等中使用。另一个选项是为实现getId 方法的网格提供自定义DataProvider,例如你的kpiId
    猜你喜欢
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 2015-11-01
    • 2022-07-06
    相关资源
    最近更新 更多