如何在SurveyJS中查看自定义问题类型的内部变量?

问题描述 投票:0回答:1

问题

我定义了一个名为cascadedropdown的客户问题类型,代码如下:

ComponentCollection.Instance.add({
  // A unique name; must use lowercase
  name: 'casecadedropdown', // A display name used in the Toolbox
  title: 'casecade dropdown', // A default title for questions created with this question type
  defaultQuestionTitle: 'choose an option',
  elementsJSON: [{
      type: 'dropdown',
      name: 'question1',
      title: 'question1',
      choices: levelOneArray, 
 },{
      type: 'dropdown',
      name: 'question2',
      title: 'question2',
      startWithNewLine: false,
      choices:[],
 },{
      type: 'dropdown',
      name: 'question3',
      title: 'question3',
      startWithNewLine: false,
      choices:[],
      visible: {questionListLen} > 2
 },{
      type: 'dropdown',
      name: 'question4',
      title: 'question4',
      startWithNewLine: false,
      choices:[],
      visible: {questionListLen} > 3
 }
 ],
    calculatedValues:[{
        name:"questionListLen",
        expression: casecadeQuestionList.length
 } ],
  inheritBaseProps: true,
}]

cascadedropdown
加载后,casecadeQuestionList会被赋值为另一个值,question3question4是否显示取决于casecadeQuestionList.length,如何查看casecadeQuestionList.length的变化?

尝试 我在上面定义了计算值,但是不起作用。我该怎么办?

javascript surveyjs
1个回答
0
投票

我应该改变决定是否添加第三个或第四个下拉列表的方式,因此级联下拉问题的原型需要重新定义。

ComponentCollection.Instance.add({
  // A unique name; must use lowercase
  name: 'cascadedropdown', // A display name used in the Toolbox
  title: 'cascade dropdown', // A default title for questions created with this question type
  defaultQuestionTitle: 'Please choose the option',
  elementsJSON: [
    {
      type: 'dropdown',
      name: 'question1',
      title: 'question1',
      placeholder: 'Select',
      choices: []
    },
    {
      type: 'dropdown',
      name: 'question2',
      title: 'quesiton2',
      placeholder: 'Select',
      choices: [],
      startWithNewLine: false
    }
  ],
})

重点是在设计器模式下访问casecadeDropdown的实例。我发现调用

creator.onSelectedElementChanged
可以访问鼠标焦点的实例。代码如下:

const isCascadeDropdown = (focusedElemetJSON) => {
  return focusedElemetJSON?.hasOwnProperty("customQuestion") 
         && focusedElemetJSON?.jsonObj.type == "cascadedropdown" 
}

creator.onSelectedElementChanged.add((sender,options)=>{
  const focusedElemetJSON = options.newSelectedElement
  // access the json object of cascade dropdown type question which user focused
  if(isCascadeDropdown(focusedElemetJSON)){
    casecadeJSON.value = focusedElemetJSON.customQuestion.json.elementsJSON
  }
})
© www.soinside.com 2019 - 2024. All rights reserved.