>> I'm struggling to create a binding to a UIInput element in a DataTable
row.
>> (Context: I'm trying to do validation across multiple fields in the same
>> row.) 
>> ...
>> I have a backing bean for the main page (PlayBB; managed-bean-scope:
>> "session") which contains a List of backing beans for each row (NameBB;
>> managed-bean-scope: "none"). I've observed (through logging) that the
>> binding occurs exactly once per page and not once per row as I had
>> expected.

>No, this is expected behavior.   Your UIInput only exists once, but it
>has as many states as rows in the table.   This is where you need to
>call UIData.setRowIndex(row) in order to put the UIInput into the
>correct state.

Thanks. Just to finish this thread, here's what I had to do.

My above attempt was to bind each row element to a UIInput in my row object.
However, since there is really only one UIInput in multiple states, I need
to bind the row column to a UIInput in the overall backing bean. I also need
to bind the DataTable to a UIData in the backing bean. I can have a hidden
field after the table with a validator in the backing bean in order to
validate the rows.

I thus have access to the UIData object (for getRowCount() and
setRowIndex()) and the UIInput values. 

Code example below, for anyone stumbling across this thread in the future!

-David-

---------play.jsp---------

<%@ taglib uri="http://java.sun.com/jsf/html"; prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core"; prefix="f" %>

<html>
<head><title>JSF Play</title></head>
<body>

<f:view>
<h:form id="form">

<h:commandButton value="Add Names" type="submit" action="#{play.addNames}"/>

<h:dataTable id="names" value="#{play.nameBBList}" var="name"
binding="#{play.tableData}" >
 <h:column>    

    <h:inputText id="first" value="#{name.first}" maxlength="10" size="10"
binding="#{play.firstInput}" />
    <h:inputText id="last" value="#{name.last}" maxlength="10" size="10"
binding="#{play.lastInput}" />

 </h:column>
</h:dataTable>

<h:inputHidden id="datacheck" validator="#{play.validateNames}"
value="dummy"/>

</h:form>
</f:view>

</body></html>
---------play.jsp---------

---------PlayBB.java---------
package jsfplay;
import java.util.*;
import javax.faces.component.*;
import javax.faces.context.*;
import javax.faces.validator.*;
import javax.faces.application.*;
public class PlayBB {
    
  private UIInput firstInput;
  private UIInput lastInput;
  private UIData table;

  private List<NameBB> nameBBList=new ArrayList<NameBB>();

  public PlayBB(){ }
    
  public int getNumberOfNames() { return nameBBList.size();  }
  
  public List<NameBB> getNameBBList() { return nameBBList;}

  public String addNames() {
    // fill in some names.
    NameBB newName = new NameBB("Joe","Smith");
    nameBBList.add(newName);
    newName = new NameBB("Ralph","Johnson");
    nameBBList.add(newName);
    return null;
  }

  public void setFirstInput(UIInput firstInput) {
this.firstInput=firstInput; }
  public UIInput getFirstInput() {return firstInput; }
  public void setLastInput(UIInput lastInput) { this.lastInput=lastInput; }
  public UIInput getLastInput() {return lastInput; }
  public void setTableData(UIData table) { this.table=table; }
  public UIData getTableData() {return table; }

  public void validateNames(FacesContext context, UIComponent toValidate,
Object value) throws ValidatorException {
    
    for(int i=0; i<table.getRowCount(); ++i) {
      table.setRowIndex(i);

      // Now do validation comparing firstInput.getValue() and
      // lastInput.getValue()
    }
  }
}
---------PlayBB.java---------

---------NameBB.java---------
package jsfplay;
import javax.faces.component.*;
public class NameBB {
    
    //properties
  private String first="";
  private String last="";
  
  public NameBB(){ }
  public NameBB(String first, String last)
  {
    this.first=first;
    this.last=last;
  }
    
  public String getFirst() {return first;}
  public String getLast() {return last;}

  public void setFirst(String first) {this.first=first;}
  public void setLast(String last) {this.last=last;}

}
---------NameBB.java---------
-- 
View this message in context: 
http://www.nabble.com/Binding-to-elements-in-DataTable-tf2041529.html#a5621639
Sent from the MyFaces - Users forum at Nabble.com.

Reply via email to