You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

custom-select-data.vue 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <!-- 字典数据选择器 -->
  2. <template>
  3. <u-input
  4. v-model="state.showName"
  5. :placeholder="placeholder"
  6. :type="disabled ? 'text' : 'select'"
  7. input-align='right'
  8. :disabled="disabled"
  9. @click="choose"/>
  10. <u-select
  11. v-model="showPopup"
  12. :list="props.dataList"
  13. @confirm="confirm"
  14. :valueName="props.valueName"
  15. :labelName="props.labelName"
  16. ></u-select>
  17. </template>
  18. <script setup lang="ts">
  19. import { reactive, watch, onMounted, ref } from 'vue'
  20. const emits = defineEmits(['update:modelValue', 'change']);
  21. const props = defineProps({
  22. modelValue: {
  23. type: [String, Number],
  24. default: false
  25. },
  26. disabled: {
  27. type: Boolean,
  28. default: false
  29. },
  30. dicName: { // 字典名称
  31. type: String,
  32. default: ""
  33. },
  34. valueName: {
  35. type: String,
  36. default: "id"
  37. },
  38. labelName: {
  39. type: String,
  40. default: "name"
  41. },
  42. placeholder: {
  43. type: String,
  44. default: "请选择"
  45. },
  46. dataList: {
  47. type: Array,
  48. default: () => []
  49. }
  50. })
  51. const showPopup = ref(false)
  52. const state = reactive({
  53. showName: '',
  54. })
  55. onMounted(() => {
  56. setValueName()
  57. })
  58. watch([() => props.modelValue, () => props.dataList], ([]) => {
  59. setValueName()
  60. })
  61. const setValueName = () => {
  62. if ((props.modelValue || props.modelValue === 0) && props.dataList && props.dataList.length > 0) {
  63. let obj = props.dataList.find(item => item[props.valueName] === props.modelValue)
  64. console.log(obj)
  65. state.showName = obj ? obj[props.labelName] : ''
  66. }
  67. }
  68. const choose = (e) => {
  69. if (props.disabled) return
  70. showPopup.value = true
  71. }
  72. const confirm = (e) => {
  73. // state.showName = e.label
  74. emits('update:modelValue', e[0].value)
  75. emits('change', e[0].value, e)
  76. }
  77. </script>
  78. <style>
  79. </style>