更新primefaces中的Datalist值

时间:2013-09-22 19:27:09

标签: jsf primefaces

我正在使用primefaces datalist,它包含与之绑定的列表,datalist包含单选按钮我想更改/更新该绑定列表的值我试过这段代码但是它不能正常工作

<p:dataList var="question" value="#{formTableBean.question}"
                    type="definition" >
                    <h:outputText value="#{question.text}"
                        style="font-size:14px;font-weight:bold" />

                    <h:panelGrid columns="2" width="100%" style="margin-bottom:10px"
                        cellpadding="10">

                        <h:outputText value="Options: " />
                        <p:selectOneRadio id="options" value="#{question.answer}" >
                            <f:selectItem itemLabel="Strongly Agree" itemValue="1" />
                            <f:selectItem style="margin-right:20px" itemLabel="Agree"
                                itemValue="2" />
                            <f:selectItem style="margin-right:20px" itemLabel="Disagree"
                                itemValue="3" />
                            <f:selectItem itemLabel="Strongly Disagree" itemValue="4" />
                            <p:spacer width="100"></p:spacer>
                        </p:selectOneRadio>
                    </h:panelGrid>
                </p:dataList>

bean类包Bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;

import DAO.Dao;
import DTO.FormDTO;
import DTO.QuestionDTO;

@ManagedBean
@ViewScoped
public class FormTableBean {

    FormDTO selectedForm;
    List<QuestionDTO> question;

    public List<QuestionDTO> getQuestion() {
        return question;
    }

    public void setQuestion(List<QuestionDTO> question) {
        this.question = question;
    }

    public FormDTO getSelectedForm() {
        return selectedForm;
    }

    public void setSelectedForm(FormDTO selectedForm) {
        this.selectedForm = selectedForm;
    }

    boolean renderFormPanal = false;

    public boolean getRenderFormPanal() {
        return renderFormPanal;
    }

    public void setRenderFormPanal(boolean renderFormPanal) {
        this.renderFormPanal = renderFormPanal;
    }

    List<FormDTO> formList;

    public List<FormDTO> getFormList() {
        return formList;
    }

    public void setFormList(List<FormDTO> formList) {
        this.formList = formList;
    }

    Dao daoclass = new Dao();

    public FormTableBean() {

        User u = getCurrentUser();
        String email = u.getEmailAddress();
        int eid = daoclass.emailToEid(email);
        formList = new ArrayList<FormDTO>();
        formList = daoclass.getAllForm(eid);

    }

    protected User getCurrentUser() {
        User u = null;
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext externalContext = fc.getExternalContext();
        Long id = Long.parseLong(externalContext.getUserPrincipal().getName());
        try {
            u = UserLocalServiceUtil.getUserById(id);
        } catch (com.liferay.portal.kernel.exception.PortalException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (com.liferay.portal.kernel.exception.SystemException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return u;
    }

    public void showForm() {
        question = new ArrayList<QuestionDTO>();
        setRenderFormPanal(true);
        int formid = selectedForm.getFid();
        question = daoclass.getQuestion(formid);

    }

    public void saveForm() {
        for (int i = 0; i < question.size(); i++) {
            System.out.print("QID:"+question.get(i).getAnswer());
        }

    }

}

我想在列表中更新答案值。请帮助我

1 个答案:

答案 0 :(得分:3)

每次用户选择答案时,都会在p:selectOneRadio上使用Ajax提交答案。

<p:selectOneRadio id="options" value="#{question.answer}" >
   <p:ajax event="valueChange" update="any_component's_id_you_want_to_update" />
   ....
   ....
</p:selectOneRadio>