【问题标题】:AEM CQ5 - Multifield custom xtype inside a mutlifield custom xtypeAEM CQ5 - 多字段自定义 xtype 内的多字段自定义 xtype
【发布时间】:2015-03-11 05:36:23
【问题描述】:

我正在尝试在 AEM CQ5 中创建一个组件,该组件将生成一个测验模块。我的要求是创建一个对话框,允许我创建多个问题,每个问题都有多个答案。我的对话框 xml 如下 -

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    jcr:primaryType="cq:Dialog"
    xtype="dialog">
    <items jcr:primaryType="cq:Widget"
        xtype="tabpanel">
        <items jcr:primaryType="cq:WidgetCollection">
            <categories 
                jcr:primaryType="cq:Panel"
                title="Questions & Answers">
                <items jcr:primaryType="cq:WidgetCollection">
                    <questions-answers
                        jcr:primaryType="cq:Widget"
                        title="Questions & Answers"
                        name="./qasegment"
                        xtype="dialogfieldset">
                            <items jcr:primaryType="cq:WidgetCollection">
                                <link
                                    jcr:primaryType="cq:Widget"
                                    fieldDescription="Click on Add item to add questions. Once question is added, click on Edit Answers to add answers and the destination URLs"
                                    name="./questionsAnswers"
                                    typeHint="String"
                                    xtype="multifield">                         
                                    <fieldConfig 
                                        jcr:primaryType="nt:unstructured"
                                        xtype="widgets.configurableQandAfield"/>
                                </link>
                            </items>
                    </questions-answers>
                </items>       
            </categories>
        </items>
    </items>
</jcr:root>

从和 xtype:mutlifield,我指的是一个自定义的 extjs 小部件,如下所示。这将创建(问题 + 问题配置 + 添加答案多字段)多字段。

NirmalWeb.ConfigurableQandAField = CQ.Ext.extend(CQ.form.CompositeField, {

     /**
     * @private
     * @type CQ.Ext.form.HiddenField
     */
    hiddenField: null,

    /**
     * @private
     * @type CQ.Ext.form.TextField
     */
    questionField: null,

    /**
     * @private
     * @type CQ.Ext.form.TextField
     */
    linkField: null,

    textField:null,

    answerOptions:null,

    answerType:null,

    widgetConfig: null,


    constructor: function(config) {
        config = config || { };
        var defaults = {
            "border": true,
            "layout": "form",
            "labelSeparator": ":",
            "padding": "10px"
        };
        this.widgetConfig = config;
        config = CQ.Util.applyDefaults(config, defaults);
        EeWeb.ConfigurableQandAField.superclass.constructor.call(this, config);
    },

    // overriding CQ.Ext.Component#initComponent
    initComponent: function() {
        EeWeb.ConfigurableQandAField.superclass.initComponent.call(this);

        // Add a hidden field to hold our result to store.
        this.hiddenField = new CQ.Ext.form.Hidden({
            name: this.name
        });
        this.add(this.hiddenField);


        // Add the link text field and label.
        this.questionField = new CQ.Ext.form.TextField({
            fieldLabel: 'Question',
            allowBlank: false,
            emptyText: "Enter the question",
            width: 500,
            listeners: {
                change: {
                    fn:this.updateQuestionStore
                },       
                dialogclose: {
                    scope: this,
                    fn: this.updateHidden
                }
            }
        });
        this.add(this.questionField);

        var fieldsetConfig = CQ.Ext.apply({}, {
           xtype: 'fieldset',
           title: 'Edit Answer',
           // title or checkboxToggle creates fieldset header
           columnWidth: 0.5,
           checkboxToggle: true,
           collapsed: true,
           items :[
               {    
                xtype: "fieldset",
                title: 'Answer configurations',
                autoHeight:true,
                items: [
                        new CQ.Ext.form.RadioGroup({
                                vertical: false,
                                id:"answerType",
                                items: [
                                    {boxLabel: 'Text Answer', name: 'answerType', inputValue: 1, checked:true},
                                    {boxLabel: 'Image Answer', name: 'answerType', inputValue: 2}
                                ]    

                        }),
                        new CQ.Ext.form.RadioGroup({
                                vertical: false,
                                id:"answerOptions",
                                items: [
                                    {boxLabel: 'Single Answer', name: 'answerOptions', inputValue: 1, checked:true},
                                    {boxLabel: 'Multiple Answer', name: 'answerOptions', inputValue: 2}
                                ]    

                        }),
                       ]
                },
                {
                xtype: "multifield",
                    fieldConfig : {
                        items: [
                            {
                                xtype: "widgets.configurablemultiAnswerfield",
                                hideLabel: true,
                            }
                        ]
                    }
                },
           ]
        });
        this.textField = new CQ.Ext.form.FieldSet(fieldsetConfig);
        this.add(this.textField);

    },

    // overriding CQ.form.CompositeField#processInit
    processInit: function (path, record) { 
        this.linkTextField.processInit(path, record);
        this.linkField.processInit(path, record);
    },

    // overriding CQ.form.CompositeField#setValue
    setValue: function(value) {
        var link = JSON.parse(value);
        this.linkTextField.setValue(link.text);
        this.linkField.show();
        this.hiddenField.setValue(value);
    },

    // overriding CQ.form.CompositeField#getValue
    getValue: function() {
        return this.getRawValue();
    },

    // overriding CQ.form.CompositeField#getRawValue
    getRawValue: function() {
        return JSON.stringify("test");
    },

    // private
    updateHidden: function() {
        this.hiddenField.setValue(this.getValue());
    },

    editAnswer: function(value) {
                alert("radio group change");
     }

});

