3

I want to arrange p:column dynamically i.e. p:column header value should be come from database and it increase or decrease according to database value. please suggest me ..

saurabh
  • 61
  • 1
  • 2
  • 7

3 Answers3

5

You should use the <p:columns , take a look at the showcase DataTable - Dynamic Columns

here a code snippet from the showcase

<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>
Daniel
  • 36,833
  • 10
  • 119
  • 200
  • @Daniel : can you please look into my question http://stackoverflow.com/questions/20789832/how-can-i-get-the-nested-components-of-a-pcolumn-in-a-facelet-using-el – Jalal Sordo Dec 26 '13 at 19:11
  • @Daniel : I know this question is old but can you tell me how to display data if i do not know the column.property value i.e. property is from database which will be stored in a list and will depend on no. of columns – razor Apr 14 '16 at 14:56
4

I hope this code will help you

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);
       }
    }
}

Thank you

Praveenkumar_V
  • 1,394
  • 2
  • 11
  • 19
1

If you don't know how many columns you want to show in the datatable, you can use:

List <Object[]> resultsValues = service.getResultsValues(...);

List <MyHeader> resultsHeader = service.getResultsHeader(...);
  • each Object[] represents a row.
  • each MyHeader is an Object that represents a column header

Here is the datatable:

<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>
Nick Udell
  • 2,420
  • 5
  • 44
  • 83
Tchapu
  • 11
  • 3