search.vue 3.6 KB

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