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
[solved] Reusing same datatable for different datasources
- silent warrior
- Posts: 8
- Joined: 09 Feb 2012, 09:35
- Location: Pakistan
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
JDK 1.7
Java EE 6
JSF 2.2
PrimeFaces 4.0
Apache Tomcat 7
NetBeans 7.4
-
- Posts: 409
- Joined: 28 Dec 2011, 17:44
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:
Your datatable simply uses the data which was initialized in MySessionBean:
I hope i write not to crazy, so some parts are "understandable"
Beancode:
Code: Select all
@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;;
}
}
Code: Select all
<p:dataTable value="#{mySessionBean.data}" var="myInterface"> ... </p:dataTable>
Primefaces 8.0.7 (PF Extensions 8.0)
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22
-
- Posts: 70
- Joined: 12 Jan 2012, 15:06
- 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 =)
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
- silent warrior
- Posts: 8
- Joined: 09 Feb 2012, 09:35
- Location: Pakistan
Thanks in Advance! I'll be waiting for it!caioquirino wrote:When i go to my work, i post one example =)
I use...
JDK 1.7
Java EE 6
JSF 2.2
PrimeFaces 4.0
Apache Tomcat 7
NetBeans 7.4
JDK 1.7
Java EE 6
JSF 2.2
PrimeFaces 4.0
Apache Tomcat 7
NetBeans 7.4
- silent warrior
- Posts: 8
- Joined: 09 Feb 2012, 09:35
- Location: Pakistan
@Hamsterbau! Thanks for your reply. but how can we do this if our data come from different data sources?
e.g:
Beancode:
e.g:
Beancode:
Code: Select all
@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
JDK 1.7
Java EE 6
JSF 2.2
PrimeFaces 4.0
Apache Tomcat 7
NetBeans 7.4
-
- Posts: 409
- Joined: 28 Dec 2011, 17:44
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:
And your bean just delivers the right data:
What really matters is the data not the source it comes from
Code: Select all
<p:dataTable value="#{mySessionBean.dataFromAnySource}" var="anySource"> ... <p:dataTable>
Code: Select all
public List<Data> getDataFromAnySource() { ... }
Primefaces 8.0.7 (PF Extensions 8.0)
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22
- silent warrior
- Posts: 8
- Joined: 09 Feb 2012, 09:35
- Location: Pakistan
By the grace of God. After lots of struggle! i finally achieved this!
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:
and in the backing bean:
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!
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:
Code: Select all
<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>
Code: Select all
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
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
JDK 1.7
Java EE 6
JSF 2.2
PrimeFaces 4.0
Apache Tomcat 7
NetBeans 7.4
- silent warrior
- Posts: 8
- Joined: 09 Feb 2012, 09:35
- Location: Pakistan
Is this ok now?kukeltje wrote:thanks for sharing, but could you change the subject of the first post! That way it is better visible in the forum
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
JDK 1.7
Java EE 6
JSF 2.2
PrimeFaces 4.0
Apache Tomcat 7
NetBeans 7.4
-
- Information
-
Who is online
Users browsing this forum: No registered users and 7 guests