2

i am trying to create dynamic datatable without binding.Because binding is not working with view scoped bean. But the problem is dynamic datatable rendered only once. If i try to change the values it will display empty rows.....

ICEfaces 2.0.0 JSF 2.0 JSTL1.2 ViewScoped So binding is not working

Welcome.xhtml

<h:form id="form">
<h:outputText value="Welcome to ICEfaces 2"/>
<ice:commandButton value="Start" action="#{table.start}"/>
<ice:commandButton value="Update" action="#{table.update}"/>
<ice:dataTable id="table" value="#{table.list}" var="currentRow" >
<ice:column rendered="false" id="row">
<ice:rowSelector multiple="false" selectionListener="#{table.selectionRowTable}" />
</ice:column>
</ice:dataTable>
</h:form> 

JavaBean code

@ManagedBean (name="table")
@ViewScoped
public final class TestTable implements Serializable {

public sample list[]=new sample[5];

public sample[] getList() {
return list;
}

public void setList(sample[] list) {
this.list = list;
}

HtmlDataTable table=null;


public void createTable()
{
table=(HtmlDataTable)findComponent(FacesContext.getCurrentInstance().getViewRoot(), "table");

HtmlCommandButton commandSortHeader = new HtmlCommandButton();
commandSortHeader.setId("header");

HtmlOutputText headerComponent = new HtmlOutputText();
headerComponent.setId("headerComponentId");
headerComponent.setValue("Header");

ExpressionFactory factory = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
MethodExpression methodsexpression = factory.createMethodExpression(FacesContext.getCurrentInstance().getELContext(), "#{table.Test}", null, new Class[]{ActionEvent.class});
MethodExpressionActionListener actionListener = new MethodExpressionActionListener(methodsexpression);
commandSortHeader.addActionListener(actionListener);
commandSortHeader.getChildren().add(headerComponent);

UIColumn column=new UIColumn();
column.setId("column");
HtmlOutputText t=new HtmlOutputText();
t.setValueExpression("value",FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createValueExpression(FacesContext.getCurrentInstance().getELContext(), "#{currentRow.name}",String.class));
t.setId("Text");
column.getChildren().add(t);
column.setHeader(commandSortHeader);
table.getChildren().add(column);
}

public void start()
{
for(int i=0;i<5;i++)
list[i]=new sample("OLD"+i);
createTable();
}

public void update()
{
for(int i=0;i<5;i++)
list[i]=new sample("NEW"+i);
}


public void Test(ActionEvent e)
{
System.out.println("Header Clicked");
}

public void selectionRowTable(RowSelectorEvent e)
{
System.out.println("Row Clicked");
}

public UIComponent findComponent(UIComponent parent, String id) {
if (id.equals(parent.getId())) {
return parent;
}
Iterator<UIComponent> kids = parent.getFacetsAndChildren();
while (kids.hasNext()) {
UIComponent kid = kids.next();
UIComponent found = findComponent(kid, id);
if (found != null) {
return found;
}
}
return null;
}


public class sample{

public String name;

public sample(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


}
} 

If i update the list values it never get printed and empty rows displayed....

I have frustrated for the past 1 week due to this .......

pls reply for this....

Looking for solution

  • I assume that "changing the values" means clicking on the update button. You should verify that your bean isn't recreated after clicking this button (set a breakpoint in the constructor). – Matt Handy Mar 02 '12 at 07:27
  • Your assumption is correct. If i click the update button,it will call update method which in turns modify the list values. But it never get reflected in the browser and all data in the datatable disappear. So that i can see only empty datatable..... Bean is not being recreated i checked it. If the scope is request then only it will recreate it. In our case it is view scope. Where could be the problem ?? – tgsankarbabu Mar 02 '12 at 08:27
  • 1
    It is unusual that you create your table in the controller. You better should do it in the view. This could be the reason that the table is empty after update since it will be re-rendered. As workaround you could try to call your `createTable` method in your `update` method. But I strongly recommend to re-think your design. – Matt Handy Mar 02 '12 at 09:07
  • i want to add table dynamically in view scope.. How is it possible ?? – tgsankarbabu Mar 02 '12 at 15:26

0 Answers0