vv-select.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <!-- v-model -->
  3. <!-- 使用组件 -->
  4. <!-- <vvSelect placeholder="请选择所属类别" :list="categorys" label="name" valueKey="id" @change="pickerChange"></vvSelect> -->
  5. <picker :ref="refName" @change="bindPickerChange"
  6. :value="index"
  7. :range="list"
  8. :disabled="disabled"
  9. :range-key="label">
  10. <slot />
  11. <view v-if="isText" class="text-ellipsis">{{checkLabel ? checkLabel : placeholder}}</view>
  12. <view v-else class="vv-input__body text-right">
  13. <view class="vv-input__control">
  14. <text v-if="checkLabel" :class="{'disabled': disabled}">{{checkLabel}}</text>
  15. <text class="vv-input__placeholder" v-else>{{placeholder}}</text>
  16. </view>
  17. <!-- <uni-icons type="arrowright" size="14" color="#808080"></uni-icons> -->
  18. </view>
  19. </picker>
  20. </template>
  21. <script>
  22. /**
  23. * vv-select 选择
  24. * @description picker再封装
  25. * @property {String} valueKey 绑定的key
  26. * @property {String} label 绑定的label
  27. * @property {Array} list 数据
  28. * @property {Boolean} disabled 是否禁用
  29. * @event {Function} change
  30. */
  31. export default {
  32. name: 'vv-select',
  33. props: {
  34. refName: {
  35. type: String,
  36. default: 'vvSelect'
  37. },
  38. value: {
  39. type: [String,Number]
  40. },
  41. placeholder: {
  42. type: String,
  43. default: '请选择'
  44. },
  45. valueKey: {
  46. type: String,
  47. default: 'id'
  48. },
  49. label: {
  50. type: String,
  51. default: 'name'
  52. },
  53. list: {
  54. type: Array,
  55. default () {
  56. return []
  57. }
  58. },
  59. isText: {
  60. type: Boolean,
  61. default: false
  62. },
  63. disabled: {
  64. type: Boolean,
  65. default: false
  66. },
  67. },
  68. data() {
  69. return {
  70. index: 0,
  71. checkLabel: ''
  72. }
  73. },
  74. watch: {
  75. value: {
  76. handler: function(newVal) {
  77. if(this.value) {
  78. this.checkItemByKey(this.value)
  79. } else {
  80. this.$emit('change','')
  81. }
  82. },
  83. // immediate: true,
  84. // deep:true//对象内部的属性监听,也叫深度监听
  85. }
  86. },
  87. created() {
  88. // 首次选中
  89. },
  90. methods: {
  91. checkItemByKey: function(key) {
  92. // var index = this.list.findIndex((item,i)=> {return item[this.valueKey] == key});
  93. // // 首次找索引
  94. // index >= 0 && this.checkItemByIndex(index);
  95. var checkItem = this.list.find((item,i)=> {return item[this.valueKey] == key});
  96. console.log(checkItem)
  97. if(!checkItem) return;
  98. this.checkLabel = checkItem[this.label]; // 值
  99. this.$emit('change',checkItem[this.valueKey])
  100. },
  101. checkItemByIndex: function(index) {
  102. this.index = index;
  103. var checkItem = this.list.find((item,i)=> {return i == this.index});
  104. this.checkLabel = checkItem[this.label]; // 值
  105. // 返回这个值到父级
  106. // this.$emit('change',checkItem[this.valueKey])
  107. this.$emit('input',checkItem[this.valueKey])
  108. // this.$emit('on-change',checkItem[this.valueKey])
  109. },
  110. bindPickerChange: function(e) {
  111. this.checkItemByIndex(e.target.value);
  112. }
  113. }
  114. }
  115. </script>
  116. <style lang="scss" scoped>
  117. // .disabled {
  118. // color: #808080;
  119. // }
  120. </style>