Page 1 of 1

[solved] Reusing same datatable for different datasources

Posted: 14 Feb 2012, 10:11
by silent warrior
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

Re: how to reuse the same datatable for different datasource

Posted: 14 Feb 2012, 11:16
by Hamsterbau
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:

Re: how to reuse the same datatable for different datasource

Posted: 14 Feb 2012, 12:06
by caioquirino
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 =)

Re: how to reuse the same datatable for different datasource

Posted: 15 Feb 2012, 08:16
by silent warrior
caioquirino wrote:When i go to my work, i post one example =)
Thanks in Advance! I'll be waiting for it! :)

Re: how to reuse the same datatable for different datasource

Posted: 15 Feb 2012, 08:28
by silent warrior
@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;;
	}
}

Re: how to reuse the same datatable for different datasource

Posted: 15 Feb 2012, 23:47
by Hamsterbau
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:

[Solved] Reusing the same datatable for different datasource

Posted: 23 Feb 2012, 06:16
by silent warrior
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! :-)

Re: how to reuse the same datatable for different datasource

Posted: 23 Feb 2012, 14:14
by kukeltje
thanks for sharing, but could you change the subject of the first post! That way it is better visible in the forum

Re: how to reuse the same datatable for different datasource

Posted: 25 Feb 2012, 09:29
by silent warrior
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?