Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
npm
/
glsx-components
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 6b8c9cf0
authored
Apr 27, 2022
by
匡雅琴
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
省市组件
1 parent
f5631554
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
162 additions
and
27 deletions
src/packages/GLProvince/index.js
src/packages/GLProvince/src/GLPrrovince.md
src/packages/GLProvince/index.vue → src/packages/GLProvince/src/index.vue
src/packages/GLProvince/index.js
0 → 100644
View file @
6b8c9cf
import
GLProvince
from
'./src/index.vue'
// 为组件提供 install 安装方法,供按需引入
GLProvince
.
install
=
function
(
Vue
)
{
Vue
.
component
(
GLProvince
.
name
,
GLProvince
)
}
// 默认导出组件
export
default
GLProvince
src/packages/GLProvince/src/GLPrrovince.md
0 → 100644
View file @
6b8c9cf
<!-- <content>
-->
<!-- 只展示省市 -->
<!-- 不需要传subdistrict,默认为2,只展示省市 -->
<GLProvince
ref=
"provinceForm1"
@
selectProvince=
"selectProvince"
@
selectCity=
"selectCity"
></GLProvince>
<!-- 展示省市区 -->
<GLProvince
ref=
"provinceForm2"
:subdistrict=
"subdistrict"
@
selectProvince=
"selectProvince"
@
selectCity=
"selectCity"
@
selectTown=
"selectTown"
></GLProvince>
```
js {mixin:true}
{
data() {
return {
subdistrict: 3
}
},
methods:{
// 获取选择的省份名称和code
selectProvince(provinceName, provinceCode) {
console.log(provinceName, provinceCode)
},
// 获取选择的城市名称和code
selectCity(cityName, cityCode) {
console.log(cityName, cityCode)
},
// 获取选择的地区名称和code
selectTown(townName, townCode) {
console.log(townName, townCode)
},
mounted() {
// 获取省市信息(可传值,具体看index.vue文件介绍)
this.$refs.provinceForm.provinceList()
// 省市区回显
this.$refs.provinceForm.init(provinceName, cityName, townName)
// 如果不需要展示组件,只需要获取数据的话,返回的是promise对象(可传值,具体看index.vue文件介绍)
// const p = this.$refs.provinceForm.provinceList()
// p.then(r => {
// console.log(r)
// })
}
}
```
# 用来作为 markdown 代码的示例部分
<!-- <div v-if="demo1">
-->
```
html
<template>
<GLProvince
ref=
"provinceForm1"
@
selectProvince=
"selectProvince"
@
selectCity=
"selectCity"
></GLProvince>
<GLProvince
ref=
"provinceForm2"
:subdistrict=
"subdistrict"
@
selectProvince=
"selectProvince"
@
selectCity=
"selectCity"
@
selectTown=
"selectTown"
></GLProvince>
</template>
<script>
export
default
{
data
()
{
return
{
subdistrict
:
3
}
},
methods
:
{
selectProvince
(
provinceName
,
provinceCode
)
{
console
.
log
(
provinceName
,
provinceCode
)
},
selectCity
(
cityName
,
cityCode
)
{
console
.
log
(
cityName
,
cityCode
)
},
selectTown
(
townName
,
townCode
)
{
console
.
log
(
townName
,
townCode
)
}
},
mounted
()
{
this
.
$refs
.
provinceForm
.
provinceList
()
this
.
$refs
.
provinceForm
.
init
(
provinceName
,
cityName
,
townName
)
const
p
=
this
.
$refs
.
provinceForm
.
provinceList
()
p
.
then
(
r
=>
{
console
.
log
(
r
)
})
}
}
</script>
```
<!-- </div>
-->
<!-- </content>
-->
src/packages/GLProvince/index.vue
→
src/packages/GLProvince/
src/
index.vue
View file @
6b8c9cf
<
template
>
<
template
>
<div>
<div
v-if=
"provinceView"
>
<el-form
ref=
"provinceForm"
:model=
"provinceForm"
inline=
"true"
class=
"province"
>
<el-form
ref=
"provinceForm"
:model=
"provinceForm"
inline=
"true"
class=
"province"
>
<el-select
size=
"small"
v-model=
"provinceForm.provinceName"
clearable
placeholder=
"选择省份"
:style=
"
{ width: defaultWidth }" @change="selectProvince" class="select-padding">
<el-select
size=
"small"
v-model=
"provinceForm.provinceName"
clearable
placeholder=
"选择省份"
:style=
"
{ width: defaultWidth }" @change="selectProvince" class="select-padding">
<el-option
v-for=
"item in AllData"
:key=
"item.adcode"
:label=
"item.name"
:value=
"item.name"
@
click
.
native=
"selectProvince(item)"
></el-option>
<el-option
v-for=
"item in AllData"
:key=
"item.adcode"
:label=
"item.name"
:value=
"item.name"
@
click
.
native=
"selectProvince(item)"
></el-option>
...
@@ -24,10 +24,15 @@ export default {
...
@@ -24,10 +24,15 @@ export default {
type
:
Number
,
type
:
Number
,
default
:
2
default
:
2
},
},
// 省市下拉宽度
// 省市下拉
组件
宽度
defaultWidth
:
{
defaultWidth
:
{
type
:
String
,
type
:
String
,
default
:
'150px'
default
:
'150px'
},
// 组件是否展示,默认展示
provinceView
:
{
type
:
Boolean
,
default
:
true
}
}
},
},
data
()
{
data
()
{
...
@@ -54,27 +59,78 @@ export default {
...
@@ -54,27 +59,78 @@ export default {
}
}
}
}
},
},
mounted
()
{
mounted
()
{},
this
.
mapInit
()
},
methods
:
{
methods
:
{
/*
* @params:传入的为一个key-value的对象
1、keywords --查询关键字,只支持单个关键词语搜索关键词支持:行政区名称、citycode、adcode
列如:this.provinceList({keywords: '山东省'}),这样就可以筛选出山东省下的市、区信息
2、subdistrict --设置显示下级行政区级数,0:不返回下级行政区;1:返回下一级行政区;2:返回下两级行政区;3:返回下三级行政区;
不传的话默认为2,可以在组件中穿,也可以在调用方法的时候传,如果需要展示省市组件,传值最小为2
3、page --最外层的districts最多会返回20个数据,若超过限制,请用page请求下一页数据。
例如page=2;page=3。默认page=1
4、extensions --控制行政区信息中返回行政区边界坐标点; 可选值:base、all;
base:不返回行政区边界坐标点;(默认)
all:只返回当前查询district的边界值,不返回子节点的边界值;目前不能返回乡镇/街道级别的边界值
5、filter -- 按照指定行政区划进行过滤,填入后则只返回该省/直辖市信息,需填入adcode,为了保证数据的正确,强烈建议填入此参数
6、callback -- callback值是用户定义的函数名称,此参数只在output=JSON时有效
7、output --返回数据格式类型,可选值:JSON,XML(默认JSON)
* @return:返回的是一个promise对象,如果不需要展示组件,只需要获取值,可以直接调用方法,异步获取返回值
列如:
const p = this.$refs.provinceForm.provinceList({xxxx(可传值,传值可参考上文params描述) })
p.then(r => {
// r 为返回结果
console.log(r)
})
**/
// 获取省市信息
provinceList
(
params
)
{
params
.
subdistrict
?
''
:
(
params
.
subdistrict
=
this
.
subdistrict
)
let
data
=
''
for
(
let
key
in
params
)
{
console
.
log
(
params
[
key
])
data
+=
'&'
+
key
+
'='
+
params
[
key
]
}
let
path
=
`https://restapi.amap.com/v3/config/district?key=d22348363c34c266f772ae9b635eef2a`
path
=
path
+
data
const
pro
=
fetch
(
path
).
then
(
res
=>
{
return
res
.
json
()
})
if
(
this
.
provinceView
)
{
pro
.
then
(
r
=>
{
this
.
AllData
=
r
.
districts
[
0
].
districts
})
}
return
pro
},
// 省市回显
// 省市回显
init
(
province
,
city
)
{
init
(
province
,
city
,
town
)
{
this
.
provinceForm
=
{
this
.
provinceForm
=
{
provinceName
:
province
,
provinceName
:
province
,
cityName
:
city
cityName
:
city
,
townName
:
town
}
}
// this.mapInit(province, city)
if
(
province
)
{
if
(
province
)
{
this
.
AllData
.
map
(
item
=>
{
this
.
AllData
.
map
(
item
=>
{
if
(
item
.
name
.
indexOf
(
province
)
!=
-
1
)
{
if
(
item
.
name
.
indexOf
(
province
)
!=
-
1
)
{
this
.
provinceForm
.
provinceName
=
item
.
name
this
.
provinceForm
.
provinceName
=
item
.
name
this
.
cityList
=
item
.
district
List
this
.
cityList
=
item
.
district
s
this
.
provinceForm
.
provinceCode
=
item
.
adcode
this
.
provinceForm
.
provinceCode
=
item
.
adcode
this
.
cityList
.
map
(
itm
=>
{
this
.
cityList
.
map
(
itm
=>
{
if
(
itm
.
name
.
indexOf
(
city
)
!=
-
1
)
{
if
(
itm
.
name
.
indexOf
(
city
)
!=
-
1
)
{
this
.
provinceForm
.
cityCode
=
itm
.
adcode
this
.
provinceForm
.
cityCode
=
itm
.
adcode
this
.
provinceForm
.
cityName
=
itm
.
name
this
.
provinceForm
.
cityName
=
itm
.
name
this
.
townList
=
itm
.
districts
if
(
town
)
{
this
.
townList
.
map
(
itmx
=>
{
if
(
itmx
.
name
.
indexOf
(
town
)
!=
-
1
)
{
this
.
provinceForm
.
townCode
=
itmx
.
adcode
this
.
provinceForm
.
townName
=
itmx
.
name
}
})
}
}
}
})
})
}
}
...
@@ -84,34 +140,18 @@ export default {
...
@@ -84,34 +140,18 @@ export default {
// 选择省
// 选择省
selectProvince
(
val
)
{
selectProvince
(
val
)
{
this
.
provinceForm
.
cityName
=
''
this
.
provinceForm
.
cityName
=
''
this
.
cityList
=
val
.
district
List
this
.
cityList
=
val
.
district
s
this
.
$emit
(
'selectProvince'
,
val
.
name
,
val
.
adcode
)
this
.
$emit
(
'selectProvince'
,
val
.
name
,
val
.
adcode
)
},
},
// 选择市
// 选择市
selectCity
(
val
)
{
selectCity
(
val
)
{
this
.
provinceForm
.
townName
=
''
this
.
provinceForm
.
townName
=
''
this
.
townList
=
val
.
district
List
this
.
townList
=
val
.
district
s
this
.
$emit
(
'selectCity'
,
val
.
name
,
val
.
adcode
)
this
.
$emit
(
'selectCity'
,
val
.
name
,
val
.
adcode
)
},
},
// 选择区
// 选择区
selectTown
(
val
)
{
selectTown
(
val
)
{
this
.
$emit
(
'selectTown'
,
val
.
name
,
val
.
adcode
)
this
.
$emit
(
'selectTown'
,
val
.
name
,
val
.
adcode
)
},
mapInit
()
{
map
=
new
AMap
.
Map
(
'district'
,
{
resizeEnable
:
true
})
AMap
.
plugin
([
'AMap.DistrictSearch'
],
()
=>
{
var
opts
=
{
subdistrict
:
this
.
subdistrict
,
//返回下一级行政区
level
:
'city'
,
//查询的范围
showbiz
:
true
//查询行政级别为 市
}
var
districtSearch
=
new
AMap
.
DistrictSearch
(
opts
)
districtSearch
.
search
(
'中国'
,
(
status
,
result
)
=>
{
this
.
AllData
=
result
.
districtList
[
0
].
districtList
})
})
}
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment