[solved] Reusing same datatable for different datasources

Components, Ajax Framework, Utilities and More.
Post Reply
User avatar
silent warrior
Posts: 8
Joined: 09 Feb 2012, 09:35
Location: Pakistan

14 Feb 2012, 10:11

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

Hamsterbau
Posts: 349
Joined: 28 Dec 2011, 17:44

14 Feb 2012, 11:16

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:

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;;
	}
}
Your datatable simply uses the data which was initialized in MySessionBean:

Code: Select all

<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

caioquirino
Posts: 70
Joined: 12 Jan 2012, 15:06
Location: Brazil

14 Feb 2012, 12:06

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

User avatar
silent warrior
Posts: 8
Joined: 09 Feb 2012, 09:35
Location: Pakistan

15 Feb 2012, 08:16

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

User avatar
silent warrior
Posts: 8
Joined: 09 Feb 2012, 09:35
Location: Pakistan

15 Feb 2012, 08:28

@Hamsterbau! Thanks for your reply. but how can we do this if our data come from different data sources?
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

Hamsterbau
Posts: 349
Joined: 28 Dec 2011, 17:44

15 Feb 2012, 23:47

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:

Code: Select all

<p:dataTable value="#{mySessionBean.dataFromAnySource}" var="anySource"> ... <p:dataTable>
And your bean just delivers the right data:

Code: Select all

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

User avatar
silent warrior
Posts: 8
Joined: 09 Feb 2012, 09:35
Location: Pakistan

23 Feb 2012, 06:16

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:

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>
and in the backing bean:

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
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: 8499
Joined: 17 Jun 2010, 13:34
Location: Netherlands

23 Feb 2012, 14:14

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 5.2, PrimeFaces plus 0.0.2 | JbossWildfly 8.1| Mojarra 2.2.8
Fedora 21, Firefox 'most recent'
Read the forum posting rules
Beginners: https://jsf.zeef.com/bauke.scholtz

User avatar
silent warrior
Posts: 8
Joined: 09 Feb 2012, 09:35
Location: Pakistan

25 Feb 2012, 09:29

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

Post Reply
  • Information
  • Who is online

    Users browsing this forum: Baidu [Spider] and 10 guests