【问题标题】:Vaadin Gird getSelectedRows() returns empty but there are selected rowsVaadin Gird getSelectedRows() 返回空但有选定的行
【发布时间】:2017-03-15 10:12:22
【问题描述】:

我在使用 vaadin-gird 时遇到了一种奇怪的行为。我只是想将来自网格中选定行的信息(gRegisteredRecords)存储到 LinkedHashMaps 的 arrayList 中。出于任何原因,当我第一次单击按钮 btnGenerateXML 时,getSelectedRows() 方法返回一个空列表,但第二个方法工作正常......为什么它在第一次尝试时不起作用?这是代码..谢谢!

_btnGenerateXML.addClickListener(p -> {
        ArrayList <LinkedHashMap<String, String >> alSelected = new ArrayList<>();

        for (Object itemId: gRegisteredRecords.getSelectedRows()) {

            LinkedHashMap<String, String> lhmProgrammValue = new LinkedHashMap<>();

            this.gRegisteredRecords.getContainerDataSource().
            getContainerPropertyIds().forEach(propertyId ->
                lhmProgrammValue.put(propertyId.toString(),
                    this.gRegisteredRecords.getContainerDataSource()
                    .getItem(itemId)
                    .getItemProperty(propertyId)
                    .getValue().toString()));
            alSelected.add(lhmProgrammValue);
        }

    }
}

【问题讨论】:

    标签: java vaadin7 vaadin-grid


    【解决方案1】:

    以防万一其他人将来遇到此问题,我发现了问题并想分享解决方案。正如我一开始所想的那样,clicklistener 并没有出现问题。 _btnGenerateXML 按钮链接到 Vaadin com.vaadin.server.FileDownloader 对象,因此元素被正确检索并添加到列表中,但 filedownloader 元素在生成要下载的文件时不使用它们。我通过创建一个从 FileDownloader 扩展的类并覆盖方法 handleConnectorRequest 解决了这个问题,如下所示:

    public class CustomFileDownload extends FileDownloader {
     public interface OnDemandStreamResource extends StreamSource {
        String getFilename ();
      }
    
      private static final long serialVersionUID = 1L;
      private final OnDemandStreamResource onDemandStreamResource;
    
      public CustomFileDownload (OnDemandStreamResource onDemandStreamResource) {
        super(new StreamResource(onDemandStreamResource, ""));
        this.onDemandStreamResource = onDemandStreamResource;
       }
    
      @Override
      public boolean handleConnectorRequest (VaadinRequest request, VaadinResponse response, String path)
      throws IOException {
        getResource().getStream().setParameter("Cache-Control", "private,no-cache,no-store");
        getResource().getStream().setParameter("Pragma", "no-cache");
        getResource().setCacheTime(0);
        getResource().setFilename(onDemandStreamResource.getFilename());
        return super.handleConnectorRequest(request, response, path);
      }
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 2021-10-31
      相关资源
      最近更新 更多