uniapp滚动到顶部及三级联动省市县通用组件

uniapp 滚动到顶部

uni.createSelectorQuery().select('.' + class).boundingClientRect(data=>{//目标节点
  uni.createSelectorQuery().select('.' + class).boundingClientRect((res)=>{//最外层盒子节点     
    uni.pageScrollTo({       
      duration: 0,//过渡时间必须为0,否则运行到手机会报错
      scrollTop: 位置 //滚动到实际距离是元素距离顶部的距离减去最外层盒子的滚动距离(如res.top -data.top)
    })
  }).exec()
}).exec()

uniapp 三级联动省市县通用组件

  1. 使用接口的的数据结构
 [
	{name:'北京',id:11}
 ]    
  1. 在页面中使用
//html
 <selectTree :selectId="form.birthplaceAncestorsTemp"
             @getTreeName="getTreeName"
             style="width:100%"
             :disabled="isEditOrDetail"
             :placeholder="isEditOrDetail?'':'请选择籍贯'"></selectTree>
 //methods
 getTreeName (obj) {
   this.form.birthplaceAncestors = obj.value
}
  1. 完整组件
<template>
  <view class="houseInfo">
    <u-input @click="inputClick"
             v-model="birthplaceAncestorsName"
             :type="disabled?'text':'select'"
             style="width:100%"
             :disabled="disabled"
             :select-open="showselect"
             :placeholder="placeholder" />
    <u-picker v-model="showselect"
              mode="multiSelector"
              ref="multiSelector"
              style="width:100%"
              :range="getRange"
              :defaultSelector="defaultSelector"
              :key="rangeKey"
              @columnchange="columnchange"
              @confirm="confirm6"></u-picker>
  </view>
</template>

<script>
import { getBirthAddress } from '@api/index.js'
export default {
  props: {
    placeholder: {
      type: String,
      default: '请选择户籍地'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    selectId: {
      type: String,
      default: "[]"
    },
  },
  computed: {
    getRange: {
      get () {
        return this.range;
      },
    }
  },
  watch: {
    selectId:{
      handler(nv,ov){
        if(nv){
          let temp = JSON.parse(nv)
          this.currentId = temp
        }
        this.getData({ pid: 0 })
      },
      immediate:true
    }
  },
  data () {
    return {
      showselect: false,
      range: [],
      rangeKey: 0,
      birthplaceAncestorsName: '',
      defaultSelector: [],
      // 省
      provinceList: [],
      // 市
      cityList: [],
      // 县
      countyList: [],
      currentId:[]
    }
  },
  methods: {
    cancelSelected(){
      // this.defaultSelector = []
      // this.birthplaceAncestorsName = ""
    },
    inputClick () {
      if (this.disabled) return
      this.showselect = true
      if(this.isEmpty(this.defaultSelector)){
        this.defaultSelector = [0,0,0]
      }
      this.getData({ pid: 0 })
    },
    isEmpty (obj) {
      if (obj === 0) return true;
      if (Array.isArray(obj)) return !(obj.length > 0);
      return (obj === null || obj === undefined || obj === '' || obj === NaN);
    },
    getData (obj) {
      getBirthAddress(obj).then(res => {
        this.provinceList = res.data;
        if (this.currentId) {
          this.provinceList.some((d, index) => {
            if (d.id == this.currentId[0]) {
              this.defaultSelector[0] = index
            }
          })
        }
        let temp = res.data.map(tel => tel.name)
        this.$set(this.range, '0', temp)
        this.$forceUpdate()
        getBirthAddress({ pid: this.isEmpty(this.currentId) ? this.provinceList[0].id : this.currentId[0] }).then(del => {
          this.cityList = del.data;
          if (this.currentId) {
            this.cityList.some((d, index) => {
              if (d.id == this.currentId[1]) {
                this.defaultSelector[1] = index
              }
            })
          }
          let temp = del.data.map(tel => tel.name)
          this.$set(this.range, '1', temp)
          getBirthAddress({ pid: this.isEmpty(this.currentId) ? this.cityList[0].id : this.currentId[1] }).then(top => {
            this.countyList = top.data;
            if (this.currentId) {
              this.countyList.some((d, index) => {
                if (d.id == this.currentId[2]) {
                  this.defaultSelector[2] = index
                }
              })
              this.confirm6(this.defaultSelector)
            } else {
              this.confirm6(this.defaultSelector)
            }
            let temp = top.data.map(tel => tel.name)
            this.$set(this.range, '2', temp)
          })
        })
      })
    },
    columnchange (e) {
      let column = e.column, index = e.index;
      this.defaultSelector[column] = index;
      switch (column) {
        case 0: //拖动第1列
          getBirthAddress({ pid: this.provinceList[index].id }).then(res => {
            this.cityList = res.data;
            let temp = res.data.map(tel => tel.name)
            this.$set(this.range, '1', temp)
            console.log(this.cityList[0].name)
            getBirthAddress({ pid: this.cityList[0].id }).then(res => {
              this.countyList = res.data;
              let temp = res.data.map(tel => tel.name)
              this.$set(this.range, '2', temp)
            })
          })
          break
        case 1: //拖动第2列
          getBirthAddress({ pid: this.cityList[index].id }).then(res => {
            this.countyList = res.data;
            let temp = res.data.map(tel => tel.name)
            this.$set(this.range, '2', temp)
          })
          break
        case 2: //拖动第3列
          this.$forceUpdate()
          break
      }
    },
    // 户籍地选择事件
    confirm6 (e) {
      let isEmptyFlag = 0
      e.some(d=>{
        isEmptyFlag += 1
      })
      if (!this.isEmpty(e)&&!this.isEmpty(this.countyList)&&isEmptyFlag==3) {
        this.birthplaceAncestors = [Number(this.provinceList[e[0]].id), Number(this.cityList[e[1]].id), Number(this.countyList[e[2]].id)].join()
        this.birthplaceAncestorsName = this.provinceList[e[0]].name + '/' + this.cityList[e[1]].name + '/' + this.countyList[e[2]].name
      } else {
        this.birthplaceAncestors = ''
        this.birthplaceAncestorsName = ''
      }
      this.$emit('getTreeName', { value: this.birthplaceAncestors, name: this.birthplaceAncestorsName })
    }
  }
}
</script>

<style lang="scss">
</style>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp是一款跨平台的开发框架,可以用于开发多种类型的应用程序,包括小程序、H5、App等。对于自定义省市三级联动组件,我们可以参照以下步骤来实现。 首先,我们需要获取省市的数据源,可以从后台接口获取或者使用静态的json文件。将数据源存储在data的变量中。 接下来,我们可以创建一个Picker组件实现三级联动选择。通过使用uni-app的Picker组件,我们可以选择省份,然后根据选择的省份,展示对应的城市和区的选项。同时,需在Picker组件上设置改变省份选项时的change事件,以及城市和区选项的列数、初始值等属性。 在change事件中,我们根据选择的省份,从数据源中获取对应的城市和区选项,并更新到data的变量中。然后,将更新后的data传递给Picker组件,刷新选项。 最后,我们可以在页面上引入并使用自定义的省市三级联动组件。通过v-model指令,将选择的省市的值与页面的数据进行双向绑定,并可以在提交时获取选中的省市的值,进行后续的业务处理。 通过以上步骤,我们可以实现自定义的省市三级联动组件。根据不同的需求,我们还可以对组件进行优化和扩展,例如添加搜索功能、增加默认值等。总之,Uniapp提供了强大的开发能力,可以帮助我们轻松地实现自定义的省市三级联动组件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值