Table Generated Columns

Hi, i am new to Vaadin and i am stucked.

i cant find a way to access generated checkboxes. All i want to do is change value of
all
generated checkboxes after “selectAll” checkbox is clicked.

         table.addGeneratedColumn("selected", new Table.ColumnGenerator() {
            public Object generateCell(Table source, final Object itemId, Object columnId) {
                boolean selected = selectedItemIds.contains(itemId);
                final CheckBox cb = new CheckBox("", selected);
                cb.addListener(new Property.ValueChangeListener() {
                    public void valueChange(Property.ValueChangeEvent event) {
                        if(selectedItemIds.contains(itemId)){
                            selectedItemIds.remove(itemId);
                        } else {
                            selectedItemIds.add(itemId);
                        }
                    }
                });
                return cb;
            }
        });


        BeanQueryFactory<HtmlContentBeanQuery> queryFactory = new
                BeanQueryFactory<HtmlContentBeanQuery>(HtmlContentBeanQuery.class);

        Map<String,Object> queryConfiguration=new HashMap<String,Object>();
        queryConfiguration.put("htmlContentRepository", repository);
        queryFactory.setQueryConfiguration(queryConfiguration);

        LazyQueryContainer container=new LazyQueryContainer(queryFactory, true, 50);
        container.addContainerProperty("title", String.class,  null);
        table.setContainerDataSource(container);

        final CheckBox selectAll = new CheckBox();
        selectAll.setImmediate(true);
        selectAll.addListener(new Property.ValueChangeListener() {

            public void valueChange(Property.ValueChangeEvent event) {
              for (Iterator i = table.getVisibleItemIds().iterator(); i.hasNext(); ) {
                    int iid = (Integer) i.next();

                     Item item = table.getItem(iid);

                    // this items are  org.vaadin.addons.lazyquerycontainer.CompositeItem 
                    // so code below dont actualy works


                    CheckBox checkbox = (CheckBox)
                            (item.getItemProperty("selected").getValue());

                    checkbox.setValue(selectAll.getValue());
                }

            }
        });

        GridLayout tableHeader = new GridLayout(2,1);
        tableHeader.setHeight("25px");
        tableHeader.addStyleName("fakeheader");
        selectAll.setWidth(selectAllWidth, Sizeable.UNITS_PIXELS);
        tableHeader.addComponent(selectAll, 0, 0);
        Label titleLabel = new Label("Title");
        titleLabel.setWidth(titleWidth + 2 * 6, Sizeable.UNITS_PIXELS);
        tableHeader.addComponent(titleLabel, 1, 0);

Hi,
Take a look at this post
https://vaadin.com/forum/-/message_boards/view_message/1329207

I hope it helps.

Hi, tnx a lot, but i readed this post already, have copy pasted suggested code :slight_smile: The problem is to select all checkboxes, not to track selected checkboxes.

I hacked this. Bit ugly perhaps, but modify as you wish.

		final Map<Object, Property> checkBoxMap = new HashMap<Object, Property>(); 
		
		Table checkBoxes = new Table();
		checkBoxes.addContainerProperty("i", Integer.class, null);
		
		for (int i = 0; i < 1000; i++) {
			Object id = checkBoxes.addItem();
			checkBoxes.getContainerProperty(id, "i").setValue(i);
		}
		
		checkBoxes.addGeneratedColumn("checkbox", new ColumnGenerator() {
			public Object generateCell(Table source, Object itemId, Object columnId) {
				Field field = new CheckBox();
				((AbstractComponent) field).setImmediate(true);
				Property property = checkBoxMap.get(itemId);
				if (property == null) {
					property = new ObjectProperty<Boolean>((Boolean) selectAll.getValue()); 
					checkBoxMap.put(itemId, property);
				}
				field.setPropertyDataSource(property);
				return field;
			}
		});
		selectAll = new CheckBox("Select All", new Button.ClickListener() {
			
			public void buttonClick(ClickEvent event) {
				for (Map.Entry<Object, Property> entry : checkBoxMap.entrySet()) {
					entry.getValue().setValue(event.getButton().getValue());
				}
			}
		});
		selectAll.setImmediate(true);
		mainWindow.addComponent(selectAll);
		mainWindow.addComponent(checkBoxes);

Yes i already thought about holding references to each generated checkbox.

I cant get use of generated checkboxes coz something goes fishy after i delete a row in a table, i get now out of sync errors.

UPD:

I delete with extern button a row from my Container, generated buttons in each row just logging row number. i dont know what happens, but after i delete a row and click any of generated button i got out of sync error.


        //extern button perfectly delete row
        mainLayout.addComponent(new Button("Delete", new Button.ClickListener() {
            public void buttonClick(Button.ClickEvent event) {
                    try {
                        
                        Object itemId = table.getValue();

                        LazyQueryContainer container = (LazyQueryContainer) table.getContainerDataSource();
                        container.removeItem(itemId);
                        container.commit();
                        container.refresh();


                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            }
        }));

...
        //table intern button that raise out of sync error if i click it after row removal
        table.addGeneratedColumn("action", new Table.ColumnGenerator() {
            public Object generateCell(Table source, final Object itemId, Object columnId) {
                final NativeButton b = new NativeButton("Delete");
                b.setImmediate(true);
                b.addListener(new Button.ClickListener() {
                    public void buttonClick(Button.ClickEvent event) {
                        System.out.println("click: " + itemId);
                   }
                });
                return b;
            }
        });

In my example I’m not holding references to the checkboxes, because they get generated and deleted however. I’m holding references to properties that are datasources for the generated checkboxes. Anyway I’m not sure if it even then works.