How to select all rows in a vaadin table?

Hi

I have one checkbox, and one table and table have 10 rows. If the user selects the checkbox, then all 10 rows in the vaadin table should select, but I do not know how to achieve this functionality. will someone tell me how to do this? If possible, provide me with a code snippet.

+4
source share
4 answers

Table.getValue() accepts either the identifier of one element or a set of several element identifiers, and Table.getItemIds() returns the identifier of all elements in the table. This means that you can select all the elements in the table, simply:

 yourTable.setValue(yourTable.getItemIds()); 

Note that this will cause performance problems if there are many elements in the table container. Should work in a simple case like yours.

+10
source

Make sure the table has its own TABLE.setMultiSelect (true), and then just repeat the identification obtained from yourTable.getItemIds () and call yourTable.select (id) for all identifiers. This is one way.

+1
source

In Vaadin 7, when you have a table with a container data source, you can do this:

 table.setValue(container.getItemIds()); 

Vaadin 6 works for me:

 public void selectAll() { int size = table.getItemIds().size(); for(int i = 0; i < size; i++) { table.select(i); } table.requestRepaint(); } 

And of course, in both versions of Vaadin, do not forget these lines:

 table.setSelectable(true); table.setMultiSelect(true); 
+1
source

You can just do it with

 Table table = new Table(); table.setValue(table.getItemIds()); 

It should not cause performance problems, instead you have several hundred lines. If you have bad architecture.

You can also just iterate over the list (Vaadin does the same) Here you can find how to undo the selected list using a simple iteration. In a nutshell:

  Collection<Object> toSelect = new ArrayList<Object>(); for (Iterator<?> it = simpleTable.getItemIds().iterator(); it.hasNext(); ) { Object next = it.next(); if (!((Collection<?>) simpleTable.getValue()).contains(next)) toSelect.add(next); } simpleTable.setValue(toSelect); 
0
source

Source: https://habr.com/ru/post/1305540/


All Articles