Solved

Adding rows to a p:datatable dynamically

Posted on 2014-03-29
11
338 Views
Last Modified: 2014-04-01
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
Comment
Question by:prain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 39965892
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
 

Author Comment

by:prain
ID: 39966278
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
 
LVL 35

Expert Comment

by:girionis
ID: 39966345
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
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 

Author Comment

by:prain
ID: 39966352
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
 
LVL 35

Expert Comment

by:girionis
ID: 39966418
Try this

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

Open in new window

0
 

Author Comment

by:prain
ID: 39966467
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
 

Author Comment

by:prain
ID: 39966513
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
 
LVL 35

Expert Comment

by:girionis
ID: 39967633
Do you also update the data table?
0
 

Author Comment

by:prain
ID: 39967999
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
 
LVL 35

Accepted Solution

by:
girionis earned 250 total points
ID: 39968566
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
 

Author Closing Comment

by:prain
ID: 39969035
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question