代码之家  ›  专栏  ›  技术社区  ›  Malin

xp:复选框值在读取和编辑模式下不同

  •  0
  • Malin  · 技术社区  · 6 年前

    <xp:checkBox id="cbOther" value="#{customerBean.customer.other}" disabled="#{!customerBean.customer.editable}" checkedValue="1" uncheckedValue="0">
    

    在我的后端代码中,我尝试根据另一个字段的值设置复选框的值:

     if(doc.hasItem("fldOtherVal")){
                        if(doc.getItemValueString("fldOtherVal").equals("")){
                            System.out.println("no relations");
                            customer.setOther("0");
                        }else{
                            System.out.println("relations");
                            customer.setOther("1");
                            customer.setOtherVal(doc.getItemValueString("fldOtherVal"));    
                        }                           
                    }else{
                        customer.setOther("0");
                    }
    

    当我在xpage中以read模式打开customer对象时,这很好。但是,当我将customer对象设置为edit模式时,复选框中的值设置为默认值0。

    2 回复  |  直到 6 年前
        1
  •  0
  •   Frantisek Kossuth    6 年前

    我发现与您的文档/bean不一致。Chceckbox绑定到“other”值,但有时引用“otherVal”字段/属性。

    如果保存的文档在“fldOtherVal”字段中包含值“0”,则代码将落入

                System.out.println("relations");
                customer.setOther("1");
                customer.setOtherVal(doc.getItemValueString("fldOtherVal"));   
    

    我的评论是:

    if(doc.getItemValueString("fldOtherVal").equals("")){
    

    不检查“0”值。

    编辑:

    返回复选框的别名值(“0”和“1”)。

     // no hasItem check needed
     var other = doc.getItemValue("fldOtherVal"); // must not be multivalue
     if ("".equals(other) || "0".equals(other)) {
          System.out.println("no relations");
          customer.setOther("0");
          customer.setOtherVal("0"); // update "otherVal" also
     } else {
          System.out.println("relations");
          customer.setOther("1");
          customer.setOtherVal("1"); // do not copy value, just set to "1"
     }
    
        2
  •  0
  •   Stanislaw Guzik    6 年前

    尝试删除 disabled 你的财产 <xp:checkbox> 使用 readonly 残疾人 属性完整,只是添加 具有相同表达式的属性也应有效。 如果有帮助的话,我会解释这个行为作为这个答案的更新。