Checkbox for mark all record in table

Hello, I have a doubt about being able to mark all records in a Table. Can this be done through a check ?, is that keys can through the operating system but if there are many records becomes uncomfortable. If a checkbox to mark all is possible and for example the datasource assigned to the Table has 100 records but only displays 10 in the Table, to obtain the selected records that would give me 10 or 100?
Thanks and regards.

Hi,

you can use selectAll method of Table to select all rows (not only visible, but all loaded by datasource).

Example layout:


<layout expand="usersTable"
        spacing="true">
    <checkBox id="selectAllChecker"
              caption="Select all"/>
    <table id="usersTable"
           width="100%"
           multiselect="true">
        <columns>
            <column id="login"/>
            <column id="name"/>
            <column id="position"/>
            <column id="email"/>
        </columns>
        <rows datasource="usersDs"/>
    </table>
</layout>

Code:


public class UserList extends AbstractWindow {

    @Inject
    protected Table<User> usersTable;

    @Inject
    protected CheckBox selectAllChecker;

    @Inject
    protected CollectionDatasource<User, UUID> usersDs;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);

        usersDs.addItemChangeListener(e -> {
            // if select all rows manually, set checker value to true
            if (usersTable.getSelected().size() == usersDs.size()) {
                selectAllChecker.setValue(true);
            }
        });

        selectAllChecker.addValueChangeListener(e -> {
            if (Boolean.TRUE.equals(selectAllChecker.getValue())) {
                usersTable.selectAll();
            } else {
                usersTable.setSelected(Collections.emptyList());
            }
        });
    }
}

In this case all items of datasource will be marked as selected, just scroll table to make sure of it.

Fantastic Yuriy, thank you.