index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <div class="date-cell">
  3. <van-cell v-if="disabled" :title="title" :label="label" is-link/>
  4. <van-cell v-else :title="title" :label="label" is-link @click="clickItem"/>
  5. <van-popup v-model="showPicker" round lazy-render position="bottom" :close-on-popstate="true" get-container="#app">
  6. <van-picker
  7. v-bind="$attrs"
  8. show-toolbar
  9. :value-key="prop.label"
  10. v-model="selected"
  11. :columns="columns"
  12. @confirm="pickerConfirm"
  13. @cancel="cancelPicker"
  14. />
  15. </van-popup>
  16. </div>
  17. </template>
  18. <script>
  19. import {getDict} from "@/api/toConsult";
  20. export default {
  21. props:{
  22. //禁用
  23. disabled:{
  24. type: Boolean,
  25. default: false,
  26. },
  27. //双向绑定的数据
  28. value:{
  29. type: [String,Number],
  30. default: null,
  31. },
  32. //字典表
  33. dict:{
  34. type: String,
  35. default: null,
  36. },
  37. //标题
  38. title:{
  39. type: String,
  40. default: null,
  41. },
  42. //父组件给的列表数据
  43. dataList:{
  44. type: Array,
  45. default: ()=>[],
  46. },
  47. //自定义字段
  48. prop:{
  49. type: Object,
  50. default: ()=>(
  51. {
  52. label:'dictLabel',
  53. value:'dictValue'
  54. }
  55. ) ,
  56. }
  57. },
  58. data(){
  59. return{
  60. showPicker:false,
  61. selected:null,
  62. label:'',
  63. columns: [],
  64. }
  65. },
  66. created() {
  67. if(!this.dict) return;
  68. this.queryDict();
  69. },
  70. watch:{
  71. dataList:{
  72. handler(val){
  73. if(this.dict) return;
  74. this.columns = this.dataList;
  75. },
  76. immediate: true
  77. },
  78. value:{
  79. handler (val) {
  80. if(!val){
  81. this.selected = null;
  82. this.label = null;
  83. }else{
  84. this.columns.forEach(v=>{
  85. if(v[this.prop.value] === val){
  86. this.selected = v;
  87. this.label = v[this.prop.label];
  88. }
  89. })
  90. // this.selected = val;
  91. // this.label = val.dictLabel;
  92. }
  93. },
  94. immediate: true
  95. }
  96. },
  97. methods:{
  98. queryDict(){
  99. getDict( this.dict ).then(res => {
  100. let { code, data, msg } = res;
  101. if (code == 200) {
  102. this.columns = data;
  103. }
  104. })
  105. },
  106. cancelPicker(){
  107. this.showPicker = false;
  108. },
  109. pickerConfirm(val){
  110. this.selected = val;
  111. this.label = val[this.prop.label];
  112. this.showPicker = false;
  113. this.$emit('change',this.selected[this.prop.value])
  114. },
  115. clickItem(){
  116. this.showPicker = true;
  117. },
  118. },
  119. model:{
  120. prop: 'value',
  121. event: 'change',
  122. }
  123. }
  124. </script>
  125. <style scoped>
  126. </style>