there is a table

<h:panelGroup> <div class="row"> <div class="col-lg-12"> <br/> <h:dataTable value="#{ datacontroller.getList()}" var="value" styleClass="table table-bordered"> <h:column> <f:facet name="header">Data</f:facet> <h:form> <h:inputText value="#{data.inputvalue}> </h:inputText> </h:form> </h:column> </h:dataTable> </div> </div> </h:panelGroup> 

The getList () method returns an ArrayList of a certain length, for example [a, b, c], the base of which is a table with three input fields (). Numeric values ​​are entered in these fields.

  @Named @SessionScoped public class Data implements Serializable { private String inputvalue; public String getInputvalue() { return inputvalue; } public void setInputvalue(String inputvalue) { this.inputvalue = inputvalue; } 

It is necessary to process the values ​​entered in these fields in another method of calculate () which is called by the "OK" button.

  <h:commandButton value="Ok" action="#{datacontroller.calculate()}" /> 

How to save the entered values ​​in these fields and and process? Thank.

    1 answer 1

    Thus, as in your example, you get three forms (or how long the list will be there). No means available to keep them together at once.
    Forms work independently of each other.
    The form should frame all its fields and buttons.
    Then you write in datatable var="value" , and in column use some kind of data . What name was given to the variable is what should be used.

     <h:form> ... <h:dataTable value="#{datacontroller.list}" styleClass="table table-bordered" var="data"> <h:column> <f:facet name="header">Data</f:facet> <h:inputText value="#{data.inputvalue}"/> </h:column> </h:dataTable> ... <h:commandButton value="Ok" action="#{datacontroller.calculate}"/> ... </h:form> 

    Controller

     public class Datacontroller { private List<Data> list; ... List<Data> getList() { if (list == null) list = ... // создать и заполнить return list; } ... public void calculate() { ... for (Data data : getList()) { String inputvalue = data.getInputvalue(); ... } ... } } 

    Data

     public class Data { private String inputvalue; public String getInputvalue() { return inputvalue; } public void setInputvalue(String inputvalue) { this.inputvalue = inputvalue; } } 

    If data is a List<String> , then here https://stackoverflow.com/questions/21236170/using-hdatatablehinputtext-on-a-liststring-doesnt-update-model-values

     <h:form> ... <h:dataTable value="#{datacontroller.list}" styleClass="table table-bordered" var="data" binding="#{table}"> <h:column> <f:facet name="header">Data</f:facet> <h:inputText value="#{datacontroller.list[table.rowIndex]}"/> </h:column> </h:dataTable> ... <h:commandButton value="Ok" action="#{datacontroller.calculate}"/> ... </h:form> 

    You can change the list item by index. To get the index in this case, use the method with the table's binding to the variable table . Through table we get the ability to extract the properties of the table (and not only). So table.rowIndex is the index of the current row. What we need. The table variable itself is created automatically in the request scope.

    Controller

     public class Datacontroller { private List<String> list; ... List<String> getList() { if (list == null) list = ... // создать и заполнить return list; } ... public void calculate() { ... for (String data : getList()) { ... } ... } } 
    • but if I write value = "# {data.inputvalue}" then an error warning appears. The task is to save the entered data to the ArrayList in the bean-not. Found link stackoverflow.com/questions/9747157/… But also not entirely clear - Mac
    • What other warning? At the table itself, here and there, everything is fine. What is the getList() list? ArrayList<Data>? Seems understood. This is because your Data @Named @SessionScoped for some ... I think there is a name conflict here. Write something else instead of data. Although the same value , only to the same everywhere. both in var and in input. var="value" <h:input value="#{value.inputvalue}"/> - Sergey
    • well in the sense of a yellow wavy line underlined. ArrayList <String> is returned in getList () method - Mac
    • changed all the same does not work - Mack
    • Not just as emphasized. There must be some explanation. And if ArrayList<String> , then where does the String property of inputvalue say please? Then you would just write <h:input value="#{value}"/> . But that won't work either. Editing requires that the items in the list are either bean or map. It is not the list element that is being edited, but the element properties Stringa has nothing to change. stackoverflow.com/questions/21236170/… - Sergey