ant design form表单动态增减表单项Form.List选中Select值后更新相关联Select选项

ant design form表单动态增减表单项Form.List选中Select值后更新相关联Select选项。代码中地市联动获取区县数据:

html 复制代码
<Form name="dynamic_form_nest_item" onFinish={onFinish} autoComplete="off" form={form} initialValues={{ list: [{}] }} >
  <Form.List name="list">
    {(fields, { add, remove }) => (
      <>
        {fields.map(({ key, name, ...restField }, index) => (
          <Row
            key={key}
            gutter={8}
          >
            <Col span={3}>
              <Form.Item
                {...restField}
                name={[name, 'area']}
                rules={[
                  {
                    required: true,
                    message: '请选择地市',
                  },
                ]}
              >
                <Select
                  labelInValue
                  options={areaList}
                  placeholder="请选择地市"
                />
              </Form.Item>
            </Col>
            <Col span={4}>
              <Form.Item
                noStyle={true}
                shouldUpdate={true} // 当 shouldUpdate 为 true 时,Form 的任意变化都会使该 Form.Item 重新渲染
              >
                {() => (
                  <Form.Item
                    {...restField}
                    name={[name, 'city']}
                    rules={[
                      {
                        required: true,
                        message: '请选择区县',
                      },
                    ]}
                  >
                    <Select
                      labelInValue
                      options={ // 这里是获取city数据的关键
                        cityObj?.[ // cityObj是某个地市下city数据的集合 数据类型是对象{地市ID:[]}
                        form.getFieldValue([
                          'list',
                          name,
                          'area'
                        ])?.value // 由于Select使用了labelInValue属性,所以这里取value即地市ID
                        ] || []
                      }
                      placeholder="请选择区县"
                    />
                  </Form.Item>
                )}
              </Form.Item>
            </Col>
            <Col span={1} style={{ textAlign: 'center' }}>
              <MinusCircleOutlined onClick={() => remove(name)} />
            </Col>
          </Row>
        ))}
        <Form.Item >
          <Button type="dashed" onClick={() => add()} block icon={<PlusOutlined />}>
            新增
          </Button>
        </Form.Item>
      </>
    )}
  </Form.List>
  <Form.Item style={{ textAlign: 'center' }}>
    <Button type="primary" htmlType="submit">
      提交
    </Button>
  </Form.Item>
</Form>
相关推荐
埃博拉酱4 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
唐宋元明清21884 天前
.NET 本地Db数据库-技术方案选型
windows·c#
加号34 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
tryCbest4 天前
Windows环境下配置pip镜像源
windows·pip
呉師傅4 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
百事牛科技4 天前
保护文档安全:PDF限制功能详解与实操
windows·pdf
一个人旅程~4 天前
如何用命令行把win10/win11设置为长期暂停更新?
linux·windows·经验分享·电脑
一个假的前端男4 天前
[特殊字符] Flutter 安装完整指南 Windows—— 2026最新版
windows·flutter
科技前瞻观察4 天前
腾讯控股下的销售易,如何重塑中国CRM格局?
microsoft
倚肆5 天前
在 Windows Docker 中安装并配置 Nginx (映射 Windows 端口与路径)
windows·nginx·docker