Solved

Adding rows to a p:datatable dynamically

Posted on 2014-03-29
11
333 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
  • 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
zeroFront challenge 7 71
wordsWithout 49 80
changeXy challenge 13 58
thymeleaf natural templating vs JSP 2 30
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now