封装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" />

相关推荐
西洼工作室18 分钟前
UniApp云开发笔记
前端·笔记·uni-app
zhangxingchao24 分钟前
AI应用开发一: AI 编程、大模型调用和 Agent
前端·人工智能·后端
ljt272496066143 分钟前
Vue笔记(三)--用户交互
javascript·vue.js·笔记
颖火虫盟主1 小时前
Hello World MCP Server 实现总结
java·前端·python
Martin -Tang1 小时前
uniapp 实现录音操作,长按录音,放开取消
前端·javascript·vue.js·uni-app·css3·录音
Full Stack Developme1 小时前
Spring-web 解析
java·前端·spring
humcomm1 小时前
AI编程对前端架构师技能的具体要求有哪些变化
前端·系统架构·ai编程
ZC跨境爬虫1 小时前
跟着 MDN 学 HTML day_58:(构建行星数据表——HTML表格高级实战指南)
前端·javascript·ui·html·音视频
kyriewen1 小时前
用户打开飞行模式都能打开你的网站?Service Worker 做离线缓存,PWA 实战
前端·javascript·面试
我是汪先生1 小时前
学习 day8 memory
前端