首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在optionGroups上设置defaultValue和/或选择开花的玉兰CMS中的字段?

如何在optionGroups上设置defaultValue和/或选择开花的玉兰CMS中的字段?
EN

Stack Overflow用户
提问于 2017-04-25 17:35:32
回答 2查看 566关注 0票数 0

设置defaultValue既不适用于select-fields,也不适用于木兰花不育系optionGroup-fields。

代码语言:javascript
复制
@TabFactory("pages.properties.label")
public void pageProperties(UiConfig cfg, TabBuilder tab) {

    List<String> targetGroup = new ArrayList<>();
    targetGroup.add("Group A");
    targetGroup.add("Group B");

    List<String> menuTeaser = new ArrayList<>();
    menuTeaser.add("Teaser A");
    menuTeaser.add("Teaser B");

    tab.fields(
        cfg.fields.text("pageTitle").label("pages.properties.pageTitle.label").i18n()
            .description("pages.properties.pageTitle.description").i18n()
            .defaultValue("any value").i18n()   // works!
            .requiredErrorMessage("pages.properties.pageTitle.requiredErrorMessage").i18n()
            .required(),

        cfg.fields.select("targetGroup").label("pages.properties.targetGroup.label").i18n()
            .description("pages.properties.targetGroup.description").i18n()
            .options(targetGroup)
            .defaultValue(targetGroup.get(0)).i18n(),      // doesn't work!

        cfg.fields.optionGroup("menuTeaser").label("pages.properties.menuTeaser.label").i18n()
            .description("pages.properties.menuTeaser.description").i18n()
            .options(menuTeaser).i18n()
            .defaultValue(menuTeaser.get(0)).i18n()     // doesn't work!
            .required(),
    );
}

有趣的是,它适用于text-fields。

对于其他字段类型,如何实现这一点?

EN

回答 2

Stack Overflow用户

发布于 2017-04-27 15:28:15

您需要做的就是在select中默认选择的选项上设置selected=true。这是正常的Magnolia代码。对于Blossom和TabBuilder,不幸的是,它只允许将选项设置为字符串,而不是公开/允许传入整个SelectOption。您需要扩展TabBuilder来创建自己的重载addSelect方法。现有的一个如下所示:

代码语言:javascript
复制
    public DialogSelect addSelect(String name, String label, String description, Collection<String> options) {
    try {
        DialogSelect select = DialogFactory.getDialogSelectInstance(
                context.getRequest(),
                context.getResponse(),
                context.getWebsiteNode(),
                null);
        select.setName(name);
        select.setLabel(label);
        select.setDescription(description);
        for (String option : options) {
            select.addOption(new SelectOption(option, option));
        }
        tab.addSub(select);
        return select;
    } catch (RepositoryException e) {
        throw new RuntimeRepositoryException(e);
    }
}

在您的代码中,您可能希望传入SelectOption项的Collection。要将项标记为选中,只需调用mySelectOption.setSelected(true)

或者,您可能希望将您的重载代码作为Blossom本身的补丁。

HTH,

1月

票数 0
EN

Stack Overflow用户

发布于 2017-05-04 20:47:29

我没有让你的建议以一种简单的方式运行。非常感谢,尽管如此。

最终我这样解决了这个问题:

代码语言:javascript
复制
@TabFactory("pages.properties.label")
public void pageProperties(UiConfig cfg, TabBuilder tab) {

    List<String> targetGroup = new ArrayList<>();
    targetGroup.add(new OptionBuilder().value("Group A").label("Group A").selected());
    targetGroup.add(new OptionBuilder().value("Group B").label("Group B").selected());

    List<String> menuTeaser = new ArrayList<>();
    menuTeaser.add(new OptionBuilder().value("Group A").label("Teaser A").selected());
    menuTeaser.add(new OptionBuilder().value("Group A").label("Teaser B").selected());

    tab.fields(
        cfg.fields.text("pageTitle").label("pages.properties.pageTitle.label").i18n()
            .description("pages.properties.pageTitle.description").i18n()
            .defaultValue("any value").i18n()   // works!
            .requiredErrorMessage("pages.properties.pageTitle.requiredErrorMessage").i18n()
            .required(),

        cfg.fields.select("targetGroup").label("pages.properties.targetGroup.label").i18n()
            .description("pages.properties.targetGroup.description").i18n()
            .options(targetGroup.toArray(new OptionBuilder[targetGroup.size()])).i18n(),

        cfg.fields.optionGroup("menuTeaser").label("pages.properties.menuTeaser.label").i18n()
            .description("pages.properties.menuTeaser.description").i18n()
            .options(menuTeaser.toArray(new OptionBuilder[menuTeaser.size()])).i18n()
            .required(),
    );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43606885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档