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>
相关推荐
sun0077001 小时前
SniffMaster(读取苹果的ats文件) 和 wireshark
windows
caimouse5 小时前
Reactos 第 5 章 进程与线程 — 5.8 Windows 的 APC 机制
c语言·windows
PHP隔壁老王邻居6 小时前
windows菜单搜索栏无法显示历史记录或者无法使用修复方法
windows
道一236 小时前
Windows系统查看端口占用进程的3种实用方法
windows·笔记
半条-咸鱼6 小时前
【INACCESSIBLE_BOOT_DEVICE】安装 Config Tool 后 Windows 蓝屏,最终通过 VMware 虚拟机解决
windows·stm32·vmware·芯片
人工小情绪7 小时前
Windows 安装 Codex 桌面版,并用 CC Switch 管理配置
人工智能·windows·codex·cc switch
caimouse9 小时前
Reactos 第 5 章 进程与线程 — 5.11 线程本地存储 TLS
c语言·windows
李小白6610 小时前
第二天-认识Windows
windows
liu64491133711 小时前
claude code 安装
windows
caimouse11 小时前
Reactos 第 5 章 进程与线程 — 5.9 Windows 线程的调度和切换
windows