vantUI 2.x 版本,picker组件支持特殊索引的数据(非纯数组)

组件代码

<!-- 该picker组件是用于字典数据第一项索引不是从0开始的特殊数据集 -->
<template>
  <div class="picker-con">
    <div class="picker-box" @click="handleClick">
      <span class="select-tip" v-if="!selectName">请选择</span>
      <span class="select-value" v-else>{{selectName}}</span>
      <div class="arw-box">
        <van-icon name="arrow" />
      </div>
    </div>
    <van-popup v-model="showPicker" round position="bottom">
      <van-picker @confirm="onConfirm" show-toolbar :columns="option" value-key="name" :default-index="defaultIndex" @cancel="showPicker = false" />
    </van-popup>
  </div>
</template>
<script>
export default {
  props: {
    // v-model 默认值
    value: {
      default: ''
    },
    // 下拉选项
    option: {
      default: () => []
    },
    readonly: { // 只读
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      defaultIndex: 0, // 默认选中项的索引
      selectName: '',
      selectValue: '',
      showPicker: false
    }
  },
  model: {
    prop: 'value',
    event: 'getValue'
  },
  watch: {
    value: {
      handler (value) {
        if (!value) {
          this.selectName = ''
          this.defaultIndex = 0
        } else {
          if (this.option.length) {
            // 根据value值 检索出对应项的name
            const findItem = this.option.find(item => {
              return item.value === value
            })
            this.selectName = (findItem && findItem.name) || ''
            // 根据value值 检索出对应项的索引,方便高亮列表项
            const cIndex = this.option.findIndex(item => {
              return item.value === value
            })
            this.defaultIndex = (cIndex > 0) ? cIndex : 0
          }
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    handleClick () {
      if (this.readonly) return // 只读操作,不可选择
      this.showPicker = true
    },
    getValue () {
      this.$emit('getValue', this.selectValue.toString())
    },
    onConfirm (val, index) {
      this.selectName = val.name
      this.selectValue = val.value
      this.showPicker = false
      this.getValue()
    }
  }
}
</script>
<style lang="less" scoped>
.picker-con {
  &:last-child {
    .picker-box {
      margin-bottom: 0 !important;
    }
  }
}
.picker-box {
  background: #F7F9FC;
  box-shadow: 0px 2px 6px 0px rgba(0,70,139,0.06);
  border-radius: 4px;
  padding: 12px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .inline-tit-block {
    width: 50%;
    display: flex;
    flex-direction: column;
    .inline-desc {
      margin-top: 4px;
      font-size: 13px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #666666;
      line-height: 18px;
    }
  }
  .select-block {
    display: flex;
    align-items: center;
  }
}
.select-tip {
  font-size: 15px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #95969F;
  line-height: 21px;
  margin-right: 6px;
}
.select-value {
  font-size: 15px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #333333;
  line-height: 21px;
  margin-right: 6px;
}
.arw-box {
  display: flex;
  align-items: center;
  /deep/ .van-icon.van-icon-arrow{font-size:16px;color:#95969F;}
}
</style>

使用

<Picker v-model="form.e3" :option="e3List" />

注:如果数据来自接口须在组件上进行数据的判断 如下所示:

<Picker v-model="form.e3" v-if="e3List && e3List.length" :option="e3List" />

// 对应数据

const e3List= [
  { value: '1', name: '每周1天及以下' },
  { value: '3', name: '每周2-3天' },
  { value: '5', name: '每周4-6天' },
  { value: '7', name: '每天' }
]