You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <!-- 有搜索功能的picker -->
  2. <template>
  3. <view class="date-background" v-show="state.flag">
  4. <view class='date-gray-background' @click="cancelPicker"></view>
  5. <view class='date-container'>
  6. <view class="transparent">
  7. <view class='date-confirm'>
  8. <view @click="cancelPicker" class="pickerCancel">取消</view>
  9. <u-input v-model="state.keywords" placeholder="请输入搜索关键词" :border="true" :auto-height="false"
  10. :adjust-position="false" style="flex:1;" @input='searchChange' height="65"></u-input>
  11. <view @click="confirm" class="pickerConfirm">确定</view>
  12. </view>
  13. <picker-view indicator-class="indicator" :value="state.setValue" @change="bindChange"
  14. indicator-style="height: 100rpx;" mask-style="height:900rpx;"
  15. style="width: 100%; height: 80%;position:absolute;bottom:0rpx;text-align:center;background:white">
  16. <picker-view-column class="pickViewColumn">
  17. <view v-for="item in state.dataList" :key="item" style="line-height: 68rpx">{{item}}
  18. </view>
  19. </picker-view-column>
  20. </picker-view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script lang="ts" setup>
  26. import { onMounted, reactive, watch } from "vue";
  27. const emits = defineEmits(['update:modelValue', 'hidePicker'])
  28. const props = defineProps({
  29. dataSource: {
  30. type: Array,
  31. default: null
  32. },
  33. value: {
  34. type: Array,
  35. default() {
  36. return [0]
  37. }
  38. },
  39. visible: {
  40. type: Boolean,
  41. default: false
  42. }
  43. })
  44. onMounted(() => {
  45. state.flag = props.visible;
  46. state.dataList = props.dataSource;
  47. })
  48. const state = reactive({
  49. flag: false, //是否显示
  50. keywords: '', // 搜索值
  51. setValue: [0], // picker 选择值
  52. dataList: [],
  53. selectValue: '', //选择的值
  54. })
  55. watch(() => props.dataSource, (newVal, oldVal) => {
  56. state.dataList = newVal;
  57. });
  58. watch(() => props.visible, (newVal, oldVal) => {
  59. state.flag = newVal;
  60. });
  61. function bindChange(e) {
  62. let value = e.detail.value.toString();
  63. state.dataList.forEach((item, index) => {
  64. if (value == index) {
  65. state.selectValue = item as string;
  66. }
  67. });
  68. }
  69. function confirm(e) {
  70. state.flag = !state.flag;
  71. if (!state.selectValue) {
  72. state.selectValue = state.dataList[0];
  73. }
  74. emits('update:modelValue', state.selectValue);
  75. emits('hidePicker', false);
  76. }
  77. // 搜索查询
  78. function searchChange(val) {
  79. state.setValue = [0];
  80. state.selectValue = '';
  81. state.dataList = props.dataSource.filter((item) => (item as string).indexOf(val) > -1);
  82. }
  83. function showDatePicker() {
  84. state.flag = !state.flag
  85. }
  86. const cancelPicker = () => {
  87. state.flag = !state.flag
  88. emits('hidePicker', false);
  89. }
  90. defineExpose({
  91. showDatePicker
  92. })
  93. </script>
  94. <style lang='scss' scoped>
  95. .date-background {
  96. position: fixed;
  97. left: 0;
  98. top: 0;
  99. bottom: 0;
  100. width: 100%;
  101. height: 100%;
  102. z-index: 1000;
  103. }
  104. .date-gray-background {
  105. position: absolute;
  106. width: 100%;
  107. top: 0rpx;
  108. background: rgba(0, 0, 0, .5);
  109. height: 100vh;
  110. }
  111. .chaxunjieguo {
  112. width: 100%;
  113. height: 500rpx;
  114. overflow: scroll;
  115. color: black;
  116. }
  117. .chaxunjieguo text {
  118. display: block;
  119. text-align: center;
  120. padding-bottom: 10rpx;
  121. }
  122. .date-container {
  123. position: absolute;
  124. width: 100%;
  125. height: 600rpx;
  126. overflow: hidden;
  127. background: #fff;
  128. bottom: 0;
  129. top: 0;
  130. left: 0;
  131. right: 0;
  132. margin: auto;
  133. }
  134. .date-confirm {
  135. display: flex;
  136. justify-content: space-between;
  137. align-items: center;
  138. padding: 20rpx;
  139. font-size: 34rpx;
  140. }
  141. .pickViewColumn {
  142. height: 600rpx;
  143. }
  144. .indicator {
  145. height: 80rpx;
  146. }
  147. .pickerCancel {
  148. font-size: 30rpx;
  149. color: #777;
  150. margin-right: 30rpx;
  151. }
  152. .pickerConfirm {
  153. font-size: 30rpx;
  154. color: #4C83D6;
  155. margin-left: 30rpx;
  156. }
  157. </style>