Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

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="#{dataManager.refresh}" />  

<p:dataTable var="employees" value="#{dataManager.dataList}">  

</h:form>
 
Server Code...
public class DataManager implements Serializable
{
  private List dataList = new List();

  ..
  ..

  public void refresh()
  {
 
 
  }
}
0
prain
Asked:
prain
  • 6
  • 5
1 Solution
 
girionisCommented:
In your refresh method create a new list and then add the data in there

public void refresh()
{
    dataList = new List();
    dataList.add(...);
}

Open in new window

0
 
prainAuthor Commented:
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.dataList}" 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(...);

 
  }
}
0
 
girionisCommented:
First of all you have two syntax errors.

value="dataManager.dataList}"

Open in new window

You are missing a hash and a left curly bracket.
dataList.add(...);

Open in new window

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).
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
prainAuthor Commented:
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="dataManager")
@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;
      }
      
           
}
0
 
girionisCommented:
Try this

<p:dataTable value="#{dataManager.list}" var="o">
        <p:column>
#{o}
        </p:column>
      
      </p:dataTable>

Open in new window

0
 
prainAuthor Commented:
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="dataManager")
@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;
      }
}
0
 
prainAuthor Commented:
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("Reached 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"
0
 
girionisCommented:
Do you also update the data table?
0
 
prainAuthor Commented:
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.
0
 
girionisCommented:
You need to update the data table dynamically upon changing the data model. How do you call the update method? You said you use a command link. In that part of code you should update the data table. You should give an id to your data table and add an "update" property in your command link.

        update="tableId" 

Open in new window

0
 
prainAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now