How to create dynamic rich:datatable with richfaces 4.x?
kittop Jan 1, 2011 8:50 PMI try to do like here:
rich.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head></h:head>
<body>
<rich:panel>
<f:facet name="header">
Write your own custom rich components with built-in AJAX support
</f:facet>
<h:form>
<rich:dataTable binding="#{richBean.htmlDataTable}">
</rich:dataTable>
</h:form>
</rich:panel>
</body>
</html>
Backing Bean: RichBean:
package demo;
import java.util.ArrayList;
import java.util.List;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlColumn;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.richfaces.component.UIDataTable;
@ManagedBean
@SessionScoped
public class RichBean {
private DataModel dataModel;
private UIDataTable htmlDataTable;
public void setDataModel(DataModel dataModel) {
this.dataModel = dataModel;
}
@SuppressWarnings({ "rawtypes" })
public DataModel getDataModel() {
dataModel = new ListDataModel();
List list = new ArrayList();
list.add(new Affix("1", "kyo"));
list.add(new Affix("2", "kitto"));
list.add(new Affix("3", "ptianfeng"));
list.add(new Affix("4", "ptfeng"));
dataModel.setWrappedData(list);
return dataModel;
}
public UIDataTable getHtmlDataTable() {
FacesContext fc = FacesContext.getCurrentInstance();
Application app = fc.getApplication();
ExpressionFactory exp = app.getExpressionFactory();
htmlDataTable = new UIDataTable();
htmlDataTable.setVar("var");
HtmlOutputText headerText = new HtmlOutputText(); // 列名的内容
headerText.setValue("Sqlid");
HtmlColumn column = new HtmlColumn();
HtmlOutputText out = new HtmlOutputText();
ValueExpression value = exp.createValueExpression(fc.getELContext(),
"#{var.sqlid}", String.class);
out.setValueExpression("value", value);
column.getChildren().add(out);
column.setHeader(headerText);
htmlDataTable.getChildren().add(column);
htmlDataTable.setValue(getDataModel());
return htmlDataTable;
}
public void setHtmlDataTable(UIDataTable htmlDataTable) {
this.htmlDataTable = htmlDataTable;
}
}
and the Affix:
package demo;
import java.io.Serializable;
public class Affix implements Serializable{
private String sqlid;/* */
private String name; /* */
public Affix(String sqlid, String name){
this.sqlid = sqlid;
this.name = name;
}
public String getSqlid() {
return sqlid;
}
public void setSqlid(String sqlid) {
this.sqlid = sqlid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and it just show:
Sqlid |
---|
1 |
2 |
3 |
4 |
Is any error in the code ?
how to set the skin of richfaces?
Thank!