0
<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 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much for the solution.
But dataController.display() is a method which returns List<Bean> and I cant initialize display method initially in init() as it needs to be invoked after another method call.
Can u please tell me if i can use nested ui:repeat or any other approach to be suggested?
you just have to move adding values to inputs in getDisplay() method. I have updated my answer to reflect that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.