Giter Club home page Giter Club logo

v-distpicker's Introduction

Powered By Jiajian Chan

A flexible, highly available district selector for picking provinces, cities and districts of China.

V - Distpicker

github-stars github-stars download-image

Documents and Demo

[English] | 简体中文

CHANGELOG

Installation

Vue 2

npm install v-distpicker@^1.3.3 --save

Vue 3

npm install v-distpicker@^2.1.0 --save

CDN

<script src="https://cdn.jsdelivr.net/npm/v-distpicker@version/dist/v-distpicker.js"></script>
<!-- or -->
<script src="https://unpkg.com/v-distpicker@version/dist/v-distpicker.js"></script>

Usage

Register component

Registe global component:

import VDistpicker from 'v-distpicker'
const app = createApp(App)
app.component('v-distpicker', VDistpicker)

Use setup-api:

<script setup>import VDistpicker from 'v-distpicker'</script>

Registe component:

import { defineComponent } from 'vue'
import VDistpicker from 'v-distpicker'

export default defineComponent({
  components: { VDistpicker },
})

How to use

Basic:

<v-distpicker></v-distpicker>

Default Value:

<v-distpicker province="广东省" city="广州市" area="海珠区"></v-distpicker>

Mobile:

<v-distpicker type="mobile"></v-distpicker>

Demo:

<template>
  <v-distpicker
    :province="select.province"
    :city="select.city"
    :area="select.area"
    @selected="onSelect"
    @change="onChange"
    @province="selectProvince"
    @city="selectCity"
    @area="selectArea"
  ></v-distpicker>
</template>

<script setup>
import VDistpicker from 'v-distpicker'
let select = reactive({ province: '', city: '', area: '' })
function onSelect(data) {
  console.log(data)
}
function onChange(data) {
  console.log(data)
}
function selectProvince({ code, value }) {
  select.province = value
  console.log({ code, value })
}
function selectCity({ code, value }) {
  select.city = value
  console.log({ code, value })
}
function selectArea({ code, value }) {
  select.area = value
  console.log({ code, value })
}
</script>

Props

Name Type Description Default Sample
province String/Number 省级编码或名称 '广东省'/440000/'440000'
city String/Number 市级编码或名称 '广州市'/440100/'440100'
area String/Number 区级编码或名称 '海珠区'/440105/'440105'
placeholder Object 默认显示的值 {province:'省',city:'市',area:'区'}
type String 区分 pc 和 mobile,默认 pc
only-province Boolean 只显示省级选择器 false
hide-area Boolean 隐藏区级 false
disabled Boolean 禁用 false
province-disabled Boolean 禁用省 false
city-disabled Boolean 禁用市 false
area-disabled Boolean 禁用区 false
province-source Object 省数据源 examples/components/data
city-source Object 市级数据源
address-source Object 区级数据源
wrapper String className 'distpicker-address-wrapper'
address-header String className(mobile) 'address-header'
address-container String className(mobile) 'address-container'

Event

Name Type Description Return
province Function 选择省时触发 {code,value}
city Function 选择市时触发 {code,value}
area Function 选择区时触发 {code,value}
selected Function 选择最后一项时触发 {province:{code,value},city:{code,value},area:{code,value} }
change-province Function 省级改变时触发 {code,value}
change-city Function 市级改变时触发 {code,value}
change-area Function 区级改变时触发 {code,value}
change Function 省市区改变时触发 {province:{code,value},city:{code,value},area:{code,value} }

Contributors

Thanks

License

The plugin is open-sourced software licensed under the MIT license.

v-distpicker's People

Contributors

aufree avatar dependabot[bot] avatar iyangsheng avatar jcc avatar kent666a avatar liushuai05 avatar lyh2668 avatar milogenius avatar pikachuking avatar qzhongyi avatar tcsiye avatar wwhwwhwwh avatar xu8117 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

v-distpicker's Issues

selected方法小bug?

从选择好的省市区再重新改为默认的placeholder值时,onselect方法貌似没有触发,这样之前传递进去的值也没有改变,貌似有点难以判断用户是否选择完整省市区了。
我想监听到选回到默认placeholder到行为,应该要怎么优化呢?🤕

