首页 > 解决方案 > 向 SurveyJS 评级组件添加额外的字段

问题描述

我们想在 SurveyJS 中的 Rating-component 中添加一个额外的“N/A”和“Don't know”按钮,用于具有自定义值的 React。例如,我们有值 1-2-3-4-5 和 N/A。选择此按钮时,最终状态的值应为“N/A”。

我们尝试的是添加一个渲染后功能:

var nvtRatingAfterRender = function(question, el) {
  const parent = el.getElementsByClassName("sv_q_rating")[0];
  const newChild = parent.children[0].cloneNode(true);
  newChild.children[0].value = -99;
  newChild.getElementsByClassName("sv_q_rating_item_text")[0].innerText = "NVT";
  newChild.classList.add("nvt-option");
  parent.appendChild(newChild);
};

并将其添加到配置中。这会导致 React 不接受纯 HTML/JavaScript 按钮的错误:

Uncaught Error: ReactDOMInput: Mixing React and non-React radio inputs with the same `name` is not supported.

它没有添加到最后的状态中,也不可能使其“活跃”(下面我们在评级中添加了“NVT”):

WaCHOwQgNj.gif

另一种方法可能是复制整个 Rating 组件的 JavaScript 代码,然后添加一些额外的值(从 min 到 max 循环,还添加额外的按钮)。

向参与组件的 Rating 组件添加自定义按钮的好方法是什么?

标签: javascriptreactjssurveyjs

解决方案


SurveyJS 团队的回答:

您好,
您可以将rateValues用于此目的。这是工作示例和 JSON:

{
    type: "rating",
    name: "satisfaction",
    title: "How satisfied are you with the Product?",
    rateValues: [
        1, 2, 3, 4, 5, {value: 'anyvalue', text: "NVT"}
    ]
}

谢谢
Andrew
SurveyJS 团队


推荐阅读