简体   繁体   English

标记行在 vaadin 网格中单击鼠标时选中(SelectionMode.MULTI)

[英]Mark row selected on mouse click in vaadin grid(SelectionMode.MULTI)

I have a Grid with a multi-select option.我有一个带有多选选项的网格。 In a normal way when I click in the checkbox the row is selected with the default selected color.通常,当我单击复选框时,会使用默认选定颜色选择行。 But I need to have the same or other selected color when I click in the grid´s row.但是当我单击网格行时,我需要具有相同或其他选定的颜色。

I have created a CSS:我创建了一个 CSS:

:host(#grdPeriodicos) td.selected{
    background-color: #BDBDBD;
    color: #242140;
    font-weight: bold;
}

Imported it in my class view:在我的 class 视图中导入它:

@CssImport(value = "./styles/grid-styles.css", themeFor = "vaadin-grid")
public class PeriodicoListaView extends VerticalLayout implements Serializable {
...

grdPeriodicos.setId("grdPeriodicos");
grdPeriodicos.addColumn(periodico -> periodico.getId_Nota()).setHeader("Nota");
grdPeriodicos.addColumn(periodico -> periodico.getId_NotaItem()).setHeader("Nota item"); 
grdPeriodicos.setSelectionMode(SelectionMode.MULTI);

grdPeriodicos.setClassNameGenerator( p -> {
            return ((periodicoDTO != null) && (periodicoDTO.getId_NotaItem().equals(p.getId_NotaItem()))) ? "selected" : null; 
});


grdPeriodicos.addItemClickListener(e -> {

            if(e.getItem() != null) {
                carregaEPreencheDetalhesPeriodico(e.getItem());             
            }
        });

The code above works when the grid is loaded, but when the row is clicked nothing happens.上面的代码在加载网格时有效,但是当单击该行时没有任何反应。 What can I put in the addItemClickListener to fire again setClassNameGenerator?我可以在 addItemClickListener 中放入什么来再次触发 setClassNameGenerator?

Or what can I do to have the line selected when the row is clicked?或者当单击该行时,我该怎么做才能选择该行?

In multi-selection mode, the item is selected when clicking the checkbox, but not when clicking anywhere in the row.在多选模式下,单击复选框时会选中该项目,但单击行中的任何位置时不会选中该项目。 If you want to select the item in response to a row click, you can do:如果你想对 select 项目响应一行点击,你可以这样做:

grdPeriodicos.addItemClickListener(e -> {
  grid.asMultiSelect().select(e.getItem());
});

Alternatively, if you want to update the row styles without selecting the item, you need to call refreshItem (which will cause the ClassNameGenerator to be evaluated again for that row)或者,如果要更新行 styles 而不选择项目,则需要调用refreshItem (这将导致对该行再次评估 ClassNameGenerator)

grdPeriodicos.addItemClickListener(e -> {
  if(e.getItem() != null) {
    carregaEPreencheDetalhesPeriodico(e.getItem());             
    grid.getDataProvider().refreshItem(e.getItem());
  }
});

There is a live demo using similar approach in Vaadin's Cookbook .Vaadin's Cookbook中有一个使用类似方法的现场演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM