prain
asked on
Adding rows to a p:datatable dynamically
Hello,
I have a large web page with multiple components. Part of it is on click of a button I must add several rows to a p:datatable. Every time I click the button I must first delete existing rows in the table and add fresh rows starting from the beginning. So the table's data is always new.
<h:form>
<p:commandButton value="Add Data" actionListener="#{dataMana ger.refres h}" />
<p:dataTable var="employees" value="#{dataManager.dataL ist}">
</h:form>
Server Code...
public class DataManager implements Serializable
{
private List dataList = new List();
..
..
public void refresh()
{
}
}
I have a large web page with multiple components. Part of it is on click of a button I must add several rows to a p:datatable. Every time I click the button I must first delete existing rows in the table and add fresh rows starting from the beginning. So the table's data is always new.
<h:form>
<p:commandButton value="Add Data" actionListener="#{dataMana
<p:dataTable var="employees" value="#{dataManager.dataL
</h:form>
Server Code...
public class DataManager implements Serializable
{
private List dataList = new List();
..
..
public void refresh()
{
}
}
ASKER
Hi girionis,
In my Jboss, when I create a simple p:dataTable and attach a dataList in the bean, jboss complains "Property Cannot Be Resolved". What could be the reason?
<p:dataTable value="dataManager.dataLis t}" var="o"> </p:dataTable>
then in my DataManager I have it as a ManagedBean and SessionsScoped.
@ManagedBean
@SessionScoped
public class DataManager implements Serializable
{
private List dataList = new List();
..
..
public void refresh()
{
dataList = new List();
dataList.add(...);
}
}
In my Jboss, when I create a simple p:dataTable and attach a dataList in the bean, jboss complains "Property Cannot Be Resolved". What could be the reason?
<p:dataTable value="dataManager.dataLis
then in my DataManager I have it as a ManagedBean and SessionsScoped.
@ManagedBean
@SessionScoped
public class DataManager implements Serializable
{
private List dataList = new List();
..
..
public void refresh()
{
dataList = new List();
dataList.add(...);
}
}
First of all you have two syntax errors.
value="dataManager.dataList}"
You are missing a hash and a left curly bracket.dataList.add(...);
The above is not valid java code. I added there just to show you how it should be done. You need to replace it with the actual object you are trying to add in your list (and therefore in your data table).
ASKER
Ok. Thanks. But now I expected
one
two
to be shown in the browser. But it comes out blank.
My simple code is shown below...
<p:dataTable value="#{dataManager.list} " var="o">
<p:column>
</p:column>
</p:dataTable>
@ManagedBean(name="dataMan ager")
@SessionScoped
public class DataManager {
private List<String> list;
public DataManager() {
// TODO Auto-generated constructor stub
list = new ArrayList<String>();
list.add("one");
list.add("two");
}
public List<String> getList()
{
return list;
}
public void setList(List<String> aList)
{
this.list = aList;
}
}
one
two
to be shown in the browser. But it comes out blank.
My simple code is shown below...
<p:dataTable value="#{dataManager.list}
<p:column>
</p:column>
</p:dataTable>
@ManagedBean(name="dataMan
@SessionScoped
public class DataManager {
private List<String> list;
public DataManager() {
// TODO Auto-generated constructor stub
list = new ArrayList<String>();
list.add("one");
list.add("two");
}
public List<String> getList()
{
return list;
}
public void setList(List<String> aList)
{
this.list = aList;
}
}
Try this
<p:dataTable value="#{dataManager.list}" var="o">
<p:column>
#{o}
</p:column>
</p:dataTable>
ASKER
Here is my code, what I have now.
But all I can see is
"No records found"
Where this message comes from?
<p:dataTable value="#{dataManager.list} " var="o">
<p:column>
#{o}
</p:column>
</p:dataTable>
@ManagedBean(name="dataMan ager")
@SessionScoped
public class DataManager {
private List<String> list;
public void init() {
// TODO Auto-generated constructor stub
list = new ArrayList<String>();
list.add("one");
list.add("two");
}
public List<String> getList()
{
return list;
}
public void setList(List<String> aList)
{
this.list = aList;
}
}
But all I can see is
"No records found"
Where this message comes from?
<p:dataTable value="#{dataManager.list}
<p:column>
#{o}
</p:column>
</p:dataTable>
@ManagedBean(name="dataMan
@SessionScoped
public class DataManager {
private List<String> list;
public void init() {
// TODO Auto-generated constructor stub
list = new ArrayList<String>();
list.add("one");
list.add("two");
}
public List<String> getList()
{
return list;
}
public void setList(List<String> aList)
{
this.list = aList;
}
}
ASKER
OK. My mistake not having as the constructor.
I add a constructor. Then I can see the first items coming ok.
public void DataManager() {
// TODO Auto-generated constructor stub
list = new ArrayList<String>();
list.add("one");
list.add("two");
}
Then I added another function to the class
public void updade()
{
System.out.println("Reache d Updater");
list = new ArrayList<String>();
list.add("three");
list.add("four");
list.add("five");
}
Then from a commadLink I trigger the update()
I can see the println message prints on the console which means the method update() was called. But the List on the browser stays the same. I expected that to change to "three", "four" and "five"
I add a constructor. Then I can see the first items coming ok.
public void DataManager() {
// TODO Auto-generated constructor stub
list = new ArrayList<String>();
list.add("one");
list.add("two");
}
Then I added another function to the class
public void updade()
{
System.out.println("Reache
list = new ArrayList<String>();
list.add("three");
list.add("four");
list.add("five");
}
Then from a commadLink I trigger the update()
I can see the println message prints on the console which means the method update() was called. But the List on the browser stays the same. I expected that to change to "three", "four" and "five"
Do you also update the data table?
ASKER
Nop. I am not sure if @SessionScope vs. @ViewScope make a difference. I can see the data getting changed in the bean, but the view (HTML) never get updated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
PERFECT!!!!!
Thank You Very Much. Yes somehow I did not realize that the update property must be set. Phew, coming from ASP.NET background this is different. Thanks again.
Thank You Very Much. Yes somehow I did not realize that the update property must be set. Phew, coming from ASP.NET background this is different. Thanks again.
Open in new window