Use Panels to Group Related Questions
If your survey or form contains related questions, you can organize them in groups to give respondents context and help them better understand the purpose of each question. A survey element that groups other survey elements on a page is Panel. This demo shows how to create and configure panels.
Create a Panel
To add a panel to your survey or form, define an object with the type
property set to "panel"
and add it to the elements
array. Within this object, specify the panel's title
and a unique name
that identifies the panel. Optionally, you can specify a description
to place under the title
.
To add questions to a panel, declare a nested elements
array and populate it with question configurations. Since panels can be nested one into another, the elements
array can also contain configurations of other panels.
Expand and Collapse Panels
Panels can be expanded and collapsed in code or in the UI. When collapsed, a panel displays only its title
and description
. To expand or collapse a panel, users should click its title. This feature helps you optimize screen space when your survey contains many questions.
To expand or collapse a panel, call its expand()
or collapse()
method. To find out the current state of a panel and decide which method to call, use the isExpanded
and isCollapsed
properties. Alternatively, you can call the toggleState()
method to toggle a panel's expand state. All these methods and properties belong to a PanelModel
object instance. To access it, use SurveyModel
's getPanelByName()
method:
import { Model } from "survey-core";
const surveyJson = {
// ...
"elements": [{
"type": "panel",
"name": "myPanel",
"elements": [ ... ]
}]
};
const survey = new Model(surveyJson);
// Access a panel with a specified name
const myPanel = survey.getPanelByName("myPanel");
// Call `toggleState()` or any other `PanelModel` method
myPanel.toggleState();
To allow users to expand and collapse a panel, set its state
property to "expanded"
or "collapsed"
, depending on the initial state you want. In this demo, you can expand and collapse both panels.