Add checkbox in table and selected items create new data source

i have a table static table (datasources = chartDs) in this table I want to add checkbok in all row in this table.
then checked row’s data want to change to new datasource.
tabel

Do you want to copy selected rows to another datasource? By clicking on a separate button or immediately when a user selects a checkbox?

when I checked items after click OK then selected item get on new data source

Hi,

Here’s one of the possible solutions.

  1. Create a new datasource to be populated on click:
<groupDatasource id="selectedDs"
                 refreshMode="NEVER"
                 class="com.company.demo.entity.Statistic"
                 view="_local"/>
  1. Create a generated column in your table with a checkbox inside:
<columns>
    <column id="checkBox"
            caption="msg://selectSkills"
            width="50"
            generator="generateCheckBox"/>
    <column id="skill"/>
    <column id="count"/>
</columns>
  1. In the column generator add ValueChangeListener that will update the list of items when the user clicks on the checkbox:
private List<Statistic> selectedItemsList = new ArrayList<>();

public Component generateCheckBox(Statistic item) {
    CheckBox checkBox = componentsFactory.createComponent(CheckBox.class);
    checkBox.addValueChangeListener(e -> {
        if (Boolean.TRUE.equals(e.getValue())) {
            selectedItemsList.add(item);
        } else {
            selectedItemsList.remove(item);
        }
    });
    return checkBox;
}
  1. When the user clicks the OK button, the following method is invoked. It adds all items from the list to the datasource:
public void refreshChartWithSelectedItems() {
    selectedDs.clear();
    for (Statistic item : selectedItemsList) {
        selectedDs.addItem(item);
    }
}

I’ve attached a small demo project with two pieCharts: one displays all items from the table, another one displays only the items selected by the checkbox, when the users clicks OK:

demo.zip (86.6 KB)

1 Like

Error on Statistic
public Component generateCheckBox(Statistic item)

checkboxxmlcheckbox