组件默认值是否支持地区编码?

存储一般仅仅存储地区编码,映射地区名称有点麻烦

要实现两种:
1、要么组件提供映射工具方法,然后反查出地区名称
2、参数支持地区编码,当然这个对使用者而言最简单

多谢啦~ 看你那边之前有过计划没? 如果没有那我这边抽时间来个 pr ~

AMD方式引入时需要用VDistpicker.default来注册组件

如果在页面上用<script src="">引入v-distpicker.js,在注册为VUE全局组件时会报错,之前提过一个issue:
#29
Failed to mount component: template or render function not defined

而如果将注册为全局组件的代码从:
Vue.component('v-distpicker', VDistpicker)
改为:
Vue.component('v-distpicker', VDistpicker.default)
那么就不会报错了

我不太清楚这个算不算BUG, @jcc

请问下能否支持多个国家

因为项目需求,可能要做别的国家的地区选择,大佬这组件非常符合项目需求,所以有接口或者什么参数可以传入别的国家地区 json 文件,然后就能应用了?

在组件中使用以后更改不了样式啊 ??

image
想做出和上面 input 框一样的效果 但是好像怎么也选不到 v-distpicker啊 ??
用了less 和 stylus
image
image
刚开始还以为是因为 stylus或者是 scoped限制了 后来换了发现还是不行
求助啊 !!!

数据没有双向绑定

Vue版本是2.4.4
是用的在页面上直接引入v-distpicker.js的方式引入组件,发现当选择的地区改变时,数据没有同步到$data上去....

image

Vue实例的$data如下:

image

直辖市下级文字太长了

还请作者别建议我的修改。

例:北京市下面是北京市市辖区,最后串联直来就是北京市北京市市辖区,可以考虑将北京市市辖区缩短成市辖区。

我项目中适当修改了一下这个地方。
html里在三个selecte 的option下面添加了自定义属性:
:data-code="index"

script里面的修改:
data里面增加了:
currentProvinceCode: '',
currentCityCode: '',
currentAreaCode: ''

然后在:
getAreas()方法里将:
his.areas = this.determineValue(this.currentCity, this.placeholders.city)
修改成了:
this.currentCityCode = this.$refs.city.options[this.$refs.city.selectedIndex].getAttribute('data-code');
if(this.currentCity == this.placeholders.city) {
this.areas = []
} else {
this.areas = this.getDistricts(this.currentCityCode)
}

区级同名的情况可能会引发错误

看了源码,是通过中文名找到对应的数字代号,问题是区级同名的情况比较多,会出错:
北京市->市辖区->朝阳区
吉林省->长春市->朝阳区

Reset Button

hello , 我在看官网案例遇到一个问题 在Reset Button那一栏 好像没有看到 code 展示?

country/region/city

Hello,

is there any alternative, to support country/region (not always)/city?

Thanks

县级市选择后报错“Cannot read property 'length' of null"”

当选择湖北省仙桃市时,因为县级市没有区县,会出现如下错误
Error in render: "TypeError: Cannot read property 'length' of null"
好像将下面的代码:
getDistricts(code = DEFAULT_CODE) { return DISTRICTS[code] || null },
改为:
getDistricts(code = DEFAULT_CODE) { return DISTRICTS[code] || [] },
即可,望作者在源码中修复这个错误!

base

image

这个值是什么拿到的

Failed to mount component: template or render function not defined

Vue版本: v2.4.4
VDistpicker使用的是umd的最新版本
Vue.component('v-distpicker', VDistpicker)
使用
<v-distpicker></v-distpicker>

console 报错:

vue.js:482 [Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <VDistpicker>
       <Root>

小建议

  1. 大佬能否完善一下存档,props可以传入的参数希望能够添加说明,ref操作reset也能得到体现。
  2. 能否添加一个header的fixed props样式,好控制header是否顶置,当前得在type mobile的时候生效。
    em...当然我也可以提供pr参与

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.