Question T19842
Visible to All Users
Duplicate

We have closed this ticket because another page addresses its subject:

Expressions in Matrix

Assign a field with the value of another using the trigger's setValue

created 4 months ago

Hello,

I have a scenario where I need the result of the sum of 2 fields of a matrix to be displayed in another matrix.

Is there any way to perform the calculation within the trigger's setValue property or just assign the value of another field to a field?

I tried to do something like the attached example, but I was unsuccessful. setValue apparently does not accept values from other fields or calculations, only constant values.

I have the premise of keeping the calculation in JSON, as I know that this can be resolved by calling an external javascript function.

Answers approved by surveyjs Support

created 4 months ago

Hello André,
Thank you for sharing the JSON. From what I gather, you wish to produce the following calculations for each matrix row:
Clipboard-File-1.png

You can achieve this output by creating individual input fields and specifying the setValueExpression to copy a corresponding cell value or perform calculations over target cells. To follow a grid layout, wrap input fields within a panel and enable the grid layout. Consider the updated survey JSON:

JSON
{ "title": "Prescrição", "logoPosition": "right", "pages": [ { "name": "page1", "elements": [ { "type": "radiogroup", "name": "questaoTipoPrescricao", "maxWidth": "1200px", "title": "Tipo de Refração:", "titleLocation": "left", "hideNumber": true, "defaultValue": "opcPerto", "choices": [ { "value": "opcLonge", "text": "Longe" }, { "value": "opcPerto", "text": "Perto" } ], "colCount": 2 }, { "type": "matrixdropdown", "name": "questaoRefracaoLonge", "maxWidth": "1200px", "title": "Refração Dinâmica-Longe", "hideNumber": true, "defaultValue": { "od": { "esf": 1, "cil": 2, "eixo": 3, "av": 4, "adicao": 4, "avp": 4 }, "oe": { "esf": 0.25, "cil": 34, "eixo": 44, "av": 4, "adicao": 3, "avp": 5 } }, "columns": [ { "name": "esf", "cellType": "text", "inputType": "number", "min": -30, "max": 30, "step": 0.25 }, { "name": "cil", "cellType": "text", "inputType": "number", "min": -30, "max": 30, "step": 0.25 }, { "name": "eixo", "cellType": "text", "inputType": "number", "min": 0, "max": 360 }, { "name": "av", "title": "AV", "cellType": "text", "inputType": "number", "min": 1, "max": 11 }, { "name": "adicao", "title": "Adição", "cellType": "text", "visibleIf": "{questaoTipoPrescricao} = 'opcPerto'", "inputType": "number", "min": -30, "max": 30, "step": 0.25 }, { "name": "avp", "title": "AV Perto", "cellType": "text", "visibleIf": "{questaoTipoPrescricao} = 'opcPerto'", "inputType": "number", "min": 1, "max": 11 } ], "choices": [ 1, 2, 3, 4, 5 ], "cellType": "text", "rows": [ { "value": "od", "text": "OD" }, { "value": "oe", "text": "OE" } ] }, { "type": "panel", "name": "panel1", "readOnly": true, "questionTitleWidth": "50px", "questionTitleLocation": "left", "layoutColumns": [ { "width": 35, "questionTitleWidth": "130px" }, { "width": 20 }, { "width": 20 }, { "width": 20 } ], "minWidth": "300px", "maxWidth": "1200px", "showQuestionNumbers": "off", "elements": [ { "type": "text", "name": "question1", "minWidth": "", "maxWidth": "", "title": "OD", "setValueExpression": "{questaoRefracaoLonge.od.esf} + {questaoRefracaoLonge.od.adicao}" }, { "type": "text", "name": "question3", "minWidth": "", "maxWidth": "", "startWithNewLine": false, "title": "OD", "titleLocation": "hidden", "setValueExpression": "{questaoRefracaoLonge.od.cil}" }, { "type": "text", "name": "question4", "minWidth": "", "maxWidth": "", "startWithNewLine": false, "title": "OD", "titleLocation": "hidden", "setValueExpression": "{questaoRefracaoLonge.od.eixo}" }, { "type": "text", "name": "question5", "minWidth": "", "maxWidth": "", "startWithNewLine": false, "title": "OD", "titleLocation": "hidden", "setValueExpression": "{questaoRefracaoLonge.od.av}" }, { "type": "text", "name": "question2", "minWidth": "0px", "maxWidth": "30%", "title": "OE", "setValueExpression": "{questaoRefracaoLonge.oe.esf} + {questaoRefracaoLonge.oe.adicao}" }, { "type": "text", "name": "question6", "minWidth": "", "maxWidth": "", "startWithNewLine": false, "title": "OD", "titleLocation": "hidden", "setValueExpression": "{questaoRefracaoLonge.oe.cil}" }, { "type": "text", "name": "question7", "minWidth": "", "maxWidth": "", "startWithNewLine": false, "title": "OD", "titleLocation": "hidden", "setValueExpression": "{questaoRefracaoLonge.od.eixo}" }, { "type": "text", "name": "question8", "minWidth": "", "maxWidth": "", "startWithNewLine": false, "title": "OD", "titleLocation": "hidden", "setValueExpression": "{questaoRefracaoLonge.oe.av}" } ] } ] } ], "gridLayoutEnabled": true }

