封装uview2的picker组件(uniapp)

1.源码

复制代码
<template>
  <view>
    <view :class="[getPickerName ? '' : 'is-placeholder']" @click="onShowPicker">{{ getPickerName || placeholder }}</view>
    <u-picker v-if="showStatus" :show="show" :columns="columns" :key-name="keyName" @cancel="show = false" @confirm="onConfirm" />
  </view>
</template>

<script>
export default {
  name: 'HgPicker',
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    columns: {
      type: Array,
      default() {
        return []
      }
    },
    keyName: {
      type: String,
      default: 'label'
    },
    placeholder: {
      type: [String, Number],
      default: '请选择'
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      show: false,
      showStatus: true
    }
  },
  computed: {
    getPickerName() {
      if (this.columns.length) {
        for (let item of this.columns[0]) {
          if (item.id === this.value) {
            return item[this.keyName]
          }
        }
      }
      return ''
    }
  },
  methods: {
    onShowPicker() {
      if (!this.disabled) {
        this.show = true
      }
    },
    onConfirm({ indexs, value, values }) {
      this.$emit('input', value[0].id)
      this.$emit('change', value[0].id)
      this.$emit('changes', indexs[0])
      this.show = false
    },
    reload() {
      this.showStatus = false
      this.$nextTick(() => {
        this.showStatus = true
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.is-placeholder {
    color: #b5afaf;
}
</style>

2.使用方法

<hg-picker v-model="formModel.mode" :columns="capacityModeList" style="flex:1" />

相关推荐
许商4 小时前
【stm32】【printf】
java·前端·stm32
JIngJaneIL4 小时前
智慧物业|物业管理|基于SprinBoot+vue的智慧物业管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·论文·智慧物业管理系统
爬坑的小白4 小时前
vue 2.0 路由跳转时新开tab
前端·javascript·vue.js
爬坑的小白4 小时前
vue x 状态管理
前端·javascript·vue.js
凌览5 小时前
一键去水印|5 款免费小红书解析工具推荐
前端·javascript·后端
有意义5 小时前
栈数据结构全解析:从实现原理到 LeetCode 实战
javascript·算法·编程语言
lichong9515 小时前
鸿蒙 web组件开发
前端·typescript
1024小神5 小时前
在html中使用js动态交换两个元素的位置
前端
鹿鹿鹿鹿isNotDefined5 小时前
逐步手写,实现符合 Promise A+ 规范的 Promise
前端·javascript·算法
一千柯橘5 小时前
Electron - IPC 解决主进程和渲染进程之间的通信
前端