Access values from dynamic input text generated from ui:repeat JSF

1k Views Asked by At
<p:outputPanel id="panel">
<ui:repeat value="#{dataController.display()}" 
var="item" rendered="#{Bean.showtable}">
    <b:row>
    <b:column col-md="5">
    <h:outputText value="#{item.name}" style="font-family: verdana;font-size:16px;margin-bottom:15px;"></h:outputText>
    </b:column>     

    <b:column col-md="7">
    <h:inputText style="width:200px;height:30px;margin-bottom:15px;" 
                            autocomplete="off"></h:inputText>
    </b:column>                        
    </b:row>
    </ui:repeat>

In the above code, I have used ui:repeat for displaying the names of the items in a list in output text alongwith the input text for entering the values of the items.

The input text depends on the values in the list i.e dynamically generated.

I need to access the values from the input text and add them to List.

Can anyone please suggest me an approach to access the values from input text to bean/list despite using ui:repeat once to display the input text?

I have tried to create an empty list and again using ui:repeat only for input text.. tried to access values from input text.But ui:repeat doesnot work again.. as it was already used once for displaying.

I am new to JSF.Any help would be appreciated.Thankyou.

1

There are 1 best solutions below

4
Tonkichi On BEST ANSWER

Don't use empty list. Initialize it with null or empty values.
Let's say we have inputs as your list, your bean should look like this.

@Named
@ViewScoped
public class DataController implements Serializable {

    private List<String> inputs;

    // getters and setters

    @PostConstruct
    public void init() {
        inputs = new ArrayList<String>();
    }

    public List<Bean> getDisplay() {
        List<Bean> display = new ArrayList<Bean>();

        // add values to display

        for (int i = inputs.size(); i < display.size(); i++) {
            inputs.add("");
        }

        return display;
    }

    // for testing inputs
    public void testInputs() {
        for (String input : inputs) {
            System.out.println(">>>>>" + input);
        }
    }

}

xhtml

<ui:repeat value="#{dataController.display()}" varStatus="idx" ...>
    ...
    <h:inputText value="#{dataController.inputs[idx.index]}" style="width:200px;height:30px;margin-bottom:15px;" autocomplete="off"></h:inputText>
    ...
</ui:repeat>

<p:commandButton value="Test Inputs" action="#{dataController.testInputs}" update="@form" />

Hope this helps.