Board index JavaServer Faces General [solved] Reusing same datatable for different datasources

[solved] Reusing same datatable for different datasources

Components, Ajax Framework, Utilities and More.


Posts: 8
Location: Pakistan
Is it possible to use the same datatable with multiple data sources? If yes, then how? Can someone give me an example of it?
To be more precise, can be bind/unbind different sources to datatable programaticaly? Thanks in Advance
Last edited by silent warrior on 26 Feb 2012, 15:54, edited 1 time in total.
I use...
JDK 1.7
Java EE 6
JSF 2.2
PrimeFaces 4.0
Apache Tomcat 7
NetBeans 7.4


Posts: 345
Therefore is a backing bean or a data model, which holds the real data. DataTable only shows the data it becomes by previous mentioned objects. For example you can have 2 different objects, which share the same interface (so the same datatable columns can be used). Than you can decide in your request/view or session scoped bean, which data you initialize (will be shown later in dataTable).

Beancode:
@SessionScoped
@ManagedBean
public class MySessionBean {
   private List<MyInterface> data;
   
   @PostConstruct
   public void initData() {
                // Beware of the pseudo code :)
      if(TODAY == HAPPY_CADAVER) {
         data = HAPPY_CADAVER_DATA;
      } else {
         data = NORMAL_DAY_DATA;
      }
   }
   
   public List<MyInterface> getData() {
      return data;;
   }
}


Your datatable simply uses the data which was initialized in MySessionBean:
<p:dataTable value="#{mySessionBean.data}" var="myInterface"> ... </p:dataTable>


I hope i write not to crazy, so some parts are "understandable" :mrgreen:
Primefaces 3.4-SNAPSHOT (with dataExporter patch)
JSF: Mojarra 2.1.17 (Spec 2.1) - running on JBoss AS 7.1.1.Final


Posts: 70
Location: Brazil
Or you use many datatables, but with a rendered="#{myBean.condition}" to show only the correct datatable =)
Mixing it with ui:include + ui:params, you have only one code for the same datatable!
When i go to my work, i post one example =)
PrimeFaces 3.3 GA, Mojarra 2.1.7, Tomcat 7, JDK1.7 x64, Fedora Core 16, Eclipse Indigo


Posts: 8
Location: Pakistan
caioquirino wrote:
When i go to my work, i post one example =)

Thanks in Advance! I'll be waiting for it! :)
I use...
JDK 1.7
Java EE 6
JSF 2.2
PrimeFaces 4.0
Apache Tomcat 7
NetBeans 7.4


Posts: 8
Location: Pakistan
@Hamsterbau! Thanks for your reply. but how can we do this if our data come from different data sources?
e.g:

Beancode:
@SessionScoped
@ManagedBean
public class MySessionBean {
   private List<MyDatabaseTable1> data1;
   private List<MyDatabaseTable2> data2;
   private DataTable table;
   @PostConstruct
   public void initData() {
                // Beware of the pseudo code :)
      if(someCondition) {
         table.source = data1;
      } else {
         table.source = data2;
      }
   }
   
   public List<MyInterface> getData() {
      return data;;
   }
}
I use...
JDK 1.7
Java EE 6
JSF 2.2
PrimeFaces 4.0
Apache Tomcat 7
NetBeans 7.4


Posts: 345
Do you bind your dataTable to your backing bean? Otherwise think inverse: Do not inject the dataTable in your bean. Insert the data from your bean into your dataTable:
<p:dataTable value="#{mySessionBean.dataFromAnySource}" var="anySource"> ... <p:dataTable>


And your bean just delivers the right data:
public List<Data> getDataFromAnySource() { ... }


What really matters is the data not the source it comes from :roll:
Primefaces 3.4-SNAPSHOT (with dataExporter patch)
JSF: Mojarra 2.1.17 (Spec 2.1) - running on JBoss AS 7.1.1.Final


Posts: 8
Location: Pakistan
By the grace of God. After lots of struggle! i finally achieved this!
Image
For this i pay bundles of thanks to BalusC for his expert tips at StackOverflow
So i would like to share my solution with all. So here is what i did in my xhtml file:

<p:selectOneMenu value="#{dbmBean.selectedTable}" style="height:27px" >
    <c:forEach items="#{dbmBean.tableNames}" var="table">
        <f:selectItem itemLabel="#{table.value}" itemValue="#{table.key}"/>
    </c:forEach>
</p:selectOneMenu>
<p:commandButton value="Go" action="#{dbmBean.goToTable}" ajax="false" />
...
<p:dataTable binding="#{dbmBean.table}" var="row" rowIndexVar="index">
<f:facet name="header"/>
<p:columns value="#{dbmBean.columns}" var="column" columnIndexVar="colIndex" > 
        <f:facet name="header"> 
            #{column.header} 
        </f:facet>
        <h:outputText value="#{row[column.property]}"/>
    </p:columns>
</p:dataTable>

and in the backing bean:

public class DatabaseManagerBean implements Serializable {
    private List<ColumnModel> columns; // Column model is a simple class with two string properties: header, property
    ...   
    public void goToTable() {
        int tableIndex = new Integer(this.selectedTable);
        switch (tableIndex) {
            case 1:
                 Players tempPlayers = new Players(); //the class which get data from a database table
                 this.players = tempPlayers.getAllPlayers();
                 this.columnNames = tempPlayers.getColumnNames();
                 for (String colName : columnNames) {
                    columns.add(new ColumnModel(colName.toUpperCase(), colName));
                 }
                 table.setRendered(true);
                 table.setValue(this.players);
                 break;
                 ...
                 default:
                 table.setRendered(false);
         } //end of switch statement
    } //end of goToTable() method
} //end of DatabaseManagerBean

This code snippet would work exactly as i wanted in the given screenshot!
Also if someone find something left unexplained or missing, please do reply to this post
And again hats off to BalusC. Because without his hints, i won't be able to achieve this objective!
I would also like to say thanks to Optimus Prime and all the autobots to create such wonderful Faces!
thank you all! :-)
Last edited by silent warrior on 26 Feb 2012, 15:57, edited 1 time in total.
I use...
JDK 1.7
Java EE 6
JSF 2.2
PrimeFaces 4.0
Apache Tomcat 7
NetBeans 7.4

kukeltje Expert Member

Posts: 6376
Location: Netherlands
thanks for sharing, but could you change the subject of the first post! That way it is better visible in the forum
Ronald van Kuijk
______________________________
PrimeFaces 4.0, PrimeFaces plus 0.0.2 | Jboss 7.2.0 | Mojarra 2.1.18
Fedora 19, Firefox 26.0
Read the forum posting rules
Beginners: http://www.coreservlets.com/JSF-Tutorial/jsf2/


Posts: 8
Location: Pakistan
kukeltje wrote:
thanks for sharing, but could you change the subject of the first post! That way it is better visible in the forum

Is this ok now? ;)
if not, then Can you suggest a suitable subject for this post?
I use...
JDK 1.7
Java EE 6
JSF 2.2
PrimeFaces 4.0
Apache Tomcat 7
NetBeans 7.4


Return to General