• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating Datatable dynamically

 
ved gunjan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My requirment is that i have to create a dynamic datatable and which contains listing of Questions and its respectives Answer. These questions and answer come from the server and along with the answer i also get the answer type , which tells what is the type of the UIComponent that is associated with the answer say input text,select many,select one etc., so i have to dynamically create these UIComponent inside the datable based on the answer type.
Actually i'm having the logic to create the components which is based on the answer type which comes from the backend and this answer type define what kind type of component(component for single answers/component for multiple answer,input and other types) needs to be created.(this will be done in the if-else block)

My problem is that im not able to create this componets dynamically in the data table.
In the code below , i have binded the datatable and in the java method i 'm trying to create the ui components(as of now im creating it without the creation logic of answer type).
The idea was to just try and see whether the components are created and is getting added to the datable.

when i run this code im getting no output.



Please suggest me where im going wrong and what is the correct approach.

I'm really in a dire straits.

Here's the code:

JSP

---------------------------------

<h:dataTable id="mainTable" binding="#{dynamicQuestions.dataTable}" value="#{requirementBean.questionList}"

var="questions" border="1"/>



Java method

-------------------------------

public UIData getDataTable()

{

Application application = FacesContext.getCurrentInstance().getApplication();
UIViewRoot uiViewRoot = FacesContext.getCurrentInstance().getViewRoot();
HtmlDataTable dataTable =(HtmlDataTable)uiViewRoot.findComponent("mainTable");

//------------------- adding HtmlOutputText ---------------//
String vbStr = "#{questions.questionData}";
ValueBinding vb = FacesContext.getCurrentInstance().getApplication().createValueBinding(vbStr);
HtmlOutputText output = new HtmlOutputText();
output.setId("HtmlOutputText1");
output.setValueBinding("value", vb);


//------------------- adding HtmlSelectOneMenu ---------------//
vbStr = "#{questions.itemList}";
vb = FacesContext.getCurrentInstance().getApplication().createValueBinding(vbStr);

HtmlSelectOneMenu hsom = new HtmlSelectOneMenu();
hsom.setId("HtmlSelectOneMenu2");
hsom.setValueBinding("value",vb);


//------------------- adding HtmlSelectManyListbox ---------------//


vbStr = "#{questions.itemList}";
vb = FacesContext.getCurrentInstance().getApplication().createValueBinding(vbStr);

HtmlSelectManyListbox hsmlb = new HtmlSelectManyListbox();
hsmlb.setId("HtmlSelectManyListbox3");
hsmlb.setValueBinding("value",vb);


//datatable column
UIColumn column = new UIColumn();
column.setId("UIColumn4");
column.getChildren().add(output);
column.getChildren().add(hsom);
column.getChildren().add(hsmlb);

dataTable.getChildren().add(column);

return dataTable;

}





Thanks

Ved
 
Ravisekhar Kopparthi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSF has a complex lifecycle. It all boils down to if you have considered the JSF life cycle in the code that you pasted.

To me it appears that the <h:datable> is rendered before calling your method.

I think you need to use IBM's scriptcollector
<h:ScriptCollector pre-Render ="onPageLoadBegin()">

and put all the pre render logic in onPageLoadBegin(facesContext)

Hope this helps.

Thanks
Ravisekhar Kopparthi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic