« Previous day (Feb 26, 2005) | Main | Next day (Feb 28, 2005) »

20050227 Sunday February 27, 2005

Mixed Table Columns

Before I begin: Creator Update 7 is out. This is a pretty small patch (from update 6) containing just a handful of bugfixes - but some of them are important, so do update.

Anyway, I just browsed the Creator forum and saw a couple of questions I thought would make an interesting blog entry. Here's the first question:

I would like to selectively have some items in the datatable be links and others not (e.g. some as simple outputtext, others as links). I need to make this decision at run time.

Is this possible to do? Is there an event to dynamically alter the table item output?

Yes, it's possible - with a little trick! You cannot change the types of components, but you -can- dynamically control whether a component is visible or not! Therefore, all you need to do is place BOTH types of components in the column, create a property which determines which type of output you want for the current row, and then cause exactly one of the two components to be made visible for this row. You control component visibility with the "rendered" property of a JSF component.

Let's take a specific example. I drop a Data Table component on the canvas, and then I drop the "PERSON" table from the Sample "Travel" database that ships with Creator. It has 4 columns: Id, Name, Jobtitle, and FrequentFlyer. I want to make the Name column use a hyperlink if that person is a Frequent Flyer. To do this, I drag a hyperlink and drop it in the Application Outline on top of "Column2" - this places the hyperlink in the second column in the table - the Name column.

Then I double click on the canvas to go to the Page bean for this file (the Java code) and add the following method to create a read-only boolean property:

    public boolean isFrequentFlyer() {
        Object o = getValue("#{currentRow['FREQUENTFLYER']}");
        return o instanceof Boolean && ((Boolean)o).booleanValue();
    }
Go back to the Page1.jsp file.

Finally, I go and update the value bindings:

When you run, you should see this:

Notice how the hyperlinks are only showing up when the FrequentFlyer value is true. And of course there's no reason you need to show the frequent flyer column in the table - you can hide it with the Table Layout context menu item on the table. And furthermore, you can obviously make the logic for deciding which type to use as complex as you want - your isFrequentFlyer() method can do anything you want.

(2005-02-27 21:37:56.0) Permalink Comments [3]