Link to home
Start Free TrialLog in
Avatar of prain
prainFlag for United States of America

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="#{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()
  {
 
 
  }
}
Avatar of girionis
girionis
Flag of Greece image

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

Avatar of prain

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.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(...);

 
  }
}
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).
Avatar of prain

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="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;
      }
      
           
}
Try this

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

Open in new window

Avatar of prain

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="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;
      }
}
Avatar of prain

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("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"
Do you also update the data table?
Avatar of prain

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
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prain

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.