代码之家  ›  专栏  ›  技术社区  ›  Pops Atula

如果设置为“必需”,是否可以动态更改窗口窗体组件?

  •  2
  • Pops Atula  · 技术社区  · 14 年前

    编辑:
    这个问题最初是关于复选框的,但是我在下拉列表中得到了相同的行为。代码:

    productInput = new DropDownChoice<String>("productInput",
                                              new PropertyModel<String>(this, "productSelection"),
                                              products);
    
    productInput.add(new AjaxFormComponentUpdatingBehavior("onchange") {
        @Override
        protected void onUpdate(AjaxRequestTarget target)
        {
            if (productSelection == null) // Breakpoint is set on this line
            {
                updateDropdownsAfterFieldDisabled(1, target);
            }
            else
            {
                updateDropdownsAfterFieldEnabled(1, target);
            }
        }
    });
    

    如果 productInput 没有设置为必需的,每次从列表中选择某个值以选择空白行选项时,断点会被击中。如果设置为必需,则不会命中断点。


    如果Wicket表单组件通过 setRequired(true); ?下面是一个简单的例子,如果是人为的,来说明我的意思:

    在加油站洗车,通常是三到四级服务。每一个都包括由较低层次提供的所有东西,并在一层以上的蜡或额外的漂洗或其他东西上粘上。洗漱室通常有四个按钮,旁边有灯。( example )当用户按下其中一个按钮时,相应的灯和所有较便宜级别的灯一起亮起;较好级别的灯熄灭。该行为由以下代码建模:

    boolean economy, standard, deluxe, ultimate = false;
    
    CheckBox economyBox = new CheckBox("economyBox",
                                       new PropertyModel<Boolean>(this, "economy"));
    // Similar declarations for standardBox, deluxeBox and ultimatebox
    
    OnChangeAjaxBehavior standardChangeListener = new OnChangeAjaxBehavior() {
        protected void onUpdate(AjaxRequestTarget target)
        {
            // If "standard" is activated, also turn on the "economy" light
            if (standard)
            {
                economy = true;
                target.addComponent(economyBox);
            }
            // If "standard" is deactivated, deactivate "deluxe" and "ultimate"
            else if (!standard)
            {
                deluxe = false;
                ultimate = false;
                target.addComponent(deluxeBox);
                target.addComponent(ultimateBox);
            }
        }
    };
    standardBox.add(standardChangeListener);
    // Similar listeners declared for the other three checkboxes
    

    我已经测试了这段代码——好吧,这是一个简化版本的真正代码——它按预期工作。但加入 economyBox.setRequired(true); 盒子停止动态更新。是否有方法在不破坏“链接复选框”行为的情况下添加验证?

    1 回复  |  直到 14 年前
        1
  •  0
  •   Don Roby    14 年前

    我看不出这里有什么问题,除了你

            target.addComponent(standardBox);
    

    我想你想去的地方

            target.addComponent(economyBox);
    

    验证应与 OnChangeAjaxBehavior 很好。我很确定我已经在某些表单中使用了这两种格式,但我认为没有使用复选框。

    我不确定用 economyBox.setRequired(true); 不过在这里是有道理的。您提供的是值,在您的模型中,所有复选框的默认值都为false,所以在这个上下文中没有真正的方法可以忽略任何内容。

    更明智的验证可能是 某物 必须进行检查,但这需要为整个表单或 FormComponent 合并所有复选框。

    编辑: 基于你的编辑关于看到同一个问题 DropDownChoice 我做了一个实验。

    有一个例子 AjaxFormComponentUpdatingBehavior 具有 下拉选择 组件 here . 我下载了这个的代码,补充说

        models.setRequired(true);
        makes.setRequired(true);
        add(new FeedbackPanel("messages"));
    

    当然,还为反馈面板添加了标记。

    它验证了所需字段,并根据make下拉列表的值继续更新模型下拉列表。

    由于这对我来说很好,我建议您尝试同样的方法,看看它是否有效,然后与您的代码进行比较。