m-multiple-picker.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <template>
  2. <view class="m-mul-picker" v-if="showMulPicker">
  3. <uni-transition
  4. :mode-class="['fade']"
  5. :styles="maskClass"
  6. :duration="duration"
  7. :show="showTrans"
  8. @click="maskTap"
  9. />
  10. <uni-transition :mode-class="['slide-bottom']" :styles="transClass" :duration="duration" :show="showTrans">
  11. <view class="m-mul-body">
  12. <view class="m-body-top">
  13. <view class="m-body-top-btn" @click="mulCancel">
  14. <text
  15. class="m--cancel-text"
  16. :style="{ color: cancelBtn.color, 'font-size': cancelBtn.fontSize }"
  17. >
  18. {{ cancelBtn.text }}
  19. </text>
  20. </view>
  21. <view class="m-body-title">
  22. <text
  23. class="m--title-text"
  24. :style="{ color: pickerTitle.color, 'font-size': pickerTitle.fontSize }"
  25. >
  26. {{ pickerTitle.text }}
  27. </text>
  28. </view>
  29. <view class="m-body-top-btn" @click="mulConfirm">
  30. <text
  31. class="m--confirm-text"
  32. :style="{ color: confirmBtn.color, 'font-size': confirmBtn.fontSize }"
  33. >
  34. {{ confirmBtn.text }}
  35. </text>
  36. </view>
  37. </view>
  38. <view class="m-body-content">
  39. <view class="m-search-content" v-if="openSearch">
  40. <view class="m-search-input">
  41. <uni-icons type="search" :size="searchIconSize" :color="searchIconColor"></uni-icons>
  42. <input
  43. class="m-uni-input"
  44. :style="'font-size:' + searchFontsize + ';color:' + searchColor"
  45. :placeholder="searchPlaceholder"
  46. confirm-type="search"
  47. v-model="searchVal"
  48. adjust-position
  49. @confirm="searchConfirm"
  50. />
  51. <uni-icons
  52. v-if="searchVal != ''"
  53. type="clear"
  54. :size="searchIconSize"
  55. :color="searchIconColor"
  56. @click="clearSearchVal"
  57. ></uni-icons>
  58. </view>
  59. </view>
  60. <scroll-view class="m-body-scroll" scroll-y="true">
  61. <template v-if="dataList.length">
  62. <template v-for="(item, index) in dataList">
  63. <view
  64. class="m--scroll-item"
  65. :key="index"
  66. :class="{ 'item-active': selectedVals.includes(item[mapKeys.value]) }"
  67. @click="selectedItem(item)"
  68. v-if="filterSearch(item)"
  69. >
  70. <text class="m-s-item-text" :style="{ 'text-align': textAlign }">
  71. {{ item[mapKeys.label] }}
  72. </text>
  73. <view class="m--selected-icon" v-if="selectedVals.includes(item[mapKeys.value])">
  74. <uni-icons type="checkmarkempty" color="#2F77F1" size="18"></uni-icons>
  75. </view>
  76. </view>
  77. </template>
  78. </template>
  79. <template v-else>
  80. <view class="mul-picker-empty">
  81. <text class="empty-text">{{ emptyText }}</text>
  82. </view>
  83. </template>
  84. </scroll-view>
  85. </view>
  86. </view>
  87. </uni-transition>
  88. </view>
  89. </template>
  90. <script>
  91. export default {
  92. name: 'm-multiple-picker',
  93. props: {
  94. // 取消按钮自定义
  95. cancelBtn: {
  96. type: Object,
  97. default: () => ({
  98. text: '取消',
  99. color: '#666666',
  100. fontSize: '32rpx'
  101. })
  102. },
  103. // 确定按钮自定义
  104. confirmBtn: {
  105. type: Object,
  106. default: () => ({
  107. text: '确定',
  108. color: '#2f77f1',
  109. fontSize: '32rpx'
  110. })
  111. },
  112. // picker标题
  113. pickerTitle: {
  114. type: Object,
  115. default: () => ({
  116. text: '请选择',
  117. color: '#101010',
  118. fontSize: '32rpx'
  119. })
  120. },
  121. // key和value字段映射
  122. mapKeys: {
  123. type: [Object],
  124. default: () => ({
  125. label: 'label',
  126. value: 'value'
  127. })
  128. },
  129. // 是否允许点击mask关闭
  130. maskClick: {
  131. type: Boolean,
  132. default: true
  133. },
  134. // 数据
  135. dataList: {
  136. type: Array,
  137. default: () => []
  138. },
  139. // 默认选中
  140. checkedVals: {
  141. type: Array,
  142. default: () => []
  143. },
  144. // 无数据显示文案
  145. emptyText: {
  146. type: String,
  147. default: '暂无数据'
  148. },
  149. // 设置选项的对齐方式 left center right
  150. textAlign: {
  151. type: String,
  152. default: 'center'
  153. },
  154. // 设置picker 单选single 多选multiple,默认多选
  155. selectType: {
  156. type: String,
  157. default: 'multiple'
  158. },
  159. //S 搜索框设置
  160. // 是否开启搜索框 默认关闭
  161. openSearch: {
  162. type: Boolean,
  163. default: false
  164. },
  165. // 图标大小
  166. searchIconSize: {
  167. type: String,
  168. default: '16'
  169. },
  170. // 图标颜色
  171. searchIconColor: {
  172. type: String,
  173. default: '#666666'
  174. },
  175. // input输入字体大小
  176. searchFontsize: {
  177. type: String,
  178. default: '24rpx'
  179. },
  180. // input输入字体颜色
  181. searchColor: {
  182. type: String,
  183. default: '#333333'
  184. },
  185. // 占位符
  186. searchPlaceholder: {
  187. type: String,
  188. default: '请输入'
  189. }
  190. //E 搜索框设置
  191. },
  192. data() {
  193. return {
  194. duration: 300,
  195. maskClass: {
  196. position: 'fixed',
  197. bottom: 0,
  198. top: 0,
  199. left: 0,
  200. right: 0,
  201. backgroundColor: 'rgba(0, 0, 0, 0.3)'
  202. },
  203. transClass: {
  204. position: 'fixed',
  205. left: 0,
  206. right: 0,
  207. bottom: 0
  208. },
  209. showMulPicker: false,
  210. showTrans: false,
  211. selectedVals: [],
  212. searchVal: ''
  213. };
  214. },
  215. methods: {
  216. selectedItem(item) {
  217. if (this.selectType == 'multiple') {
  218. this.selectedVals.indexOf(item[this.mapKeys.value]) > -1
  219. ? this.selectedVals.splice(this.selectedVals.indexOf(item[this.mapKeys.value]), 1)
  220. : this.selectedVals.push(item[this.mapKeys.value]);
  221. } else {
  222. // 单选,可以取消选中
  223. if (this.selectedVals.length > 0) {
  224. if (this.selectedVals.indexOf(item[this.mapKeys.value]) > -1) {
  225. this.selectedVals.splice(this.selectedVals.indexOf(item[this.mapKeys.value]), 1);
  226. } else {
  227. this.selectedVals = [];
  228. this.selectedVals.push(item[this.mapKeys.value]);
  229. }
  230. } else {
  231. this.selectedVals.push(item[this.mapKeys.value]);
  232. }
  233. }
  234. },
  235. // 打开picker
  236. openMulpicker() {
  237. this.selectedVals = JSON.parse(JSON.stringify(this.checkedVals));
  238. this.showMulPicker = true;
  239. this.$nextTick(() => {
  240. clearTimeout(this.timer);
  241. this.timer = setTimeout(() => {
  242. this.showTrans = true;
  243. }, 50);
  244. });
  245. this.$emit('change', {
  246. show: true,
  247. selectedVals: this.selectedVals
  248. });
  249. },
  250. // 关闭picker
  251. closeMulpicker(type) {
  252. this.showTrans = false;
  253. this.$nextTick(() => {
  254. clearTimeout(this.timer);
  255. this.timer = setTimeout(() => {
  256. this.$emit('change', {
  257. show: false,
  258. selectedVals: this.selectedVals
  259. });
  260. this.showMulPicker = false;
  261. }, 300);
  262. });
  263. },
  264. mulCancel() {
  265. this.closeMulpicker();
  266. },
  267. mulConfirm() {
  268. let selectedObj = this.dataList.filter((item) => {
  269. return this.selectedVals.includes(item[this.mapKeys.value]);
  270. });
  271. this.$nextTick(() => {
  272. this.$emit('mulConfirm', {
  273. value: this.selectedVals,
  274. objValue: selectedObj
  275. });
  276. });
  277. this.closeMulpicker();
  278. },
  279. maskTap() {
  280. if (!this.maskClick) return;
  281. this.closeMulpicker();
  282. },
  283. // 搜索筛选关键字
  284. searchConfirm(e) {
  285. let { value } = e.detail;
  286. this.searchVal = value;
  287. },
  288. clearSearchVal() {
  289. this.searchVal = '';
  290. },
  291. // 筛选 显示
  292. filterSearch(item) {
  293. if (this.searchVal) {
  294. return item[this.mapKeys.label].includes(this.searchVal);
  295. } else {
  296. return true;
  297. }
  298. }
  299. }
  300. };
  301. </script>
  302. <style scoped>
  303. .m-mul-picker {
  304. position: absolute;
  305. bottom: 0;
  306. width: 750rpx;
  307. /* height: 620rpx; */
  308. }
  309. .m-mul-body {
  310. width: 750rpx;
  311. height: 620rpx;
  312. background-color: #fff;
  313. /* #ifndef APP-NVUE */
  314. display: flex;
  315. /* #endif */
  316. flex-direction: column;
  317. }
  318. .m-body-top {
  319. /* #ifndef APP-NVUE */
  320. display: flex;
  321. box-sizing: border-box;
  322. /* #endif */
  323. flex-direction: row;
  324. justify-content: space-between;
  325. align-items: center;
  326. height: 88rpx;
  327. }
  328. .m-body-top-btn {
  329. height: 88rpx;
  330. padding: 0 30rpx;
  331. /* #ifndef APP-NVUE */
  332. display: flex;
  333. box-sizing: border-box;
  334. /* #endif */
  335. flex-direction: row;
  336. justify-content: space-between;
  337. align-items: center;
  338. }
  339. .m--cancel-text {
  340. color: #666666;
  341. font-size: 32rpx;
  342. }
  343. .m--confirm-text {
  344. color: #2f77f1;
  345. font-size: 32rpx;
  346. }
  347. .m-body-title {
  348. flex: 1;
  349. height: 88rpx;
  350. flex-direction: row;
  351. justify-content: center;
  352. align-items: center;
  353. /* #ifndef APP-NVUE */
  354. display: flex;
  355. box-sizing: border-box;
  356. /* #endif */
  357. }
  358. .m--title-text {
  359. color: #101010;
  360. font-size: 32rpx;
  361. lines: 1;
  362. text-overflow: ellipsis;
  363. }
  364. .m-body-content {
  365. flex: 1;
  366. }
  367. /* new 筛选 */
  368. .m-search-content {
  369. width: 750rpx;
  370. height: 80rpx;
  371. padding: 6rpx 30rpx;
  372. /* #ifndef APP-NVUE */
  373. box-sizing: border-box;
  374. /* #endif */
  375. }
  376. .m-search-input {
  377. flex: 1;
  378. border: 2rpx solid #d8e3ff;
  379. /* #ifndef APP-NVUE */
  380. display: flex;
  381. box-sizing: border-box;
  382. /* #endif */
  383. flex-direction: row;
  384. align-items: center;
  385. border-radius: 8rpx;
  386. padding: 0 20rpx;
  387. }
  388. .m-uni-input {
  389. flex: 1;
  390. font-size: 12px;
  391. padding: 0 8px;
  392. }
  393. .m-body-scroll {
  394. flex: 1;
  395. /* #ifndef APP-NVUE */
  396. display: flex;
  397. /* #endif */
  398. }
  399. /* #ifndef APP-NVUE */
  400. /deep/ .uni-scroll-view {
  401. flex: 1;
  402. /* #ifndef APP-NVUE */
  403. display: flex;
  404. /* #endif */
  405. }
  406. /deep/ .uni-scroll-view-content {
  407. flex: 1;
  408. /* #ifndef APP-NVUE */
  409. display: flex;
  410. /* #endif */
  411. flex-direction: column;
  412. }
  413. /* #endif */
  414. .m--scroll-item {
  415. width: 750rpx;
  416. padding: 0 30rpx;
  417. /* #ifndef APP-NVUE */
  418. display: flex;
  419. box-sizing: border-box;
  420. flex-shrink: 0;
  421. /* #endif */
  422. flex-direction: row;
  423. align-items: center;
  424. justify-content: flex-start;
  425. position: relative;
  426. height: 80rpx;
  427. }
  428. .m-s-item-text {
  429. width: 640rpx;
  430. lines: 1;
  431. text-overflow: ellipsis;
  432. color: #101010;
  433. font-size: 28rpx;
  434. }
  435. .m--selected-icon {
  436. position: absolute;
  437. right: 30rpx;
  438. }
  439. .item-active .m-s-item-text {
  440. color: #2f77f1;
  441. }
  442. .mul-picker-empty {
  443. flex: 1;
  444. /* #ifndef APP-NVUE */
  445. display: flex;
  446. /* #endif */
  447. flex-direction: row;
  448. justify-content: center;
  449. align-items: center;
  450. }
  451. .empty-text {
  452. font-size: 28rpx;
  453. color: #666666;
  454. }
  455. </style>