How to create dynamically fileds during runtime ?
Hi All,
I need to create InputBox dynamically during runtime in a table. If user clicks a button I need to create one inputfield , one lable and one command button during runtime in a row (in a table).
Please share your knowledge and help me in this. If you throw some links or refernece text books that will help me a lot.
Thanks
Phani Kiran Kumar prakhya
# 1
You're talking about datatables? Just add a new row object to the list which is passed to the datatable.
JSF<h:form>
<h:dataTable binding="#{myBean.table}" value="#{myBean.rows}" var="row">
<h:column><h:inputText value="#{row.input}" /></h:column>
<h:column><h:outputText value="#{row.label}" /></h:column>
<h:column><h:commandButton value="submit" action="#{myBean.action}" /></h:column>
</h:dataTable>
<h:commandButton value="add row" action="#{myBean.add}" />
</h:form>
MyBeanprivate HtmlDataTable table; // + getter + setter
private List<Row> rows; // + getter + setter + initialization to prefill it
public void action() {
Row row = (Row) table.getRowData(); // retrieve the row object where the button was clicked on.
}
public void add() {
rows.add(new Row());
}
Rowprivate String input;
private String label;
// + getters + setters
You may find this article useful: http://balusc.xs4all.nl/srv/dev-jep-dat.html
# 2
I need to create Inputtext box dynamically. If I click a button then I have to create InputBoxes accordingly. I added follwing code in button action :
(Found out this in net only)
try {
HtmlInputText in = new HtmlInputText();
in.setId(getForm1().getId() + "text2"); // form1 is a JSF form object created at design time in JSF page
in.setValue("Phani");
//in.setValid(false);
in.setSize(300 );
in.setStyleClass("inputText" );
in.setRequired(true);
getForm1().getChildren().add(in);
in.setParent(getForm1());
in.saveState(ctx);
} catch(Exception e){
e.printStackTrace();
return null;
}
But I am not able see it on UI. I am not getting any exceptions. Please help me.
# 3
In JSF 1.1 the HtmlForm doesn't render children. Upgrade to JSF 1.2 or nest a h:panelGroup in the h:form and use it instead. Also see http://forum.java.sun.com/thread.jspa?threadID=5161699
# 4
Great !!!. Thanx a lot. It is working fine.
Please observe this piece of coding adding components.
public void addRow() {
HtmlInputText outputText = new HtmlInputText();
HtmlOutputText label= new HtmlOutputText();
HtmlCommandButton button= new HtmlCommandButton();
label.setId("label"+i);
button.setId("button1"+i);
outputText.setId( "out"+i );
i++;
outputText.setValue( "some label" );
label.setValue("Enter");
button.setValue("Delete");
dynamicForm.getChildren().add( label );
dynamicForm.getChildren().add( outputText );
dynamicForm.getChildren().add(button );
}
I need to add thos components in HTML table cell, each time in new row.
How to controll alignent and spacing of components ?. Is JSF gives controll during runtime ?.
# 5
You can apply css using setStyleClass().But isn't a datatable sufficient for you? Looking to what you're doing as far now, a dataTable should be a much better solution.
# 6
Thank you very much !!!. Everything working fine. I will look into that Stylesheet creation. If you have any reference link please send me.