index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. //标题
  38. title:{
  39. type: String,
  40. default: null,
  41. },
  42. //父组件给的列表数据
  43. dataList:{
  44. type: Array,
  45. default: ()=>[],
  46. },
  47. isAll:{
  48. type: Boolean,
  49. default: false,
  50. },
  51. //自定义字段
  52. prop:{
  53. type: Object,
  54. default: ()=>(
  55. {
  56. label:'dictLabel',
  57. value:'dictValue'
  58. }
  59. ) ,
  60. }
  61. },
  62. data(){
  63. return{
  64. showPicker:false,
  65. label:'无',
  66. }
  67. },
  68. computed:{
  69. columns(){
  70. if(this.isAll){
  71. let obj = {};
  72. obj[this.prop.label] = '全部';
  73. obj[this.prop.value] = null;
  74. return [obj,...this.dataList];
  75. }
  76. return this.dataList;
  77. },
  78. selected(){
  79. let val;
  80. this.columns.forEach(v=> {
  81. if (v[this.prop.value] === this.value) {
  82. val = v;
  83. this.label = v[this.prop.label];
  84. }
  85. });
  86. return val;
  87. },
  88. },
  89. methods:{
  90. cancelPicker(){
  91. this.showPicker = false;
  92. },
  93. pickerConfirm(val){
  94. this.label = val[this.prop.label];
  95. this.showPicker = false;
  96. this.$emit('change',val[this.prop.value])
  97. },
  98. clickItem(){
  99. this.showPicker = true;
  100. },
  101. },
  102. model:{
  103. prop: 'value',
  104. event: 'change',
  105. }
  106. }
  107. </script>
  108. <style lang="scss" scoped>
  109. .van-cell__label{
  110. margin: 0;
  111. }
  112. </style>