// Register our new xtype.
CQ.Ext.reg('widgets.configurableQandAfield', NirmalWeb.ConfigurableQandAField);

在自定义小部件中,我正在创建另一个用于添加答案文本和工具提示的多字段,这是另一个自定义小部件。不幸的是,多字段添加项只是添加了一个文本字段,而不是自定义小部件“configurablemultiAnswerfield”中的所有字段。

请帮帮我。让我知道是否需要更多信息。提前致谢。

【问题讨论】:

  • 只是一个建议,你为什么要用多个多字段来混乱你的对话框。相反,您可以制作一个问题组件,其中包含一个问题和一个用于答案的多字段。在页面上多次拖放组件并使用它(类似于包含多个输入字段的默认表单组件)。
  • 感谢 rakhi 的建议。此测验组件的最终目的是提高帮助页面的实用性。我们计划通过提问向客户展示相关内容,而不是向所有客户展示全部内容。例如,airtel 数据计划的帮助页面 - 我们会问问题 1 - 你是什么类型的用户 a1。预付 a2.后付。在选择预付费时,它可以导致下一个问题或文本内容,对于后付费也是如此。绝对欢迎任何建议来实现这个逻辑。
  • rakhi 的方法是我要走的路,因此 +1。关于您的进一步需求,您可以创建一个更像问题容器的组件,您可以在其中实现接下来显示的逻辑,具体取决于上一个问题的结果。

标签: extjs aem


【解决方案1】:

感谢大家的回答和建议。我能够用下面的代码解决这个问题。它有点难看,但它可以完成工作。

  1. 调用第二个 xtype 的解决方案很简单。我叫错了。它应该只是 fieldConfig : {xtype: "widgets.configurableAnswer"} 只是为了填写以备将来参考,我遇到了另一个问题 - 从第二个 xtype 获取数据并将其作为 JSON 发送回来。我修改了第二个 xtype updateHidden 方法,如下所示

     updateHidden: function() {
        this.hiddenField.setValue(this.getValue());
       this.findParentByType('widgets.configurableQandAfield').callUpdate();
    

    }

父xtype中调用update方法如下:

    callUpdate: function() {
    this.hiddenField.setValue(this.getValue());
},

