4

我在一个向导选项卡中使用 PrimeFaces DataTable 和 Radiobuttons。是否可以以某种方式设置 Radiobuttons 像required

用户在使用单选按钮在 DataTable 中选择一个选项之前,不应转到下一个向导选项卡。

或者你有任何想法如何解决这个问题?感谢您的回复!

JSP 页面

<p:tab id="test" title="Test">
    <p:panel header="Term page">
        <p:dataTable id="collection" value="#{register.dataList}" var="dl" rowKey="#{dl.c_id}" selection="#{register.selectedTerm}"">
            <p:column selectionMode="single" style="width:2%" />
            <p:column>
                #{dl.c_id}
            </p:column>
        </p:dataTable>
    </p:panel>
</p:tab>
4

1 回答 1

1

您可以检查单击下一个/上一个按钮时触发flowListener的标记中的数据选择并有条件地添加:<p:wizard>FacesMessage

public String onFlowProcess(FlowEvent event) {
    String current = event.getOldStep();
    String next = event.getNewStep();
    boolean proceed = true;
    if(current.equals("first") && next.equals("second") && (selectedData == null)) {
        //proceed only when data was selected and user is moving to the next step
        FacesMessage facesMessage = new FacesMessage("You need to make a selection in a datatable to proceed!");
        FacesContext.getCurrentInstance().addMessage("form:selection", facesMessage);
        proceed = false;
    }
    return proceed ? next : current;
}

下面提供了完整的示例。

风景:

<h:form id="form">
    <p:wizard widgetVar="wiz" flowListener="#{q16439053Bean.onFlowProcess}">
        <p:tab id="first" title="First">
            <p:message for="selection"/>
            <p:panel id="selection" header="Term page">
                <p:dataTable id="collection" value="#{q16439053Bean.list}" var="data" rowKey="#{data.name}" selection="#{q16439053Bean.selectedData}">
                    <p:column selectionMode="single" style="width:2%" />
                    <p:column>
                        #{data.name}
                    </p:column>
                </p:dataTable>
            </p:panel>
        </p:tab>
        <p:tab id="second" title="Second">
            Done!
        </p:tab>
    </p:wizard>
</h:form>

豆子:

@ManagedBean
@ViewScoped
public class Q16439053Bean implements Serializable {

    private List<Data> list;
    private Data selectedData;

    public List<Data> getList() {
        return list;
    }

    public void setList(List<Data> list) {
        this.list = list;
    }

    public Data getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Data selectedData) {
        this.selectedData = selectedData;
    }

    public class Data {

        private String name;
        private String value;

        public Data() {
        }

        public Data(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

    }

    public Q16439053Bean() {
        list = new ArrayList<Data>();
        Data d;
        d = new Data("name", "value");
        list.add(d);
        d = new Data("name1", "value1");
        list.add(d);
        d = new Data("name2", "value2");
        list.add(d);
        d = new Data("name3", "value3");
        list.add(d);
    }

    public String onFlowProcess(FlowEvent event) {
        String current = event.getOldStep();
        String next = event.getNewStep();
        boolean proceed = true;
        if(current.equals("first") && next.equals("second") && (selectedData == null)) {
            FacesMessage facesMessage = new FacesMessage("You need to make a selection in a datatable to proceed!");
            FacesContext.getCurrentInstance().addMessage("form:selection", facesMessage);
            proceed = false;
        }
        return proceed ? next : current;
    }

}
于 2013-05-08T12:10:37.140 回答