searchSelect.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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: isSelected(item.value) }" @click="selectItem(item)">
  16. <view class="item-text">{{ item.text }}</view>
  17. <!-- 单选模式显示对勾 -->
  18. <uni-icons v-if="!multiple && innerValue === item.value" type="checkmarkempty" size="20"
  19. color="#2979ff"></uni-icons>
  20. <!-- 多选模式显示勾选框 -->
  21. <view v-else-if="multiple" class="checkbox"
  22. :class="{ checked: isSelected(item.value) }">
  23. <uni-icons v-if="isSelected(item.value)" type="checkmarkempty" size="16" color="#fff"></uni-icons>
  24. </view>
  25. </view>
  26. <!-- 空状态提示 -->
  27. <view v-if="filteredData.length === 0" class="empty-tip">
  28. <uni-icons type="search" size="40" color="#ccc"></uni-icons>
  29. <text>未找到匹配的使用人</text>
  30. </view>
  31. </scroll-view>
  32. <!-- 底部按钮 -->
  33. <view class="footer">
  34. <button class="btn cancel" @click="close">取消</button>
  35. <!-- 多选模式显示确定按钮 -->
  36. <button v-if="multiple" class="btn confirm" @click="confirm">确定</button>
  37. </view>
  38. </view>
  39. </uni-popup>
  40. </template>
  41. <script>
  42. export default {
  43. name: 'BottomSelector',
  44. props: {
  45. // 标题
  46. title: {
  47. type: String,
  48. default: '选择使用人'
  49. },
  50. // 原始数据
  51. dataList: {
  52. type: Array,
  53. default: () => []
  54. },
  55. // 选中的值(v-model)- 单选为 String/Number,多选为 Array
  56. value: {
  57. type: [String, Number, Array],
  58. default: ''
  59. },
  60. // 是否为多选模式
  61. multiple: {
  62. type: Boolean,
  63. default: false
  64. }
  65. },
  66. data() {
  67. return {
  68. searchText: '',
  69. // 过滤后的数据
  70. filteredData: [],
  71. // 内部选中的值(用于临时存储,确定后更新)
  72. innerValue: this.value,
  73. }
  74. },
  75. watch: {
  76. dataList: {
  77. immediate: true,
  78. handler(newList) {
  79. // 初始化显示所有数据
  80. this.filteredData = [...newList];
  81. }
  82. },
  83. value(newVal) {
  84. this.innerValue = newVal;
  85. }
  86. },
  87. methods: {
  88. // 打开弹窗
  89. open() {
  90. this.$refs.popup.open();
  91. // 深拷贝,避免直接修改父组件的值
  92. this.innerValue = this.multiple ? [...(this.value || [])] : this.value;
  93. },
  94. clearSearchText(){
  95. this.searchText = ''
  96. },
  97. // 关闭弹窗
  98. close() {
  99. this.$refs.popup.close();
  100. },
  101. // 过滤数据
  102. filterData() {
  103. if (this.searchText.trim() === '') {
  104. this.filteredData = [...this.dataList];
  105. return;
  106. }
  107. const keyword = this.searchText.toLowerCase();
  108. this.filteredData = this.dataList.filter(item =>
  109. item.text.toLowerCase().includes(keyword)
  110. );
  111. },
  112. // 判断是否选中
  113. isSelected(value) {
  114. if (this.multiple) {
  115. return this.innerValue.includes(value);
  116. }
  117. return this.innerValue === value;
  118. },
  119. // 选择项目
  120. selectItem(item) {
  121. if (this.multiple) {
  122. // 多选模式
  123. const index = this.innerValue.indexOf(item.value);
  124. if (index > -1) {
  125. // 已选中则取消
  126. this.innerValue.splice(index, 1);
  127. } else {
  128. // 未选中则添加
  129. this.innerValue.push(item.value);
  130. }
  131. // 触发change事件,但不关闭弹窗
  132. this.$emit('change', this.getSelectedItems());
  133. } else {
  134. // 单选模式
  135. this.innerValue = item.value;
  136. this.$emit('input', this.innerValue);
  137. this.$emit('change', item);
  138. this.close();
  139. }
  140. },
  141. // 获取选中的完整数据项(多选模式)
  142. getSelectedItems() {
  143. if (!this.multiple) {
  144. return this.dataList.find(item => item.value === this.innerValue);
  145. }
  146. return this.dataList.filter(item => this.innerValue.includes(item.value));
  147. },
  148. // 确定选择(多选模式)
  149. confirm() {
  150. if (this.multiple) {
  151. // 更新父组件v-model绑定的值
  152. this.$emit('input', [...this.innerValue]);
  153. this.$emit('confirm', this.getSelectedItems());
  154. }
  155. this.close();
  156. }
  157. }
  158. }
  159. </script>
  160. <style scoped>
  161. .bottom-selector {
  162. width: 100%;
  163. background-color: #ffffff;
  164. border-top-left-radius: 24rpx;
  165. border-top-right-radius: 24rpx;
  166. overflow: hidden;
  167. box-shadow: 0 -4rpx 24rpx rgba(0, 0, 0, 0.08);
  168. padding-bottom: env(safe-area-inset-bottom);
  169. }
  170. /* 标题样式 */
  171. .selector-title {
  172. padding: 36rpx 32rpx 24rpx;
  173. font-size: 36rpx;
  174. font-weight: 600;
  175. color: #333;
  176. text-align: center;
  177. }
  178. /* 搜索框样式 */
  179. .search-box {
  180. display: flex;
  181. align-items: center;
  182. padding: 0 32rpx 24rpx;
  183. margin: 0 32rpx;
  184. border-bottom: 1rpx solid #f0f0f0;
  185. }
  186. .search-input {
  187. flex: 1;
  188. height: 80rpx;
  189. padding: 0 20rpx;
  190. background-color: #f8f8f8;
  191. border-radius: 40rpx;
  192. margin-left: 16rpx;
  193. font-size: 30rpx;
  194. color: #333;
  195. }
  196. .placeholder {
  197. color: #999;
  198. font-size: 30rpx;
  199. }
  200. /* 列表容器 */
  201. .list-container {
  202. max-height: 60vh;
  203. min-height: 45vh;
  204. padding: 0 32rpx;
  205. box-sizing: border-box;
  206. }
  207. /* 列表项样式 */
  208. .list-item {
  209. display: flex;
  210. justify-content: space-between;
  211. align-items: center;
  212. padding: 28rpx 0;
  213. font-size: 32rpx;
  214. color: #333;
  215. border-bottom: 1rpx solid #f5f5f5;
  216. }
  217. .list-item:last-child {
  218. border-bottom: none;
  219. }
  220. .list-item.selected {
  221. color: #2979ff;
  222. font-weight: 500;
  223. }
  224. .list-item:active {
  225. background-color: #f9f9f9;
  226. }
  227. .item-text {
  228. flex: 1;
  229. }
  230. /* 勾选框样式(多选模式) */
  231. .checkbox {
  232. width: 40rpx;
  233. height: 40rpx;
  234. border-radius: 50%;
  235. border: 2rpx solid #ccc;
  236. display: flex;
  237. align-items: center;
  238. justify-content: center;
  239. transition: all 0.2s;
  240. }
  241. .checkbox.checked {
  242. background-color: #2979ff;
  243. border-color: #2979ff;
  244. }
  245. /* 空状态提示 */
  246. .empty-tip {
  247. display: flex;
  248. flex-direction: column;
  249. align-items: center;
  250. justify-content: center;
  251. padding: 100rpx 0;
  252. color: #999;
  253. font-size: 28rpx;
  254. }
  255. .empty-tip text {
  256. margin-top: 20rpx;
  257. }
  258. /* 底部按钮 */
  259. .footer {
  260. display: flex;
  261. padding: 24rpx 32rpx;
  262. background-color: #fff;
  263. border-top: 1rpx solid #f0f0f0;
  264. }
  265. .btn {
  266. flex: 1;
  267. height: 88rpx;
  268. line-height: 88rpx;
  269. border-radius: 44rpx;
  270. font-size: 34rpx;
  271. margin: 0 16rpx;
  272. display: flex;
  273. align-items: center;
  274. justify-content: center;
  275. }
  276. .cancel {
  277. background-color: #f5f5f5;
  278. color: #333;
  279. }
  280. .confirm {
  281. background-color: #2979ff;
  282. color: white;
  283. }
  284. </style>