index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <div class="select-cell">
  3. <van-cell
  4. v-if="disabled"
  5. :required="required"
  6. :border="border"
  7. :title="title"
  8. :value="isRow?label:null"
  9. :label="!isRow?label:null" />
  10. <van-cell
  11. v-else
  12. :required="required"
  13. :title="title"
  14. :border="border"
  15. :value="isRow?label:null"
  16. :label="!isRow?label:null"
  17. is-link
  18. @click="clickItem"/>
  19. <!-- :label="!isRow?label:null" -->
  20. <van-popup v-model="showPicker" round lazy-render position="bottom" :close-on-popstate="true" get-container="#app">
  21. <van-picker
  22. v-bind="$attrs"
  23. show-toolbar
  24. :value-key="prop.label"
  25. v-model="selected"
  26. :columns="columns"
  27. @confirm="pickerConfirm"
  28. confirm-button-text="确定"
  29. @cancel="cancelPicker"
  30. />
  31. </van-popup>
  32. </div>
  33. </template>
  34. <script>
  35. import {getDict} from "@/api/toConsult";
  36. export default {
  37. props:{
  38. //禁用
  39. disabled:{
  40. type: [Boolean,String],
  41. default: false,
  42. },
  43. //双向绑定的数据
  44. value:{
  45. type: [String,Number],
  46. default: null,
  47. },
  48. required:{
  49. type: [Boolean,String],
  50. default: false,
  51. },
  52. border:{
  53. type: [Boolean,String],
  54. default: false,
  55. },
  56. //标题
  57. title:{
  58. type: String,
  59. default: null,
  60. },
  61. //父组件给的列表数据
  62. dataList:{
  63. type: Array,
  64. default: ()=>[],
  65. },
  66. //是否显示全部选项
  67. isAll:{
  68. type: Boolean,
  69. default: false,
  70. },
  71. //单行显示或者多行显示
  72. isRow:{
  73. type: Boolean,
  74. default: false,
  75. },
  76. //自定义字段
  77. prop:{
  78. type: Object,
  79. default: ()=>(
  80. {
  81. label:'dictLabel',
  82. value:'dictValue'
  83. }
  84. ) ,
  85. }
  86. },
  87. data(){
  88. return{
  89. showPicker:false,
  90. label:'无',
  91. }
  92. },
  93. computed:{
  94. columns(){
  95. console.log(this.dataList,'datalist')
  96. if(this.isAll){
  97. let obj = {};
  98. obj[this.prop.label] = '全部';
  99. obj[this.prop.value] = null;
  100. if(this.dataList)
  101. { // 如果 this.dataList 为null undefine 解构会报错
  102. return [obj,...this.dataList];
  103. }
  104. else
  105. {
  106. return [obj]
  107. }
  108. }
  109. return this.dataList;
  110. },
  111. selected(){
  112. if(!this.value) {
  113. if(this.isAll){
  114. this.label = '全部';
  115. return null;
  116. }
  117. this.label = '无';
  118. return null;
  119. }
  120. let val;
  121. console.log(this.label,'label1111');
  122. this.columns.forEach(v=> {
  123. if (v[this.prop.value] === this.value) {
  124. val = v;
  125. this.label = v[this.prop.label];
  126. console.log(this.label,'label2222');
  127. }
  128. });
  129. return val;
  130. },
  131. },
  132. methods:{
  133. cancelPicker(){
  134. this.showPicker = false;
  135. },
  136. pickerConfirm(val){
  137. console.log(val,'val')
  138. if(!val) return;
  139. this.label = val[this.prop.label];
  140. console.log(this.label,'label')
  141. this.showPicker = false;
  142. this.$emit('change',val[this.prop.value]);
  143. this.$emit('itemInfo',val)
  144. },
  145. clickItem(){
  146. this.showPicker = true;
  147. },
  148. },
  149. model:{
  150. prop: 'value',
  151. event: 'change',
  152. }
  153. }
  154. </script>
  155. <style lang="scss" scoped>
  156. .select-cell{
  157. position: relative;
  158. box-sizing: border-box;
  159. width: 100%;
  160. overflow: hidden;
  161. color: #323233;
  162. background-color: #fff;
  163. }
  164. .select-cell::after{
  165. position: absolute;
  166. box-sizing: border-box;
  167. content: ' ';
  168. pointer-events: none;
  169. right: 30px;
  170. bottom: 0;
  171. left: 30px;
  172. border-bottom: 1px solid #ebedf0;
  173. -webkit-transform: scaleY(.5);
  174. transform: scaleY(.5);
  175. }
  176. .van-cell__label{
  177. margin: 0;
  178. white-space: nowrap;
  179. text-overflow: ellipsis;
  180. overflow: hidden;
  181. width: 260px;
  182. }
  183. </style>