我想动态排列p:列,即p:列标题值应来自数据库,并根据数据库值增加或减少。请建议我..
答案 0 :(得分:5)
您应该使用<p:columns
,看一下展示DataTable - Dynamic Columns
此处展示了
的代码段<p:dataTable id="cars" var="car" value="#{tableBean.carsSmall}">
<p:columns value="#{tableBean.columns}" var="column" columnIndexVar="colIndex"
sortBy="#{car[column.property]}" filterBy="#{car[column.property]}">
<f:facet name="header">
#{column.header}
</f:facet>
#{car[column.property]}
</p:columns>
</p:dataTable>
答案 1 :(得分:4)
我希望此代码可以帮助您
<强>的index.xhtml 强>
<?xml version='1.0' encoding='UTF-8' ?>
<!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:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Dynamic DataTable columns</title>
</h:head>
<h:body>
<h:form>
<p:dataTable id="cars" var="car" value="#{GenerateTable.test}">
<p:columns value="#{GenerateTable.columns}" var="column" columnIndexVar="colIndex">
<f:facet name="header">
#{column}
</f:facet>
<h:outputText value="#{car[colIndex]}"/>
</p:columns>
</p:dataTable>
</h:form>
</h:body>
</html>
<强> GenerateTable.java 强>
import java.sql.*;
import java.util.*;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name="GenerateTable")
@RequestScoped
public class generateTable
{
public static List<String> columns=new ArrayList<String>();
public static ArrayList test=new ArrayList();
/*getter and setter for above variables*/
public List<String> getColumns()
{
return columns;
}
public void setColumns(List<String> columns)
{
this.columns=columns;
}
public ArrayList getTest()
{
return test;
}
public void setTest(ArrayList test)
{
this.test=test;
}
/*end of getter and setter*/
public generateTable()
{
dynamicColumns();
}
public void dynamicColumns()
{
test=new ArrayList<String>();
columns=new ArrayList<String>();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/db_test","root","");
Statement stmt=con.createStatement();
ResultSet result=stmt.executeQuery("select * from myTable");
ResultSetMetaData md = result.getMetaData();
int columnCount = md.getColumnCount();
for(int i=1; i<=columnCount; i++)
columns.add(md.getColumnName(i)); //adding column name dynamicly
while(result.next())
{
ArrayList child=new ArrayList();
for(int i=1;i<=columnCount;i++)
child.add(result.getString(i)); //denpends on column add the data
test.add(child);
}
result.close();
con.close();
}
catch(Exception ex)
{
System.out.println(""+ex);
}
}
}
谢谢
答案 2 :(得分:1)
如果您不知道要在数据表中显示多少列,可以使用:
List <Object[]> resultsValues = service.getResultsValues(...);
List <MyHeader> resultsHeader = service.getResultsHeader(...);
Object
,表示列标题这是数据表:
<p:dataTable id="myTable" var="lineResult"
value="#{yourManagedBean.resultsValues}" paginatorAlwaysVisible="false"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
paginator="true" rows="10" rowKey="#{lineResult}">
<p:columns value="#{yourManagedBean.resultsHeader}" var="column" columnIndexVar="i" >
<f:facet name="header">
<h:outputText title="#{column.description}" value= "#{column.label}" />
</f:facet>
<h:outputText value= "#{lineResult[i]}" />
</p:columns>
</p:dataTable>