Creating datatable through backing bean
I am working on a creating components such as HtmlOutputLabel and so on through the backing bean. I add the HtmlOutputLabel to a panel grid in the bean. Then I create an object of UIComponent in the bean an assign it to the panel grid. In the jsp i write
<h:panelGrid id="panelgridtest21" binding="#{Label1.component1}" />
where label1 is my bean and component1 is the UIComponent type object refering to HtmlPanelGrid type of object. I have been able to do this for all the components such as HtmlInputText etc but for HtmlDataTable. Plz can any1 help to create this through the bean.
# 1
here is a little example:
class MyBean {
private HtmlDataTable table = new HtmlDataTable(); // + getters and setters
{
UIColumn col1 = new UIColumn();
UIColumn col2 = new UIColumn();
List childrenList = table.getChildren();
table.setRows(2);
List colChild1 = col1.getChildren();
List colChild2 = col2.getChildren();
HtmlOutputLabel l1 = new HtmlOutputLabel();
HtmlOutputLabel l2 = new HtmlOutputLabel();
HtmlOutputLabel l3 = new HtmlOutputLabel();
HtmlOutputLabel l4 = new HtmlOutputLabel();
l1.setValue("cell1");
l2.setValue("cell2");
l3.setValue("cell3");
l4.setValue("cell4");
colChild1.add(l1);
colChild1.add(l2);
colChild2.add(l3);
colChild2.add(l4);
ListDataModel dm = new ListDataModel();
dm.setWrappedData(colChild1);
dm.setWrappedData(colChild2);
table.getChildren().add(col1);
table.getChildren().add(col2);
table.setValue(dm);
}
}
# 2
Thanks for you kind reply
Can I add this table to panelgrid, eg
In Bean:
public HtmlPanelGrid panelgrd= new HtmlPanelGrid();
UIComponent component = panelgrd;//getters setters
panelgrd.getChildren().add(table); //add the table from ure code to panelgrid
In JSP:
<h:panelGrid id="idnew" binding="#{MyBean.component }" />
I would be extrememely grateful if you could tell me thanks in advance.
# 3
This is an odd approach. Rather do
JSF<h:panelGrid binding="#{myBean.grid}" />
MyBeanprivate HtmlPanelGrid grid; // + getter + setter
public void doSomething() {
grid.getChildren().add(table);
}
# 4
Thanks for the code. I have modified it a little as follows:
In bean
for(int i=0;i<5;i++)
{
UIColumn col = new UIColumn();
List colChild = col.getChildren();
HtmlOutputLabel label = new HtmlOutputLabel();
label.setValue("cell"+i);
colChild.add(label);
ListDataModel dataModel = new ListDataModel();
dataModel.setWrappedData(colChild);
table.getChildren().add(col);
table.setValue(dataModel);
}
table.setBorder(1);
It is creating one row with 5 columns. Now how do I create the second row. Thanks again in advance.
# 5
You don't need. Just fill the datamodel behind the value of the datatable.
# 6
Well I donot understand, could you kindly explain how to do that. Thanks
# 7
Well, if you want to fill all rows manually, then use panelGrid instead of dataTable. In a dataTable just define the columns and then it's sufficient to pass a datamodel or collection to the dataTable value.
# 8
Due to some project constraints I have to use datatable. I am new to JSF so don't know anything about ListDataModel. My data is coming through an XML file and not a database so I have to fill up the table manually. Is there any way I can fill up the datamodel manually. As I said before I have been able to create one row, how do I create another row. I would be very thankful if anyone could tell me.
# 9
Basically, in a datatable you only define the columns. The datatable value accepts a Collection of DTO's (objects representing each row) as well as a DataModel which wraps this Collection. http://balusc.xs4all.nl/srv/dev-jep-dat.html might give some new insights.
# 10
The source of the data (XML vs database) shouldn't impact the choice of component. The nature of the data will determine that.