Generate Dynamic columns based on Webservice output

Hi All,

i am experiencing the following problem.

Is there any way to increase the table columns dynamically based on the output of webservice. My requirement is that when i change the dropdown value it completes the server side action and refresh the client side( in this case table is client side control ) control then updates the no of columns dynamically after every refresh.

looking forward for yours quick response.

[/b]

In your action, you can lookup the table control, and use the jsf UIComponent apis (every jsf control is an instance of UIComponent; see http://java.sun.com/javaee/javaserverfaces/1.1/docs/api/javax/faces/component/package-summary.html) to add and remove columns to the table control. Here’s an example:

public void myAction() {
// lookup table by its id (“my-table”)
UIData table = (UIData) findComponentInRoot(“my-table”);
// get list of table’s children
List columns = table.getChildren();
// remove existing columns
while (!columns.isEmpty())
columns.remove(0);

// invoke webservice
String[] columnHeaders = getMyWebService().getResult().getMyColumnHeaders();
// add new column control for each column header returned by ws
for (int i = 0; i < columnHeaders.length; i++) {
    // create new column control and add it to table
    HtmlTableColumn column = new HtmlTableColumn();
    columns.add(column);

    // create header text-control and add it to column
    HtmlOutputText header = new HtmlOutputText();
    header.setValue(columnHeaders[i]);
    column.setHeader(header);

    // create column-content text-control and add it to column
    HtmlOutputText content = new HtmlOutputText();
    // create value-binding expression
    // this example assumes table's row-variable is "row", and is bound to an array of string-arrays:
    String contentExpression = "#{row[" + i + "]}";
    // if the table was instead bound to an array of typed row objects
    // (and the column's header text was the same as the name of the row property
    // to use to display the column's content), you might use the following expression instead:
    // String contentExpression = "#{row." + columnHeaders[i] + "}";
    ValueBinding contentBinding = getFacesContext().getApplication().createValueBinding(contentExpression);
    content.setValueBinding("value", contentBinding);
}

}

Justin