1
votes

I have a Vector of Scores for test grades, as a property of my Assignment class.

I have an Edit Box on an xpage where I want to edit the value and write back to the Vector in the managed bean. The value binding of the Edit Box is: <xp:this.value><![CDATA[#{rpt.scores[rowIndex]}]]></xp:this.value> Where rpt is the Assignment object from my bean. The Edit Box is in a repeat control because I don't know how many students will be taking the test each time. So I am using the rowIndex of the repeat control to identify which element of the Scores Vector I want to bind to.

It is reading the value from the Scores Vector correctly, but I cannot seem to change the value and have it written back to the Vector.

How do I get it to write the value back to the Scores[n] element of the Assignment class?

here is the Assignment Class from my bean:

package com.logickey.gradebook;

import java.io.Serializable;
import java.util.Vector;


public class Assignment implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -1500382996574385949L;
    private String Name;
    private String Teacher;
    private String AssignNum;
    private String AssignDate;
    private Vector<String> Scores;

    public Assignment() {
        Name = "";
        Teacher = "";
        AssignNum = "";
        AssignDate = "";
        Scores = new Vector<String>();
    }
    public Assignment(String name, String teacher, String assignNum, String assignDate, Vector<String> scores){
        Name = name;
        Teacher = teacher;
        AssignNum = assignNum;
        AssignDate = assignDate;
        Scores = scores;
    }


    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getTeacher() {
        return Teacher;
    }

    public void setTeacher(String teacher) {
        Teacher = teacher;
    }

    public String getAssignNum() {
        return AssignNum;
    }

    public void setAssignNum(String assignNum) {
        AssignNum = assignNum;
    }

    public String getAssignDate() {
        return AssignDate;
    }

    public void setAssignDate(String assignDate) {
        AssignDate = assignDate;
    }


    public Vector<String> getScores() {
        return Scores;
    }

    public void addScore(String input) {
        if (Scores==null) {
            Scores = new Vector<String>();
        }
        Scores.add(input);
    }


}
3

3 Answers

2
votes

Per is right. However I would suggest to take it one step further. Your scores Vector could be a class of its own including student name and score.

you need a getScores() and setScores(Vector newValues) method. The repeat control will take care to insert at the right position.

If you use a custom class you need get/set methods on it. then you can bind fields e. g. rpt. Student

You also might consider to visit the Collection framework to see if there is a better fit:

  • Vector keeps the insert order
  • Set : ensures no duplicates (implement Comparble)
  • Tree Set : keeps stuff sorted automatically

There are more, have fun!

Let us know how it goes

1
votes

Try with a setScores method instead of a addScores method. You need a getter and a setter for an editable control.

0
votes

Thanks to stwissel, I created a public class Score {} with properties of studentID and value and then made my Scores Vector a collection of Score objects instead of String values. Then I bound my Edit Box to #{rpt.scores[rowIndex].value} Now I can get and set the value via the getters and setters of the Score class!

Here's some details about how I did it:

I have a repeat control var="rowData" indexVar="rowIndex" and a nested repeat control with a var="rpt" indexVar="rptIndex" and the data source is my Assignment object. Then for the value of my Edit Box I used the value property of my new Score class.

The short-short version would look something like this:

<xp:repeat... var="rowData" indexVar="rowIndex" ...>
    <xp:repeat... var="rpt" indexVar="rptIndex" value="#{gbPage.assignments}" ...>
        <xp:inputText id="Input12" value="#{rpt.scores[rowIndex].value}" ...>
        </xp:inputText>
    </xp:repeat>
</xp:repeat>

I had to change a couple methods in my Assignment class:

package com.logickey.gradebook;

import java.io.Serializable;
import com.logickey.gradebook.Score;
import java.util.Arrays;
import java.util.Vector;


public class Assignment implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -1500382996574385949L;
    private String Name;
    private String Teacher;
    private String AssignNum;
    private String AssignDate;
    private **Vector<Score> Scores;**
    private String Category;
    private String TotalPoints;
    private Arrays Standards;

    public Assignment() {
        Name = "";
        Teacher = "";
        AssignNum = "";
        AssignDate = "";
        **Scores = new Vector<Score>();**
    }
    public Assignment(String name, String teacher, String assignNum, String assignDate, **Vector<Score> scores**){
        Name = name;
        Teacher = teacher;
        AssignNum = assignNum;
        AssignDate = assignDate;
        Scores = scores;
    }

    /*
     * 
     *  some code removed from here....
     * */

    public void setScores(Vector<Score> scores) {
        Scores = scores;
    }
    public Vector<Score> getScores() {
        return Scores;
    }
    /**
     * custom methods
     */

//added two parameters to the addScore method   
public void addScore(String studentID, String value) {
    if (Scores==null) {
        Scores = new Vector<Score>();
    }
    **Scores.add(new Score(studentID, value));**
}
}

Here's the Score class I added:

package com.logickey.gradebook;

import java.io.Serializable;

public class Score implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 3888467323290005584L;
    private String StudentID;
    private String Value;

    public Score() {
        StudentID = "";
        Value = ""; 
    }

    public Score(String studentID, String value) {
        StudentID = studentID;
        Value = value;
    }

    public String getStudentID() {
        return StudentID;
    }

    public void setStudentID(String studentID) {
        StudentID = studentID;
    }

    public String getValue() {
        return Value;
    }

    public void setValue(String value) {
        Value = value;
    }

}