Primefaces的DataTable编辑不显示新值

时间:2016-05-17 07:10:10

标签: jsf jsf-2 primefaces datatable

我已经苦苦挣扎了好几天,Primefaces的DataTable在编辑单元格后没有显示新值。

我已经找到了这个问题的答案,但没有找到任何与我有关的内容 - 大部分时间它似乎是关于使用字符串而不是对象,但在我的情况下我使用的是对象(例如{ {3}})。

我正在开发的应用程序显示了数据库中课程实体的内容,其中显示了名称,年份,期限和活动(如果它是当前的或不是)。名称,年份和期限是主键。在数据表中,我可以选择和编辑单元格,但是当我按下返回(或更改单元格)时会出现问题。对象event.getNewValue();仍然返回旧值。

<p:dataTable var="oneCourse" value="#{courseManager.courses}" paginator="true" rows="5" selectionMode="single" selection="#{courseManager.courseSelection}" 
        rowKey="#{oneCourse.id}" editable="true" editMode="cell" paginatorPosition="bottom" class="dataWidth" widgetVar="cellCourse">

            <p:ajax event="cellEdit" listener="#{courseManager.onCellEdit}" update=":form:msgs" />

            <p:column headerText="Kursnamn">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{oneCourse.id.courseName}" />
                    </f:facet>
                    <f:facet name="input">
                        <h:inputText value="#{oneCourse.id.courseName}"/>
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column headerText="År">
                <h:outputText value="#{oneCourse.id.year}" />
            </p:column>

            <p:column headerText="Termin">
                <h:outputText value="#{oneCourse.id.term}" />
            </p:column>

            <p:column headerText="Aktiv">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{oneCourse.active}" />
                    </f:facet>
                    <f:facet name="input">
                        <h:selectOneMenu value="#{oneCourse.active}">
                            <f:selectItem itemValue="0" itemLabel="0" />
                            <f:selectItem itemValue="1" itemLabel="1" />
                        </h:selectOneMenu>
                    </f:facet>
                </p:cellEditor>
            </p:column>     
</p:dataTable>

对不起瑞典语......

托管bean有点混乱,包含用于处理应用程序的前端和配置部分的代码。我把它全部粘贴,因为我怀疑在某些我没看过的地方,我可能已经做了一些蠢事......

@Named
@ViewScoped
public class CourseManager implements Serializable {
private static final long serialVersionUID = 240902154191546462L;

@EJB
private CourseBean theCourses;

private Course courseSelection;

public CourseManager() {}

private List<String> durationList = new ArrayList<String>(Arrays.asList(
        "5 min",
        "10 min",
        "15 min",
        "20 min",
        "30 min",
        "45 min",
        "60 min",
        "90 min",
        "120 min"
        ));

private List<String> activityList = new ArrayList<String>(Arrays.asList(
        "Kursansvar",
        "Planering",
        "Handledning",
        "Föreläsning",
        "Planera föreläsning",
        "Hålla labb",
        "Labbkonstruktion",
        "Labbrättning",
        "Tentakonstruktion",
        "Tentarättning"
        ));

private String activity;

private String duration;

private String selectedCourse;

private String currCourse;
private String currTerm;
private int currYear;
private int currDuration;

public List<String> getCourseList() {
    List<Course> listOfCourses = theCourses.getListOfCourses();
    List<String> courseList = new ArrayList<String>();

    for(int i = 0; i < listOfCourses.size(); i++)
    {
        if(listOfCourses.get(i).getActive()!=0)
        {
            String theCourse = listOfCourses.get(i).getId().getCourseName() + " " + listOfCourses.get(i).getId().getTerm() + " " + listOfCourses.get(i).getId().getYear();
            courseList.add(theCourse);
        }
    }

    return courseList;
}

List<Course> courses = null;

/*public void setCourses(List<Course> courses) {
    this.courses = courses;
}*/


public List<Course> getCourses() {
    courses = theCourses.getListOfCourses();
    return courses;
}

public String getSelectedCourse() {
    return selectedCourse;
}

public void setSelectedCourse(String selectedCourse) {
    this.selectedCourse = selectedCourse;
}

public String getDuration() {
    return duration;
}

public void setDuration(String duration) {
    this.duration = duration;
}

public List<String> getDurationList() {
    return durationList;
}

public String getActivity() {
    return activity;
}

public void setActivity(String activity) {
    this.activity = activity;
}

public List<String> getActivityList() {
    return activityList;
}

public void result() throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();

    if(selectedCourse!=null || duration!=null || activity!=null ) {
        String[] parts = selectedCourse.split(" ");
        currCourse = parts[0];
        currTerm = parts[1];
        currYear = Integer.parseInt(parts[2]);
        String[] durationParts = duration.split(" ");
        currDuration = Integer.parseInt(durationParts[0]);

        CourseActivity ca = new CourseActivity();

        ca.setName(currCourse);
        ca.setTerm(currTerm);
        ca.setYear(currYear);
        ca.setActivity(activity);
        ca.setDuration(currDuration);

        try {
            theCourses.addCourseActivity(ca);
            context.addMessage(null, new FacesMessage("Inlagt!",  duration + " minuter inlagt för " + activity + " på kursen " + parts[0] + " " + parts[1] + parts[2]) );
        } catch (Exception e) {
            context.addMessage(null, new FacesMessage("Ett fel har uppstått!",  "Det gick inte att lägga in i databasen.") );
        }           
    }
    else
        context.addMessage(null, new FacesMessage("Ett fel!",  "Inte allt var valt!") );
}

public Course getCourseSelection() {
    return courseSelection;
}

public void setCourseSelection(Course courseSelection) {
    this.courseSelection = courseSelection;
}

public void onCellEdit(CellEditEvent event) {
    Object oldValue = event.getOldValue();
    Object newValue = event.getNewValue();

    if(newValue != null) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}       
}

可见,我试图在Primefaces的Showcase中使用DataTable Edit教程,但我的结果不一样......我也尝试使用RowEdit,但效果相同 - 我的新值永远不会被承认。然而,它存在于某个地方,因为只要我在同一个会话中,当我再次选择相同的单元格时,新值将显示在单元格编辑器中。

任何帮助将不胜感激!我已经被困了好几天尝试了几种方法,但总是有相同的结果。如果我无法解决这个问题,在你的帮助下,我将尽力使用PrimeFaces ......

非常感谢提前!

0 个答案:

没有答案