Search code examples
jsfrichfaces

RICH:dataTable - add new row


how can I add new row to extendedDataTable at the end of the list? Is there some solution?

JSF table example:

enter image description here

Can you help me?

UPDATE:

I'm able to add new item using for example a rich:popupPanel, but not by creating a new row in datatable. What I wanted to say, I'm able to save but not as elegant as using BalusCs JSF guide.

Part of XHTML:

<rich:extendedDataTable 
 id="tableDetail"
 value="#{myBdeCheck.dataListBde}"
 var="bdeItem">

<rich:column width="80px">
  <f:facet name="header">
     <h:outputText value="Směna" />
  </f:facet>
  <h:outputText value="#{bdeItem.dayShift}"/>
</rich:column>

<rich:column width="70px">
  <f:facet name="header">
     <h:outputText value="Karta" />
  </f:facet>
  <h:outputText value="#{bdeItem.bdeno}"/>
</rich:column>


Part of BEAN: package common;

@ViewScoped
@ManagedBean(name="myBdeCheck")
public class MyBdeCheck extends Tools
{
  /**/
  private static final long serialVersionUID = -6586004426692130933L;

  private Session session; 
  private List<BDE> dataListBde; //= new ArrayList<BDE>();
  private int currentIndexDetail;   // index for BDEDetail datatable

  private BDECheckView editedWork;  // one item of BdeCheckView
  private BDE BDEItem;              // one item of BdeData

  // Constructor 
  public MyBdeCheck()
  {
    editedWork = new BDECheckView();
    BDEItem = new BDE();
  }

  /** GET DATA FROM BDEData *******************
   * @param personalNum, dayShift [whole day: YYYY-MM-DD%]
   */
  private void criteriaCheck()
  {
    try 
    {
      Criteria criteria = session.createCriteria(BDE.class);
      {some restrictions}
      dataListBde = criteria.list();
    }  
    catch (Exception e) {...}
   }

  public void saveBde()
  {    
    try
    {
      DaoCrud.update(dataListBde, 'R');  // ulozeni do dtb
    }
    catch  {...}  
  }

  public void saveNew() {...}

// and GETTERS AND SETTERS 
}

Solution

  • Your dataTable is modeling a Collection:

    private List<BDE> dataListBde;
    

    To add a new row you would just need to add a new BDE() to the Collection.

    this.dataListBde.add(new BDE());
    

    Then you can just reRender your dataTable to see the new row.

    Finally, in your xhtml you can conditionally render an inputText or outputText:

    <rich:column width="70px">
        <f:facet name="header">
            <h:outputText value="Karta" />
        </f:facet>
        <h:outputText value="#{bdeItem.bdeno}" rendered="#{bdeItem.bdeno != null}"/>
        <h:inputText value="#{bdeItem.bdeno}" rendered="#{bdeItem.bdeno == null}"/>
    </rich:column>