1.0.0 • Published 5 years ago

vue-picker-oym v1.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

#移动端picker组件。

  • 支持多联级选择,例如s国省市区、分类选择。

##demo

<template>
  <picker
    ref="picker"
    v-if="countryList.length"
    :columns="columnsData"
    :items="pickerData"
    :readonly="disabled"
    v-model="intData"
    @input="inputChange"
    @change="selectChange"></picker>
</template>

<script>
  import picker from '../components/ColumnPicker/index.vue'

  export default {
    name: 'placePicker',
    components: {
      picker
    },
    data () {
      // intData的country === 存在于pickerData的country数组的value
      return {
        pickerData: {
          country:[],
          province: [],
          city: []
        },
        intData: {
          country: '', // country_中国
          province: '',
          city: ''
        }
      }
    },
    props: {
      disabled: Boolean
    },
    watch: {
      intData (v) {
        console.log('intdata', v)
        this.$root.placeSelected = v
      }
    },
    computed: {
      columnsData () {
        return this.$root.columnsData
      },
      countryList () {
        return this.$root.countryList
      },
      placeSelected () {
        return this.$root.placeSelected
      },
      sku () {
        return this.$root.sku
      }
    },
    methods: {
      inputChange (v) {
        this.intData = Object.assign(this.intData, v)
      },
      selectChange(v) {
        const name = v.value && v.value.split('_')[1]
        if(v.column == 'country') {
          this.$root.getPlace(1, name).then(data => {
            this.pickerData.province = data || []
            this.$refs.picker.updateColumn('province', `province_${this.placeSelected.province}`)
            if (!data.length) {
              this.pickerData.city = []
            }
          })
        } else if(v.column == 'province') {
          this.$root.getPlace(2, name).then(data => {
            this.pickerData.city = data || []
            this.$refs.picker.updateColumn('city', `city_${this.placeSelected.city}`)
          })
        }
      }
    },
    mounted () {
      this.$root.$on('place.update', (data) => {
        this.pickerData.country = data
        this.intData.country = this.sku.production_addr1 ? `country_${this.sku.production_addr1}` : ''

        if (this.sku.production_addr1) {
          this.$nextTick(() => {
            this.$refs.picker.select.setValue({
              country: this.intData.country,
              province: `province_${this.sku.production_addr2}`,
              city: `city_${this.sku.production_addr3}`
            })
          })
        }
      })
    }
  }

</script>

Select 属性介绍 props

参数说明类型可选值默认值
columnsArray
items数据Object
v-model初始值Object

Select 事件介绍 event

名称说明参数
change选择项改变{column: '', value: ''}
input选择填入的内容改变{colomnKey: '', value: ''}

Select 方法介绍 api

名称说明参数
updateColumn更新列String 列名
setValue设置选中内容Object {key1: value1, key2: value2}