el-select优化TSelect,下拉框只能选末级,启用状态

 封装组件 components/TSelect

<template>
  <el-select
    v-model="selectValue"
    v-loading="loading"
    :placeholder="placeholder"
    filterable
    clearable
    :disabled="disabled"
    @change="changeEvent"
    @clear="clearEvent"
  >
    <el-option
      v-for="item in opitonList"
      :key="item.treedataid"
      :disabled="isLast?((!!item[props['disabled']||'blnisinactive'])||(!item[props['last']||'blnisdetail'])):!!item[props['disabled']||'blnisinactive']"
      :label="props['codes'] ? (item[props['label']||'treedataname']+item.dot+item.pinyin) : (item[props['label']||'treedatacodeandname']+item.dot+item.pinyin)"
      :value="item[props['value']||'treedataid']"
    >
      <span :style="{'float':'left','text-indent':(item.level-1)*1+'em','width':'100%'}" @click="selectEvent(item)">{{ item[props['label']||'treedatacodeandname'] }}</span>
    </el-option>
  </el-select>
</template>
<script>
import { flatten } from '@/base/utils'
export default {
  name: 'TSelect',
  props: {
    props: { // 配置项
      type: Object,
      default() {
        return {
          value: 'treedataid',
          label: 'treedatacodeandname',
          disabled: 'blnisinactive',
          last: 'blnisdetail',
          codes: false
        }
      }
    },
    isLast: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    value: {
      type: [Number, String],
      default: ''
    },
    dotLength: {
      type: Number,
      default: 200
    },
    options: { // 选项列表数据
      type: Array,
      default() {
        return []
      }
    },
    placeholder: {
      type: String,
      default: '请选择'
    },
    isInline: {
      type: Boolean,
      default: false
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      opitonList: [],
      firstLoad: true
    }
  },
  computed: {
    selectValue: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  watch: {
    'options'(newval) {
      if (newval.length >= 0 && this.firstLoad) {
        this.firstLoad = false
        setTimeout(() => {
          this.firstLoad = true
        }, 300)
        this.opitonList = this.flatten(this.options).map(item => { return { pinyin: this.allPinYin(this.props['codes'] ? (item[this.props['label'] || 'treedataname']) : (item[this.props['label'] || 'treedatacodeandname'])), dot: this.getDots(this.dotLength), ...item } })
      }
    }
  },
  created() {
    if (this.isInline && this.options.length > 0 && this.firstLoad) {
      this.firstLoad = false
      this.opitonList = this.flatten(this.options).map(item => { return { pinyin: this.allPinYin(this.props['codes'] ? (item[this.props['label'] || 'treedataname']) : (item[this.props['label'] || 'treedatacodeandname'])), dot: this.getDots(this.dotLength), ...item } })
    }
  },
  methods: {
    flatten,
    // 获取选中的value值
    changeEvent(val) {
      this.$emit('change', val)
    },
    clearEvent() {
      this.$emit('clear')
    },
    // 获取整个条目对象
    selectEvent(item) {
      if (item[this.props['value']] === this.selectValue) {
        return false
      }
      this.$emit('select', item)
    },
    allPinYin(str) {
      if (!window.pinyinUtil) {
        console.log('缺少拼音库依赖,需要在index.html引入pinyinjs库!')
        return ''
      }
      // eslint-disable-next-line
      return pinyinUtil.getFirstLetter(str).toLowerCase() + pinyinUtil.getPinyin(str).replace(/\s/g, '')
    },
    getDots(num = 10) {
      return ' '.repeat(num)
    }
  }
}
</script>
base/utils/index.js
// 扁平化树形json
export function flatten(data, level = 0) {
  if (data.length === 0) {
    level = 0
  }
  ++level
  return data.reduce((arr, { treedataid, treedatacode, treedataname, treedatacodeandname, lngparentid, lngextendid, blnisdetail, path, intlevel, blnisinactive, childList = [] }) =>
    arr.concat([{ treedataid, treedatacode, treedataname, treedatacodeandname, lngparentid, lngextendid, blnisdetail, path, intlevel, blnisinactive, level: level }], flatten(childList, level)), [])
}
引入组件在项目中使用
<el-form-item label="支出项目类别" prop="lngoutitemtypeid">
        <TSelect
          v-model="formInline.lngoutitemtypeid"
          style="width:130px"
          placeholder="请选择"
          :options="outItemTypeList"
          :is-last="true"
        />
      </el-form-item>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值