简体   繁体   English

FormIO - 基于另一个文本字段的输入数据的文本字段动态标签

[英]FormIO - Dynamic labels for Text Field based on the input data of another text field

In my form, I have 2 text fields, Based on the user input from the input field 1, the label of the 2nd field should change.在我的表单中,我有 2 个文本字段,根据输入字段 1 的用户输入,第二个字段的 label 应该更改。 Input 1 label: Name Input 2 label: User, Please enter your age Input 1 label: Name Input 2 label: User, Please enter your age

Now when the user enters the name as John, Label 2 should be changed from 'User, Please enter your age' to 'John, Please enter your age'.现在,当用户输入姓名 John 时,Label 2 应从“用户,请输入您的年龄”更改为“John,请输入您的年龄”。

` `

{
  "title": "Page 1",
  "label": "Page 1",
  "type": "panel",
  "key": "page1",
  "components": [
    {
      "label": "Name",
      "key": "name",
      "type": "textfield",
      "input": true,
      "tableView": true
    },
    {
      "label": "{{ row.name}}, Please enter your age",
      "redrawOn": "name",
      "key": "age",
      "type": "textfield",
      "input": true,
      "tableView": true
    }
  ],
  "input": false,
  "tableView": false
}

` ` 在此处输入图像描述

在此处输入图像描述

If I set label to " {{row.name}}, Please enter your age", Initially when the form load there will not be any data in the name field, so the 2nd label is ", Please enter your age".如果我将 label 设置为“{{row.name}},请输入您的年龄”,最初加载表单时名称字段中不会有任何数据,因此第二个 label 为“,请输入您的年龄”。

Is it possible to have "user", ie "User, Please enter your age" until the name value is entered?在输入名称值之前是否可以有“用户”,即“用户,请输入您的年龄”?

Here is the workaround I followed这是我遵循的解决方法

  1. In the second field label, change the {{row.name}} to dummy variable {{data.name1}}在第二个字段 label 中,将{{row.name}}更改为虚拟变量{{data.name1}}
  2. Change the second field redraw on based on the first field根据第一个字段更改第二个字段重绘
  3. Add the logic in the calculated values section when data.name is empty, set data.name1 value to "User", otherwise assign the data.name to data.name1 here is the snippet当 data.name 为空时,在计算值部分添加逻辑,将 data.name1 值设置为“用户”,否则将 data.name 分配给 data.name1 这里是代码片段
if (data.name === "") {
  data.name1="User";
}else{
  data.name1=data.name;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM