mchinta
mchinta

Reputation: 292

Making Table columns from database as hyperlinks in vaadin

I am new to vaadin, I want to make the vaadin table columns from database as hyperlinks. I tried with this code.Here I am using PagedTable in my code instead of normal vaadin table. I tried the all the possible ways but I am not getting the result.After making that hyperlinks, If user clicks that link I need to display the POPUP Window.

        if (rs.next()) {
        Object dashboardDataRowId = _reportTable.addItem();

                    Link l = new Link("rs.getInt(1)", new ExternalResource(
                            "#"));

                    _reportTable.getItem(dashboardDataRowId)
                            .getItemProperty("todo").setValue(l);
                    _reportTable.getItem(dashboardDataRowId)
                            .getItemProperty("watchlist")
                            .setValue(rs.getInt(2));
                    _reportTable.getItem(dashboardDataRowId)
                            .getItemProperty("terminated")
                            .setValue(rs.getInt(3));
                    _reportTable.getItem(dashboardDataRowId)
                            .getItemProperty("processed")
                            .setValue(rs.getInt(4));
                    _reportTable.getItem(dashboardDataRowId)
                            .getItemProperty("total")
                            .setValue(rs.getInt(5));

                }
            } catch (Exception e) {
                logger.error(
                        "Error loading dashboard report. Exception: {}",
                        e.getMessage());
                logger.debug("Error loading dashboard report.", e);
                showWarningNotification(
                        "Error loading dashboard report. Please contact admin",
                        "Error message is " + e.getMessage());

            } finally {
                rs.close();
                stmt.close();
            }

        }
    });

}

private void populateReportTableColumns(final PagedTable reportTable) {

    reportTable.addContainerProperty("todo", Link.class, "-", "To Do",
            null, null);

    // reportTable.addContainerProperty("watchlist", Link.class, new
    // Link("Watch List", new ExternalResource("#")), "Watch List", null,
    // null);
    reportTable.addContainerProperty("watchlist", Button.class, "-",
            "Watch List", null, null);
    reportTable.addContainerProperty("terminated", Button.class, "-",
            "Terminated", null, null);
    reportTable.addContainerProperty("processed", Button.class, "-",
            "Processed and Closed", null, null);
    reportTable.addContainerProperty("total", Button.class, "-", "Total",
            null, null);
}

Upvotes: 2

Views: 947

Answers (2)

mchinta
mchinta

Reputation: 292

I got it.

Link link = new Link(String.valueOf(rs.getInt(1)), new ExternalResource("#"));
    _reportTable.getItem(dashboardDataRowId)
   .getItemProperty("todo").setValue(link);

Now the Link is coming, but I need to display the pop up when user clicks on link.

Upvotes: 3

kris54321
kris54321

Reputation: 776

You can to accomplish this with a generated column.

Instead of adding the Link object as item property value, you just set the url:

_reportTable.getItem(dashboardDataRowId).getItemProperty("todo").setValue("http://vaadin.com");

And have the column generator create the Link object from the url:

reportTable.addGeneratedColumn("link", new Table.ColumnGenerator() {

            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                String url = (String) source.getItem(itemId).getItemProperty("todo").getValue();
                return new Link("LinkTitle", new ExternalResource(url));
            }
        });

This is untested code, but generally it should work like this.

EDIT: Most certain way to get the link to open in a new window in Vaadin is to use the BrowserWindowOpener. You need to use a button instead of a link, but if you want it to still look the same, you can style the button to look like a link:

Button link = new Button();
link.setStyleName(Runo.BUTTON_LINK); // use the theme you are currently extending here
BrowserWindowOpener opener = new BrowserWindowOpener(new ExternalResource("http://vaadin.com"));
opener.extend(link);

Alternatively you can set the link target to _blank but most browsers open this in a new tab rather than window:

link.setTargetName("_blank");

Upvotes: 1

Related Questions