1

i need to collect homework scores of students in a class. what i have done so far is

  1. get the list of student ids
  2. using ui:repeat i loop thru the list and for each student id, i

    2a)display an h:inputText whose value is the current student id, then

    2b)to the right of textbox in (2a) above, i display another h:inputText for the teacher to enter the score for that student(for now that value is a dummy variable just to get the page to display).

  3. i have a single commandbutton to submit all the data.

For example, if i have 20 students, i would have 20 rows, where each row has two h:inputText, one already containing the current student_id and the other is empty for the teacher to type the score.

How can i collect these values correctly, so that the right student id is linked to the right score.

Note that i cant hardcode the number of textfields cos the number of students in a class can change at any time.

obinini
  • 111
  • 8
  • take a look at http://stackoverflow.com/a/6743138/617373 also take a look at http://www.primefaces.org/showcase/ui/datatableEditing.jsf;jsessionid=164B58A98A6016F43E86A846B0DB637D – Daniel May 08 '12 at 09:55
  • thanks Daniel. The first link actually is better for me, but considering that i have 2 sets for each students. how can i use another array to keep the two in sync using index. i mean how can i make sure that when i submit the button, the value in say #myBean.score[0] is the one entered for the first sudent id. cos i need to identify student_id/score entered so i can build a batch sql insert appropriately – obinini May 08 '12 at 10:08
  • you can have an array (or arraylist) of objects , each object will hold 2 strings for example – Daniel May 08 '12 at 10:09
  • yeaaa, that makes sense. Thanks a ton, 'll try and get back. – obinini May 08 '12 at 10:14

1 Answers1

2

Create a model object.

public class Score {

    private Long studentId;
    private BigDecimal teacherScore;

    // Getters/setters.
}

Have a list of them in some JSF managed bean.

private List<Score> scores;

Use <h:dataTable> to present them.

<h:dataTable value="#{bean.scores}" var="score">
    <h:column><h:inputText value="#{score.studentId}" /></h:column>
    <h:column><h:inputText value="#{score.teacherScore}" /></h:column>
</h:dataTable>
<h:commandButton value="Save" action="#{bean.save}" />
<h:messages />

That's it.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thanks u so much BalusC and Daniel too. u guys are the best. your ideas have helped me solve the probem. – obinini May 08 '12 at 21:37