Clipboard-File-2.png

Let me know if you have any further questions.

    Comments (2)

      Hi Jane,
      That's exactly the idea. However, we weren't working with the idea of individual fields to receive the results, but rather with 2 matrix elements. After all, the fields that would receive the results are identical to the matrix where the data is filled in.

      Just confirm this for me:

      1 - Using the 2 matrices, as in the original JSON, wouldn't it be possible to perform calculations and assignments to the fields using only the expressions in the JSON?

      2 - When using triggers, doesn't setValue support calculations and assignments using the value of another field? Example: "eixo": "{od.eixo}".

      Thanks for your feedback.

        Hello André,

        1. It is possible to copy cell values from one matrix to another. For this, you may use the valueName setting or setValueExpression. However, it is impossible to copy and perform calculations within a single individual cell/column. If you already copy values from another matrix new copied values would simply override custom calculations.
        2. The setValue trigger may not be used to copy values from one question to another. To copy a matrix value, it is recommended to use the valueName or setValueExpression. In the following survey JSON definition, the second matrix copies values of the first matrix.
        JSON
        { "title": "Prescrição", "logoPosition": "right", "pages": [ { "name": "page1", "elements": [ { "type": "radiogroup", "name": "questaoTipoPrescricao", "maxWidth": "1200px", "title": "Tipo de Refração:", "titleLocation": "left", "hideNumber": true, "choices": [ { "value": "opcLonge", "text": "Longe" }, { "value": "opcPerto", "text": "Perto" } ], "colCount": 2 }, { "type": "matrixdropdown", "name": "questaoRefracaoLonge", "maxWidth": "1200px", "title": "Refração Dinâmica-Longe", "hideNumber": true, "columns": [ { "name": "esf", "cellType": "text", "inputType": "number", "min": -30, "max": 30, "step": 0.25 }, { "name": "cil", "cellType": "text", "inputType": "number", "min": -30, "max": 30, "step": 0.25 }, { "name": "eixo", "cellType": "text", "inputType": "number", "min": 0, "max": 360 }, { "name": "av", "title": "AV", "cellType": "text", "inputType": "number", "min": 1, "max": 11 }, { "name": "adicao", "title": "Adição", "cellType": "text", "visibleIf": "{questaoTipoPrescricao} = 'opcPerto'", "inputType": "number", "min": -30, "max": 30, "step": 0.25 }, { "name": "avp", "title": "AV Perto", "cellType": "text", "visibleIf": "{questaoTipoPrescricao} = 'opcPerto'", "inputType": "number", "min": 1, "max": 11 } ], "choices": [ 1, 2, 3, 4, 5 ], "cellType": "text", "rows": [ { "value": "od", "text": "OD" }, { "value": "oe", "text": "OE" } ] }, { "type": "matrixdropdown", "name": "questaoRefracaoPerto", "visibleIf": "{questaoTipoPrescricao} = 'opcPerto'", "maxWidth": "1200px", "title": "Refração Dinâmica-Perto", "hideNumber": true, "setValueExpression": "{questaoRefracaoLonge}", "readOnly": true, "columns": [ { "name": "esf", "cellType": "text", "setValueIf": "{questaoRefracaoLonge.od.adicao} notempty", "inputType": "number" }, { "name": "cil", "cellType": "text", "inputType": "number" }, { "name": "eixo", "cellType": "text", "inputType": "number" }, { "name": "av", "title": "AV", "cellType": "text", "inputType": "number" } ], "cellType": "text", "rows": [ { "value": "od", "text": "OD" }, { "value": "oe", "text": "OE" } ] } ] } ] }

        Clipboard-File-1.png

        As an option, you may try using the survey.onMatrixCellValueChanged function to post process the cell value for your second matrix and calculate the required summary.

        Should you have any further questions, please let us know.