Question T2644
Visible to All Users

Regarding Expression

created 5 years ago

Hi Team,

I'm writing expression for multipleText using sumInArray . It's not working.

{
"pages": [
{
"name": "page1",
"elements": [
{
"type": "expression",
"name": "question2",
"expression": "sumInArray({question1},'question3.text1')",
"commentText": "Other (describe)"
},
{
"type": "paneldynamic",
"name": "question1",
"templateElements": [
{
"type": "multipletext",
"name": "question3",
"items": [
{
"name": "text1"
},
{
"name": "text2"
}
]
}
]
}
]
}
]
}

Regards,

Priyadharshini s

Comments (2)

    Hello,

    It doesn't work because multiple text question value is an object with properties associated to row names. You need to write a custom function.

    JavaScript
    function sumOfMultiText(params) { var data = params[0]; var name = params[1]; var res = 0; if(Array.isArray(data)) { res = data.reduce(function(res, item) { var itemData = item[name]; if(!itemData) return res; return res + Object.keys(itemData).reduce(function(itemSum, itemKey) { return itemSum + parseFloat(itemData[itemKey]); }, 0) }, 0); } return res; } Survey.FunctionFactory.Instance.register("sumOfMultiText", sumOfMultiText);

    Here is the working plunker - https://plnkr.co/edit/90GQFETmsKqGp3EZeBsY?p=preview

    Thanks, Serge
    SurveyJS Team

      Hello,

      Yes, please feel free to modify my code for your needs.

      Thanks, Serge
      SurveyJS Team