0

`I'm working on a survey using SurveyJS, and I need help configuring a matrix dropdown question. My goal is to set a default value for a specific cell in this matrix.

Problem Description

In my survey, I have a matrix dropdown question where respondents provide information for different categories (Cat 1, Cat 2, Cat 3). Each row represents a category, and the columns include fields like "Base year end," "Base year value," and "Methodological details."

I want to set a default value for the "Base year value" cell specifically in the "Cat 1" row. However, my current approach sets the default value across all rows for the "Base year value" column, which is not what I need.

Current Configuration

Here's the relevant part of my survey's JSON configuration:`

{
 "pages": [
  {
   "name": "performance",
   "elements": [
    {
     "type": "panel",
     "name": "7.5Panel",
     "elements": [
      {
       "type": "matrixdropdown",
       "name": "1f901b6a-4201-49ee-8169-599c1a8b9f21",
       "title": "Provide your base year",
       "description": "A meaningful data",
       "hideNumber": true,
       "columns": [
        {
         "name": "ed2e7ac8-f1c6-470d-8c57-11da62471247",
         "title": "Base year end",
         "cellType": "text",
         "inputType": "date"
        },
        {
         "name": "51f0a4d0-4713-464c-b913-8d08a0cec24c",
         "title": "Base year value",
         "cellType": "text",
         "useDisplayValuesInDynamicTexts": false,
         "placeholder": "decimal places: 3"
        }
       ],
       "rows": [
        {
         "value": "c28454f6-4a7e-4ec7-8882-307a1009b1cb",
         "text": "Cat 1"
        },
        {
         "value": "c6a2c79b-c8bf-465d-96ca-cd2467815c5b",
         "text": "Cat 2"
        }
       ]
      }
     ]
    }
   ]
  }
 ],
 "showQuestionNumbers": "off"
}

Attempted Solution

I tried to set the default value using the defaultValueExpression property in the column definition, but it applied the value to all rows:

{
 "name": "51f0a4d0-4713-464c-b913-8d08a0cec24c",
 "title": "Base year value",
 "cellType": "text",
 "useDisplayValuesInDynamicTexts": false,
 "defaultValueExpression": "iif({row.c28454f6-4a7e-4ec7-8882-307a1009b1cb} = '', '1000', '')",
 "placeholder": "decimal places: 3"
}

`Desired Outcome

I need to set the default value for the "Base year value" cell only in the "Cat 1" row, without affecting the other rows.

Question

How can I configure the JSON to set a default value for the "Base year value" in the "Cat 1" row only?

Any help or guidance on how to achieve this would be greatly appreciated! Thank you!

`

1 Answer 1

2

Thanks for describing the problem in a way that makes it easy to understand and reproduce. Cheers!

It is indeed the intent of the Multi-Select Matrix to have columns defining the types of questions, e.g. "Base year value" and instantiate each of these question definitions for every row defined in the matrix question.

Your intuition is correct - the defaultValueExpression is the way to go. The iif function accepts an expression and can be used to achieve your goal, but I wouldn't go with it as it will be very hard to set the correct boolean expression (even for developers). Also, it might be limiting you in case you want to expand the approach further by setting default value for every row or even every cell.

I would propose a solution based of a custom function that you can register to take full control over the default value resolution. SurveyJS is very flexible and there's a defined way to extend the defaultValueExpression with custom functions.

Here is the official documentation.

Essentially, you will need to define your custom function and register it with your Survey FunctionFactory only once before creating the instance. Here's how:


function getDefaultBaseYearValue(params) {  
    const currentRowName = this.row.name; // obtain row name
    const cat1RowName = "c28454f6-4a7e-4ec7-8882-307a1009b1cb";

     if (currentRowName == cat1RowName){
        return 1000; // return your desired default value for that row
     } else {
        return '';  // return NO default value
     }
}

// register the function with its name "getDefaultBaseYearValue"
Survey.FunctionFactory.Instance.register("getDefaultBaseYearValue", getDefaultBaseYearValue);

// Logic to create your Survey or SurveyCreator instances...

....

And in your survey JSON, you can use it like this:

...
{
    "name": "51f0a4d0-4713-464c-b913-8d08a0cec24c",
    "title": "Base year value",
    "cellType": "text",
    "useDisplayValuesInDynamicTexts": false,
    "defaultValueExpression": "getDefaultBaseYearValue()",
    "placeholder": "decimal places: 3"
}
...

That's it! One thing that be added to further improve and solidify the approach is to enable business users to modify the default values in the Survey Creator. Actually, it's not too hard to do it - there's guide on how to extend the property grid with custom properties.

It's simple as calling Survey.SerializeraddProperty(questionType, propertySettings), which in your case will be:

Survey.Serializer.addProperty("matrixdropdown", {
    name: "baseYearDefaultValues",
    displayName: "Default Values for Base Year Value",
    visibleIndex: 3,
    category: "general",
    type: "itemvalues"
});

Note that the type is "itemvalues", which is essentially an array of text and value, which is great for this case as you can add the name of the "row" and the default value and end up with option to control this via the editor (see below)

enter image description here

Full Sample

Full sample is here - https://plnkr.co/edit/uFJtnHF7bh2DelrL

Notes

This can be surely improved, but should be a good starting guide. Few notes:

  • If there's a way to add the custom property on the row itself, will be a much better experience to manage, but rows seem limited to 2 properties only. Thus, I have added the property to the matrixdropdown
  • You can pass params to the getDefaultBaseYearValue and probably simplify the function logic even more, but that was not my focus. Maybe that's something you can explore if you want even simple function logic flow

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.