简体   繁体   中英

Vaadin Add new column to existing table

I'm having some problems to add new column to my table that is generated from one JPAContainer. The question is... I can add a new column to my existing table if my fields are inserted from JPAContainer? My code looks like...

persons = JPAContainerFactory.make(Users.class, PERSISTENCE_UNIT);
persons.sort(new String[]{"niu"}, new boolean[]{true});
table_1.setWidth("100%");
table_1.setSelectable(true); // Hacemos que se puedan seleccionar las filas del Grid.
table_1.setMultiSelect(false);// Selección de múltiples filas del Grid.

table_1.setContainerDataSource(persons);
table_1.addContainerProperty("admin_vis", String.class, null);
for (Iterator i = table_1.getItemIds().iterator(); i.hasNext();) {
        Object itemIdentifier = i.next();

        Item item = table_1.getItem(itemIdentifier);
        String admin = (String) item.getItemProperty("admin").getValue();
        Item item1 = ((Container) table_1.getContainerProperty(String.class,   "admin_vis")).addItem("row"+i);
        if(admin.equals("Y")) {
            Property property = item1.getItemProperty("admin_vis");
            property.setValue("true");
        } else {
            Property property = item1.getItemProperty("admin_vis");
            property.setValue("false");
        }            
    }
table_1.setVisibleColumns(new Object[] { "niu", "nom", "mail", "admin_vis" });
table_1.setColumnHeaders(new String[] { "Niu", "Nom", "Mail", "Admin", })
table_1.setPageLength(15);
table_1.setImmediate(true);

The error that I'm having is: Caused by: java.lang.UnsupportedOperationException at com.vaadin.addon.jpacontainer.JPAContainer.addContainerProperty(JPAContainer.java:666) at com.vaadin.ui.AbstractSelect.addContainerProperty(AbstractSelect.java:772) at com.vaadin.ui.Table.addContainerProperty(Table.java:4099)

Hope someone can help me. Thanks for advice!

I answer to my own, the better solution that I find to this problem is next...

table_1.addGeneratedColumn("mycolumn", new ColumnGenerator() {
        public Object generateCell(Table source, Object itemId, Object columnId) {
            Item item = source.getItem(itemId);
            String admin = (String) item.getItemProperty("admin").getValue();
            return admin.equals("Y") ? "true" : "false";

        }
    });

With this form all correctly works, thanks for reading. Greetings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM