index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <div class="date-cell">
  3. <van-cell v-if="disabled" :required="required" :title="title" :label="label"/>
  4. <van-cell v-else :required="required" :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. confirm-button-text="确定"
  14. @cancel="cancelPicker"
  15. />
  16. </van-popup>
  17. </div>
  18. </template>
  19. <script>
  20. import {getDict} from "@/api/toConsult";
  21. export default {
  22. props:{
  23. //禁用
  24. disabled:{
  25. type: [Boolean,String],
  26. default: false,
  27. },
  28. //双向绑定的数据
  29. value:{
  30. type: [String,Number],
  31. default: null,
  32. },
  33. required:{
  34. type: [Boolean,String],
  35. default: false,
  36. },
  37. border:{
  38. type: [Boolean,String],
  39. default: true,
  40. },
  41. //标题
  42. title:{
  43. type: String,
  44. default: null,
  45. },
  46. //父组件给的列表数据
  47. dataList:{
  48. type: Array,
  49. default: ()=>[],
  50. },
  51. isAll:{
  52. type: Boolean,
  53. default: false,
  54. },
  55. //自定义字段
  56. prop:{
  57. type: Object,
  58. default: ()=>(
  59. {
  60. label:'dictLabel',
  61. value:'dictValue'
  62. }
  63. ) ,
  64. }
  65. },
  66. data(){
  67. return{
  68. showPicker:false,
  69. label:'无',
  70. }
  71. },
  72. computed:{
  73. columns(){
  74. console.log(this.dataList,'datalist')
  75. if(this.isAll){
  76. let obj = {};
  77. obj[this.prop.label] = '全部';
  78. obj[this.prop.value] = null;
  79. return [obj,...this.dataList];
  80. }
  81. return this.dataList;
  82. },
  83. selected(){
  84. if(!this.value) {
  85. this.label = '无';
  86. return null;
  87. }
  88. let val;
  89. this.columns.forEach(v=> {
  90. if (v[this.prop.value] === this.value) {
  91. val = v;
  92. this.label = v[this.prop.label];
  93. }
  94. });
  95. return val;
  96. },
  97. },
  98. methods:{
  99. cancelPicker(){
  100. this.showPicker = false;
  101. },
  102. pickerConfirm(val){
  103. if(!val) return;
  104. this.label = val[this.prop.label];
  105. this.showPicker = false;
  106. this.$emit('change',val[this.prop.value])
  107. },
  108. clickItem(){
  109. this.showPicker = true;
  110. },
  111. },
  112. model:{
  113. prop: 'value',
  114. event: 'change',
  115. }
  116. }
  117. </script>
  118. <style lang="scss" scoped>
  119. .van-cell__label{
  120. margin: 0;
  121. }
  122. </style>