searchSelect.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. // selectedItem: null
  63. }
  64. },
  65. watch: {
  66. dataList: {
  67. immediate: true,
  68. handler(newList) {
  69. // 初始化显示所有数据
  70. this.filteredData = [...newList];
  71. }
  72. },
  73. value(newVal) {
  74. this.innerValue = newVal;
  75. }
  76. },
  77. methods: {
  78. // 打开弹窗
  79. open() {
  80. this.$refs.popup.open();
  81. // 打开时重置搜索状态
  82. // this.searchText = '';
  83. // this.filteredData = [...this.dataList];
  84. // 同步当前值到innerValue(因为可能外部修改了value)
  85. this.innerValue = this.value;
  86. // this.selectedItem = null;
  87. },
  88. // 关闭弹窗
  89. close() {
  90. this.$refs.popup.close();
  91. // this.$emit('close', this.selectedItem);
  92. },
  93. // 过滤数据
  94. filterData() {
  95. if (this.searchText.trim() === '') {
  96. this.filteredData = [...this.dataList];
  97. return;
  98. }
  99. const keyword = this.searchText.toLowerCase();
  100. this.filteredData = this.dataList.filter(item =>
  101. item.text.toLowerCase().includes(keyword)
  102. );
  103. },
  104. // 选择项目
  105. selectItem(item) {
  106. this.innerValue = item.value;
  107. // this.selectedItem = item;
  108. this.$emit('input', this.innerValue);
  109. this.$emit('change', item);
  110. this.close();
  111. },
  112. // 确定选择
  113. confirm() {
  114. console.log(this.innerValue,'123')
  115. if (this.innerValue) {
  116. // 更新父组件v-model绑定的值
  117. this.$emit('input', this.innerValue);
  118. }
  119. this.close();
  120. }
  121. }
  122. }
  123. </script>
  124. <style scoped>
  125. .bottom-selector {
  126. width: 100%;
  127. background-color: #ffffff;
  128. border-top-left-radius: 24rpx;
  129. border-top-right-radius: 24rpx;
  130. overflow: hidden;
  131. box-shadow: 0 -4rpx 24rpx rgba(0, 0, 0, 0.08);
  132. padding-bottom: env(safe-area-inset-bottom);
  133. }
  134. /* 标题样式 */
  135. .selector-title {
  136. padding: 36rpx 32rpx 24rpx;
  137. font-size: 36rpx;
  138. font-weight: 600;
  139. color: #333;
  140. text-align: center;
  141. }
  142. /* 搜索框样式 */
  143. .search-box {
  144. display: flex;
  145. align-items: center;
  146. padding: 0 32rpx 24rpx;
  147. margin: 0 32rpx;
  148. border-bottom: 1rpx solid #f0f0f0;
  149. }
  150. .search-input {
  151. flex: 1;
  152. height: 80rpx;
  153. padding: 0 20rpx;
  154. background-color: #f8f8f8;
  155. border-radius: 40rpx;
  156. margin-left: 16rpx;
  157. font-size: 30rpx;
  158. color: #333;
  159. }
  160. .placeholder {
  161. color: #999;
  162. font-size: 30rpx;
  163. }
  164. /* 列表容器 */
  165. .list-container {
  166. max-height: 60vh;
  167. min-height: 45vh;
  168. padding: 0 32rpx;
  169. box-sizing: border-box;
  170. }
  171. /* 列表项样式 */
  172. .list-item {
  173. display: flex;
  174. justify-content: space-between;
  175. align-items: center;
  176. padding: 28rpx 0;
  177. font-size: 32rpx;
  178. color: #333;
  179. border-bottom: 1rpx solid #f5f5f5;
  180. }
  181. .list-item:last-child {
  182. border-bottom: none;
  183. }
  184. .list-item.selected {
  185. color: #2979ff;
  186. font-weight: 500;
  187. }
  188. .list-item:active {
  189. background-color: #f9f9f9;
  190. }
  191. .item-text {
  192. flex: 1;
  193. }
  194. /* 空状态提示 */
  195. .empty-tip {
  196. display: flex;
  197. flex-direction: column;
  198. align-items: center;
  199. justify-content: center;
  200. padding: 100rpx 0;
  201. color: #999;
  202. font-size: 28rpx;
  203. }
  204. .empty-tip text {
  205. margin-top: 20rpx;
  206. }
  207. /* 底部按钮 */
  208. .footer {
  209. display: flex;
  210. padding: 24rpx 32rpx;
  211. background-color: #fff;
  212. border-top: 1rpx solid #f0f0f0;
  213. }
  214. .btn {
  215. flex: 1;
  216. height: 88rpx;
  217. line-height: 88rpx;
  218. border-radius: 44rpx;
  219. font-size: 34rpx;
  220. margin: 0 16rpx;
  221. display: flex;
  222. align-items: center;
  223. justify-content: center;
  224. }
  225. .cancel {
  226. background-color: #f5f5f5;
  227. color: #333;
  228. }
  229. .confirm {
  230. background-color: #2979ff;
  231. color: white;
  232. }
  233. </style>