为了从第一个 xtype 中保存第二个 xtype 的数据,我在 getRaw 值方法中使用了以下代码

    getRawValue: function() {
    var link = {
        "questionText": this.questionText.getValue(),
        "answerStack" : this.answerSet.findByType('multifield')[0].getValue(),
        };
    return JSON.stringify(link);
},

附上第一个 xtype JS 以供进一步参考。

Nirmalweb.ConfigurableQandAField = CQ.Ext.extend(CQ.form.CompositeField, {

 /**
 * @private
 * @type CQ.Ext.form.HiddenField
 */
hiddenField: null,

/**
 * @private
 * @type CQ.Ext.form.TextField
 */
questionText: null,

/**
 * @private
 * @type CQ.Ext.form.FieldSet
 */
answerSet:null,

constructor: function(config) {
    config = config || { };
    var defaults = {
        "border": true,
        "layout": "form",
        "labelSeparator": ":",
        "padding": "10px"
    };
    config = CQ.Util.applyDefaults(config, defaults);
    Nirmalweb.ConfigurableQandAField.superclass.constructor.call(this, config);
},

// overriding CQ.Ext.Component#initComponent
initComponent: function() {
    Nirmalweb.ConfigurableQandAField.superclass.initComponent.call(this);

    // Add a hidden field to hold our result to store.
    this.hiddenField = new CQ.Ext.form.Hidden({
        name: this.name
    });
    this.add(this.hiddenField);

    // Add the link text field and label.
    this.questionText = new CQ.Ext.form.TextField({
        fieldLabel: 'Question',
        cls:"ee-configurableqandafield-text",
        allowBlank: false,
        emptyText: "Enter the question",
        listeners: {
            change: {
                scope:this,
                fn:this.updateHidden,
                fn:this.questionBank
            },       
            dialogclose: {
                scope: this,
                fn: this.updateHidden
            }
        },
        width: 500,
    });
    this.add(this.questionText);

    var fieldsetConfig = CQ.Ext.apply({}, {
        xtype: 'fieldset',
        title: 'Add Answers',
       // title or checkboxToggle creates fieldset header
        columnWidth: 0.5,
        checkboxToggle: true,
        collapsed: true,
        items :[
            {   
            xtype: "fieldset",
            title: 'Answer configurations',
            autoHeight:true,
            items: [
                    {
                        xtype: "selection",
                        type:"select",
                        fieldLabel:'Answer Type',
                        listeners: {
                            change: {
                                scope:this,
                                fn:this.updateHidden,
                            },       
                            dialogclose: {
                                scope: this,
                                fn: this.updateHidden,
                            },
                            selectionchanged :{
                                scope:this,
                                fn:this.updateHiddenAnsType
                            }
                        },
                        options:[
                            {   value:"text",text:"Text Answer"},
                            {   value:"image",text:"Image Answer"}
                        ]
                    },
                    {
                        xtype: "selection",
                        type:"select",
                        fieldLabel:'Answer Options',
                        listeners: {
                            change: {
                                scope:this,
                                fn:this.updateHidden
                            },       
                            dialogclose: {
                                scope: this,
                                fn: this.updateHidden
                            }
                        },
                        options:[
                            {   value:"single",text:"Single Answer"},
                            {   value:"multi",text:"Multiple Answer"}
                        ]
                    },
                   ]
            },
            {
                xtype: "multifield",
                addItemLabel:"Add an answer",
                listeners: {
                    change: {
                        scope:this,
                        fn:this.updateHidden
                    },       
                    dialogclose: {
                        scope: this,
                        fn: this.updateHidden
                    }
                },
                fieldConfig : {
                    xtype: "widgets.configurableAnswer",
                }
            },
       ]
    });
    this.answerSet = new CQ.Ext.form.FieldSet(fieldsetConfig);
    this.add(this.answerSet);


},

// overriding CQ.form.CompositeField#processInit
processInit: function (path, record) { 
    this.questionText.processInit(path, record);
    this.answerSet.processInit(path, record);
},

// overriding CQ.form.CompositeField#setValue
setValue: function(value) {
    var link = JSON.parse(value);
    this.questionText.setValue(link.questionText);
    this.answerSet.setValue(link.answerStack);
    this.hiddenField.setValue(value);
},

// overriding CQ.form.CompositeField#getValue
getValue: function() {
    return this.getRawValue();
},

// overriding CQ.form.CompositeField#getRawValue
getRawValue: function() {
    var link = {
        "questionText": this.questionText.getValue(),
        "answerStack" : this.answerSet.findByType('multifield')[0].getValue(),
        };
    return JSON.stringify(link);
},

// private
updateHidden: function() {
    this.hiddenField.setValue(this.getValue());
},

callUpdate: function() {
    this.hiddenField.setValue(this.getValue());
},

});
// Register our new xtype.
CQ.Ext.reg('widgets.configurableQandAfield', Nirmalweb.ConfigurableQandAField);

