3

每次用户从 h:selectManyCheckbox 更改所选值时,我都会尝试刷新布尔变量的状态。我有从数据库动态加载的调查。题型有单选和单选两种。当问题的答案之一具有布尔值(需要附加答案)时,选择此选项后应出现 inputText。我已经成功地为单选题和 SelectOneRadio 创建了这个解决方案,它可以按我的意愿工作。

不幸的是,它不适用于 SelectManyCheckbox。当我将<f:ajax>标签放入<f:selectItems ... >其中时会抛出错误:

错误: javax.servlet.ServletException: <f:ajax> contains an unknown id 'podgladAnkietyForm:repeatLoop:2:selectBoxes' - cannot locate it in the context of the component selectBoxes

我认为这是某种问题<ui:repeat>。当我尝试使用 selectManyBox 但在 ui:repeat 循环之外执行类似的代码时,它可以工作。此外,几乎相同的代码可以使用<h:selectOneRadio>!我尝试了要刷新的绑定组件,更改绝对路径 render=":podgladAnkietyForm:repeatLoop:#{loop.index}:selectBoxes",添加额外的 NamingContainer<f:subView>但它也没有帮助。当我删除<f:ajax>标签它工作。结果代码为:

结果html代码

        <input name="podgladAnkietyForm:repeatLoop:3:selectBoxes" id="podgladAnkietyForm:repeatLoop:3:selectBoxes:0" value="121" type="checkbox"><label for="podgladAnkietyForm:repeatLoop:3:selectBoxes:0" class=""> Answer content</label>

我不知道我可能做错了什么。有没有人有类似的问题?我无法删除 ui:repeat 循环,因为它会从数据库加载所有问题。一些解决方法?

JSF 页面

        <h:form id="podgladAnkietyForm" prependId="true" >
            <ui:repeat value="#{fillSurvey.surveyQuestions}" var="surveyQuestion" varStatus="loop" id="repeatLoop">
                <br/>
                <h:outputText value=" Pytanie #{loop.index  + 1}" styleClass="naglowek" style="font-size: medium;" />
                <h:outputText value=" * odpowiedź obowiązkowa " rendered="#{surveyQuestion.question.isMandatory}" style="color: red; font: small bold"/><br/>
                <h:outputText value="#{surveyQuestion.question.content}" /> <br/>

                <h:panelGroup rendered="#{surveyQuestion.question.isMultiplechoice}" id="multiplechoiceGroup"  > 
                    <h:selectManyCheckbox value="#{surveyQuestion.answers}" id="selectBoxes" layout="pageDirection" required="#{surveyQuestion.question.isMandatory}">     
                        <f:selectItems value="#{surveyQuestion.optionsList}" var="entry" itemLabel="#{entry.content}" itemValue="#{entry.optionId}" id="multiplechoiceOption"   />
                        <f:ajax  />
                        <f:ajax event="click" execute="@this" listener="#{surveyQuestion.checkSelected}" render="selectBoxes" />
                    </h:selectManyCheckbox>     
                    <h:inputText label="Pytanie #{loop.index  + 1}" value="#{surveyQuestion.descriptionValue}" style="display: block;" rendered="#{surveyQuestion.haveDesc}" required="#{surveyQuestion.needDesc}" id="textField" />              
                </h:panelGroup>

                <!-- This part of code works perfectly - SelectOneRadio - After selecting option that require additional description TextInput is showed -->
                <h:panelGroup rendered="#{!surveyQuestion.question.isMultiplechoice}" id="selectOneGroup" >    
                    <h:selectOneRadio value="#{surveyQuestion.selectedItem}" id="selectOneID" layout="pageDirection">
                                <f:selectItems value="#{surveyQuestion.optionsList}" var="entry" itemLabel="#{entry.content}" itemValue="#{entry.optionId}" id="OneSelectOption"   />
                                <f:ajax event="click" execute="@this" render="selectOneGroup" listener="#{surveyQuestion.counter}"  />
                    </h:selectOneRadio>   
                    <h:inputText label="Pytanie #{loop.index  + 1}" value="#{surveyQuestion.descriptionValue}" style="display: block;" rendered="#{surveyQuestion.haveDesc}" required="#{surveyQuestion.needDesc}" size="40"/>              
                    <h:outputText value="#{surveyQuestion.testField}" />
                </h:panelGroup>

            </ui:repeat> 

            <br/><br/>
            <h:commandButton action="#{fillSurvey.validateSurvey()}" value="Validate" /> <br/>
        </h:form> 

支持豆

public class SurveyQuestion implements Serializable {

Question question;
List<Options> optionsList;
List<String> answers;

boolean haveDesc;
boolean needDesc;
int selectedItem;
String descriptionValue;

int testField;

///Getters and setters

public void counter(AjaxBehaviorEvent e){
        for(Options opt: optionsList){
          if ( selectedItem == opt.getOptionId()) {  
            if(opt.getHaveDesc() || opt.getDescReqr()){        
                this.haveDesc = true;
                    if (opt.getDescReqr()) {
                        this.needDesc = true;
                    } else {
                        this.needDesc = false;
                    }
            } else {this.haveDesc = false; this.needDesc=false; }
          }
        } 
        testField++;
}
//its empty but it will look like the one above
public void checkSelected(AjaxBehaviorEvent e){
    testField++;
}
}
4

1 回答 1

2

这个话题似乎有点过时了,但无论如何......

您的f:ajax元素被包裹在一个ui:repeat元素中。我的代码中遇到了同样的情况,对我有帮助的是这张票的答案:f:ajax inside ui:repeat, unknown id for the component。简而言之,我必须使用被渲染组件的绝对 ID,以冒号 ( :) 开头。希望这对你也有帮助。

于 2014-01-02T13:18:14.330 回答