From 5fae2b02eae7dc91baef774ca9dfdf0da91b8040 Mon Sep 17 00:00:00 2001 From: M69W <15809201+M69W@users.noreply.github.com> Date: Mon, 26 Apr 2021 20:53:33 +0800 Subject: [PATCH] perf: add Coordinating the selection of provinces and cities (#534) Co-authored-by: M69W --- src/views/demo/form/index.vue | 97 +++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/src/views/demo/form/index.vue b/src/views/demo/form/index.vue index 8417f3c5b69..47bef35be92 100644 --- a/src/views/demo/form/index.vue +++ b/src/views/demo/form/index.vue @@ -19,6 +19,58 @@ import { PageWrapper } from '/@/components/Page'; import { optionsListApi } from '/@/api/demo/select'; + + const provincesOptions = [ + { + id: 'guangdong', + label: '广东省', + value: '1', + key: '1', + }, + { + id: 'jiangsu', + label: '江苏省', + value: '2', + key: '2', + }, + ]; + const citiesOptionsData = { + guangdong: [ + { + label: '珠海市', + value: '1', + key: '1', + }, + { + label: '深圳市', + value: '2', + key: '2', + }, + { + label: '广州市', + value: '3', + key: '3', + }, + ], + jiangsu: [ + { + label: '南京市', + value: '1', + key: '1', + }, + { + label: '无锡市', + value: '2', + key: '2', + }, + { + label: '苏州市', + value: '3', + key: '3', + }, + ], + }; + const schemas: FormSchema[] = [ { field: 'field1', @@ -236,6 +288,51 @@ span: 8, }, }, + { + field: 'province', + component: 'Select', + label: '省份', + colProps: { + span: 8, + }, + componentProps: ({ formModel, formActionType }) => { + return { + options: provincesOptions, + placeholder: '省份与城市联动', + onChange: (e: any) => { + // console.log(e) + let citiesOptions = + e == 1 + ? citiesOptionsData[provincesOptions[0].id] + : citiesOptionsData[provincesOptions[1].id]; + // console.log(citiesOptions) + if (e === undefined) { + citiesOptions = []; + } + formModel.city = undefined; // reset city value + const { updateSchema } = formActionType; + updateSchema({ + field: 'city', + componentProps: { + options: citiesOptions, + }, + }); + }, + }; + }, + }, + { + field: 'city', + component: 'Select', + label: '城市', + colProps: { + span: 8, + }, + componentProps: { + options: [], // defalut [] + placeholder: '省份与城市联动', + }, + }, ]; export default defineComponent({