searchSelect.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <uni-popup ref="popup" type="bottom" :mask-click="true" @maskClick="close">
  3. <view class="bottom-selector">
  4. <!-- 标题 -->
  5. <view class="selector-title">{{ title }}</view>
  6. <!-- 搜索框 -->
  7. <view class="search-box">
  8. <uni-icons type="search" size="20" color="#999"></uni-icons>
  9. <input v-model="searchText" placeholder="请输入姓名搜索" class="search-input" placeholder-class="placeholder"
  10. @input="filterData" />
  11. </view>
  12. <!-- 列表数据 -->
  13. <scroll-view scroll-y class="list-container">
  14. <view v-for="(item, index) in filteredData" :key="item.value" class="list-item"
  15. :class="{ selected: innerValue === item.value }" @click="selectItem(item)">
  16. <view class="item-text">{{ item.text }}</view>
  17. <uni-icons v-if="innerValue === item.value" type="checkmarkempty" size="20"
  18. color="#2979ff"></uni-icons>
  19. </view>
  20. <!-- 空状态提示 -->
  21. <view v-if="filteredData.length === 0" class="empty-tip">
  22. <uni-icons type="search" size="40" color="#ccc"></uni-icons>
  23. <text>未找到匹配的使用人</text>
  24. </view>
  25. </scroll-view>
  26. <!-- 底部按钮 -->
  27. <view class="footer">
  28. <button class="btn cancel" @click="close">取消</button>
  29. <!-- <button class="btn confirm" @click="confirm">确定</button> -->
  30. </view>
  31. </view>
  32. </uni-popup>
  33. </template>
  34. <script>
  35. export default {
  36. name: 'BottomSelector',
  37. props: {
  38. // 标题
  39. title: {
  40. type: String,
  41. default: '选择使用人'
  42. },
  43. // 原始数据
  44. dataList: {
  45. type: Array,
  46. default: () => []
  47. },
  48. // 选中的值(v-model)
  49. value: {
  50. type: [String, Number],
  51. default: ''
  52. }
  53. },
  54. data() {
  55. return {
  56. searchText: '',
  57. // 过滤后的数据
  58. filteredData: [],
  59. // 内部选中的值(用于临时存储,确定后更新)
  60. innerValue: this.value,
  61. }
  62. },
  63. watch: {
  64. dataList: {
  65. immediate: true,
  66. handler(newList) {
  67. // 初始化显示所有数据
  68. this.filteredData = [...newList];
  69. }
  70. },
  71. value(newVal) {
  72. this.innerValue = newVal;
  73. }
  74. },
  75. methods: {
  76. // 打开弹窗
  77. open() {
  78. this.$refs.popup.open();
  79. this.innerValue = this.value;
  80. },
  81. clearSearchText(){
  82. this.searchText = ''
  83. },
  84. // 关闭弹窗
  85. close() {
  86. this.$refs.popup.close();
  87. },
  88. // 过滤数据
  89. filterData() {
  90. if (this.searchText.trim() === '') {
  91. this.filteredData = [...this.dataList];
  92. return;
  93. }
  94. const keyword = this.searchText.toLowerCase();
  95. this.filteredData = this.dataList.filter(item =>
  96. item.text.toLowerCase().includes(keyword)
  97. );
  98. },
  99. // 选择项目
  100. selectItem(item) {
  101. this.innerValue = item.value;
  102. // this.selectedItem = item;
  103. this.$emit('input', this.innerValue);
  104. this.$emit('change', item);
  105. this.close();
  106. },
  107. // 确定选择
  108. confirm() {
  109. console.log(this.innerValue,'123')
  110. if (this.innerValue) {
  111. // 更新父组件v-model绑定的值
  112. this.$emit('input', this.innerValue);
  113. }
  114. this.close();
  115. }
  116. }
  117. }
  118. </script>
  119. <style scoped>
  120. .bottom-selector {
  121. width: 100%;
  122. background-color: #ffffff;
  123. border-top-left-radius: 24rpx;
  124. border-top-right-radius: 24rpx;
  125. overflow: hidden;
  126. box-shadow: 0 -4rpx 24rpx rgba(0, 0, 0, 0.08);
  127. padding-bottom: env(safe-area-inset-bottom);
  128. }
  129. /* 标题样式 */
  130. .selector-title {
  131. padding: 36rpx 32rpx 24rpx;
  132. font-size: 36rpx;
  133. font-weight: 600;
  134. color: #333;
  135. text-align: center;
  136. }
  137. /* 搜索框样式 */
  138. .search-box {
  139. display: flex;
  140. align-items: center;
  141. padding: 0 32rpx 24rpx;
  142. margin: 0 32rpx;
  143. border-bottom: 1rpx solid #f0f0f0;
  144. }
  145. .search-input {
  146. flex: 1;
  147. height: 80rpx;
  148. padding: 0 20rpx;
  149. background-color: #f8f8f8;
  150. border-radius: 40rpx;
  151. margin-left: 16rpx;
  152. font-size: 30rpx;
  153. color: #333;
  154. }
  155. .placeholder {
  156. color: #999;
  157. font-size: 30rpx;
  158. }
  159. /* 列表容器 */
  160. .list-container {
  161. max-height: 60vh;
  162. min-height: 45vh;
  163. padding: 0 32rpx;
  164. box-sizing: border-box;
  165. }
  166. /* 列表项样式 */
  167. .list-item {
  168. display: flex;
  169. justify-content: space-between;
  170. align-items: center;
  171. padding: 28rpx 0;
  172. font-size: 32rpx;
  173. color: #333;
  174. border-bottom: 1rpx solid #f5f5f5;
  175. }
  176. .list-item:last-child {
  177. border-bottom: none;
  178. }
  179. .list-item.selected {
  180. color: #2979ff;
  181. font-weight: 500;
  182. }
  183. .list-item:active {
  184. background-color: #f9f9f9;
  185. }
  186. .item-text {
  187. flex: 1;
  188. }
  189. /* 空状态提示 */
  190. .empty-tip {
  191. display: flex;
  192. flex-direction: column;
  193. align-items: center;
  194. justify-content: center;
  195. padding: 100rpx 0;
  196. color: #999;
  197. font-size: 28rpx;
  198. }
  199. .empty-tip text {
  200. margin-top: 20rpx;
  201. }
  202. /* 底部按钮 */
  203. .footer {
  204. display: flex;
  205. padding: 24rpx 32rpx;
  206. background-color: #fff;
  207. border-top: 1rpx solid #f0f0f0;
  208. }
  209. .btn {
  210. flex: 1;
  211. height: 88rpx;
  212. line-height: 88rpx;
  213. border-radius: 44rpx;
  214. font-size: 34rpx;
  215. margin: 0 16rpx;
  216. display: flex;
  217. align-items: center;
  218. justify-content: center;
  219. }
  220. .cancel {
  221. background-color: #f5f5f5;
  222. color: #333;
  223. }
  224. .confirm {
  225. background-color: #2979ff;
  226. color: white;
  227. }
  228. </style>