mardi 22 février 2022

Grid with LitTemplate not updating checkbox consistently

We are updating our Grid forms to use LitTemplates for rendering. However we have run into an issue with a checkbox state not being updated even though the underlying data for the state has been updated. The following code demonstrates the issue:

        HorizontalLayout hlTest = new HorizontalLayout();
        List<String> selected = new ArrayList<>();
        Grid<String> testGrid =new Grid<>();
        testGrid.setWidth("250px");
        testGrid.setHeight("250px");
        List<String> dataList = new ArrayList<>(List.of("AAAAA", "BBBBB", "CCCCC", "DDDDD", "EEEEE"));
        ListDataProvider<String> dataProvider = new ListDataProvider<>(dataList);
        testGrid.setItems(dataProvider);

        String template = """
                    <vaadin-checkbox @click="${handleCheckClick}" ?checked="${item.rowSelected}"></vaadin-checkbox>
                    <div>${item.text}</div>
                """;

        LitRenderer<String> lit = LitRenderer.of(template);
        lit.withProperty("rowSelected", item -> {
            boolean rc = selected.contains(item);
            System.out.println("Item selected: " + item + " => " + rc);
            return rc;
        });
        lit.withProperty("text",item -> item);
        lit.withFunction("handleCheckClick", (item) -> {
            if(selected.contains(item))
                selected.remove(item);
            else
                selected.add(item);
        });
        testGrid.addColumn(lit);
        Button bSelect = new Button("Select");
        bSelect.addClickListener(buttonClickEvent -> {
            if(selected.size() > 0)
            {
                dataList.clear();
                dataList.addAll(selected);
                selected.clear();
                dataProvider.refreshAll();
            }
        });
        hlTest.add(testGrid, bSelect);

With the given grid, click a few checkboxes and click 'Select'. The list is reduced to the selected rows, but the checkboxes are still ticked even though 'selected' is empty. The logging within 'rowSelected' is correctly returning the state.

If I remove the column and re-add to grid, then all is displayed correctly. Also, if I select all items in the grid then the select also works correctly.

Currently using Java17 and Vaadin 22.0.6 (have also tried 23.0.0 beta3).

Thanks.




Aucun commentaire:

Enregistrer un commentaire