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>
相关推荐
玉梅小洋7 小时前
Windows 10 Android 构建配置指南
android·windows
雨中风华13 小时前
Linux, macOS系统实现远程目录访问(等同于windows平台xFsRedir软件的目录重定向)
linux·windows·macos
yuuki23323315 小时前
【C++】继承
开发语言·c++·windows
非凡ghost15 小时前
PowerDirector安卓版(威力导演安卓版)
android·windows·学习·软件需求
疯狂敲代码的老刘17 小时前
JDK 1.6到25 全版本网盘合集 (Windows + Mac + Linux)
java·linux·windows·macos·jdk
千逐6818 小时前
多物理场耦合气象可视化引擎:基于 Flutter for OpenHarmony 的实时风-湿-压交互流体系统
flutter·microsoft·交互
吹牛不交税18 小时前
安装Framework4.0时提示:Microsoft .NET Framework 4 已是此操作系统的一部分。不需要安装 .NET Framework
microsoft·.net
love530love19 小时前
Windows 下 GCC 编译器安装与排错实录
人工智能·windows·python·gcc·msys2·gtk·msys2 mingw 64
猫头虎19 小时前
OpenClaw开源汉化发行版:介绍、下载、安装、配置教程
运维·windows·开源·aigc·ai编程·agi·csdn
luffy545919 小时前
windows下通过docker-desktop创建redis实例
windows·redis·docker·容器