生成的 JSON 如下所示:

{
"questionText": "What type of user are you?",
"answerStack": [
    "{\"answerText\":\"a1\",\"answerStyle\":\"questionStyle\",\"answerToolTip\":\"t1\",\"destinationType\":\"text\",\"destinationTextCTA\":\"Fill in the Unlock form\",\"destinationTextCTAURL\":\"https://google.com\"}",
    "{\"answerText\":\"a2\",\"answerStyle\":\"questionStyle\",\"answerToolTip\":\"t2\",\"destinationType\":\"text\",\"destinationTextCTA\":\"Fill in the Unlock form 2\",\"destinationTextCTAURL\":\"https://facebook.com\"}"
]

}

【讨论】:

    【解决方案2】:

    我确实同意 cmets 的说法,最好有一个容器组件而不是使对话框复杂化,但是如果您仍在寻找一种在多字段中提取多字段的方法,那么以下方法应该会有所帮助

    您可以使用三种 xtype 创建效果:

    1) Multicomposite - xtype 将每个多字段条目存储为子节点,并将字段作为属性。 [由 Citytech 公司开发]

    2) Multifieldpanel - xtype 将多字段存储为 json 对象数组,其中每个字段都是 json 对象的属性。 [由 ACS 开发]

    3) 多字段 - 开箱即用的多字段 xtype。

    对话框结构(以菜单为例):

    multicomposite ( outer multifield ) |-fieldConfigs |- field A (lets say a textfield , for the menu's coulmn name) |- field B (a pathfield to configure the link for the column name ) |- field C (multifield , the inner multifield to hold sub menu entries ) |-fieldConfig - xtype : multifieldpanel |- field Ca (textfield to hold title) |- field Cb (pathfield for the link)

    这将以以下格式存储数据:

    {
    jcr:primaryType: "nt:unstructured",
    menu:
        {
            jcr:primaryType: "nt:unstructured",
            item_1:
            {
              submenu:
              [
                "{"title":"Link 1","link":"http://stackoverflow.com"}",
                "{"title":"Link 2","link":"http://stackoverflow.com"}"
              ],
              title: "Main Menu Item 1",
              jcr:primaryType: "nt:unstructured",
              link: "http://stackoverflow.com"
            },
            item_2:
            {
              submenu:
              [
                "{"title":"Sub menu 1","link":"http://stackoverflow.com"}",
                "{"title":"Sub menu 2","link":"http://stackoverflow.com"}"
              ],
              title: "Main Menu item 2",
              jcr:primaryType: "nt:unstructured",
              link: "http://stackoverflow.com"
            }
        }
    }
    

    您可以在我的帖子here中找到有关此方法的详细说明

    【讨论】:

    • 感谢 Sharath 的回答。绝对是您的回答帮助我得出了答案。我已经更新了我为这个问题找到的答案。
    猜你喜欢
    • 2013-11-17
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    相关资源
    最近更新 更多