surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.2k stars 812 forks source link

How can I add ItemValue custom property in a matrix to the results output of a survey? #7534

Closed DworrallRC closed 4 months ago

DworrallRC commented 10 months ago

Are you requesting a feature, reporting a bug or asking a question?

Asking a question

What is the current behavior?

Custom properties added to ItemValue (rows) in a Single-Select Matrix are not appearing in the results output (survey.data) of the survey.

image

What is the expected behavior?

When using survey.data or Survey.getPlainData, I would have expected the additional properties to be included in the json for the matrix results.

I have added the properties to matrix when running the survey.

image

How would you reproduce the current behavior (if this is a bug)?

survejs matrix custom property output code.txt

Specify your

DworrallRC commented 10 months ago

To try and clarify a little, when you call survey.getQuestionByName('question2').rows the properties are visible and have values for the ItemValue rows in the matrix:

image

I am trying to get the abbreviation (or any other property) to accompany the selected answer for each Itemvalue/row in the matrix in the results json - NOT a total.

andrewtelnov commented 10 months ago

@DworrallRC It should not be in the data since we store there question name (valueName) and question value. How do you want to get the result? What format are you looking for? Right now it is: ItemValue.value (where ItemValue is a row) and a row.value {rowName: rowValue}.

Thank you, Andrew

DworrallRC commented 10 months ago

@andrewtelnov I'm looking for a mechanism to be able to include the associated properties for each row with the answers selected. The properties have been sucessfully configured - they are available in the definition. But these properties are then required in the results to categorise and enhance the selected answer. There needs to be a way to access the properties for each row alongside it's selected value.

In the meantime, I am transforming my own results output by combining the answers from plainData with the extra properties from definition:

`

for (const row in question.rows) {
     var row_data = question.rows[row];
     var plainData = question.getPlainData();
     const row_item = {
         id: row_data.id,    // this is the FFM Question ID for matrix rows
         value: row_data.value,
         text: row_data.text,
         abbreviation: row_data.abbreviation,
         score: plainData.value[row_data.value],
         scoreTransformOption: row_data.scoreTransformOption        
     }                

     if (row_item != null && !!row_item && row_item.id != null) { 
         matrixRows.push(row_item);
     }
 }

`

JaneSjs commented 8 months ago

Hello @DworrallRC, Please accept my apologies for the delayed reply. In a Single-Select Matrix, the data array returns an array of objects which store column properties rather than row properties. Therefore, you would be able to include custom properties to column definitions and obtain them using the survey.getPlainData function passing the calculations object as it's demonstrated in the following demo: Scored Survey.

However, if you wish to attach custom properties to rows, you may need to iterate through QuestionMatrixModel.visibleRows array and generate a new response object. Consider the following demo: View Plunker.

 survey.onComplete.add((sender, options) => {
        console.log(JSON.stringify(sender.data, null, 3));
        const matrix = sender.getQuestionByName('question1');
        const res = [];
        matrix.visibleRows.forEach(row => {
            let rowObject = {
                [row.item.value]: row.value,
                abbreviation: row.item.abbreviation,
                scoreTransformOption: row.item.scoreTransformOption
            }
            res.push(rowObject);
});

Please let us know if you have further questions.