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

相关推荐
Fly1 小时前
我来提需求,你带着 Codex 做项目:AI 全栈课程开始实战了
前端
集智飞行1 小时前
解决mavros2 ros2版本cpu占用高的问题
java·服务器·前端
w2sfot2 小时前
【JS加密在线】jsjiami.online(让JavaScript 更安全!)
开发语言·javascript·安全
HSunR2 小时前
ruoyi 若依 自定义注解 参数校验
java·前端·数据库
拆房老料3 小时前
ONLYOFFICE也能像Microsoft Word和WPS一样分别设置中西文字体
前端·html·word·开源软件·wps
Solis程序员3 小时前
Stripe 幂等表:把「不确定的重试」变成「查表返回」
前端·安全·bootstrap·php·agent
李纲明3 小时前
WordPress 站点变慢:按 TTFB → 缓存 → 查询 → 前端 分层排查(含配置注意)
前端·缓存·性能优化·wordpress·后端开发
程序人生8884 小时前
PDF 转 Word 版式错乱、表格丢失?DocConverter Web 格式互转引擎的保真实践
前端·人工智能·opencv·机器学习·pdf·word
粥里有勺糖5 小时前
安利一下最近用的桌面“Agent” | T3 Code
前端·github·ai编程
xy34535 小时前
Axure 9.0 动态面板基本操作
前端·ui·html·axure·原型·产品设计