momo-multipleSelect.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <template>
  2. <view class="select-container" v-show="show" @touchmove.stop.prevent>
  3. <view
  4. class="mask"
  5. :class="activeClass ? 'mask-show' : ''"
  6. @tap="onCancel(true)"
  7. ></view>
  8. <view class="select-box" :class="activeClass ? 'select-box-show' : ''">
  9. <view class="header">
  10. <text class="cancel" @tap="onCancel">{{ cancelText }}</text>
  11. <view class="all" @tap="onAllToggle" v-if="allShow">
  12. <text :class="isAll ? 'all-active' : ''">全选</text>
  13. </view>
  14. <text class="confirm" @tap="onConfirm">{{ confirmText }}</text>
  15. </view>
  16. <view class="body-warp">
  17. <scroll-view class="body" scroll-y="true">
  18. <slot v-if="!data.length" name="tips">
  19. <view class="empty-tips">暂无数据~</view>
  20. </slot>
  21. <view
  22. class="select-item"
  23. :class="[
  24. item.disabled ? 'disabled' : '',
  25. selectedArr[index] ? 'selected' : '',
  26. ]"
  27. v-for="(item, index) in data"
  28. :key="item[valueName]"
  29. @tap="onSelected(index)"
  30. >
  31. <view class="label">{{ item[labelName] }}</view>
  32. <text v-show="selectedArr[index]" class="selected-icon">✔</text>
  33. </view>
  34. </scroll-view>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <!-- 多选组件 -->
  40. <script>
  41. export default {
  42. model: {
  43. prop: "value",
  44. event: ["input"],
  45. },
  46. data() {
  47. return {
  48. show: false, //是否显示
  49. activeClass: false, //激活样式状态
  50. selectedArr: [], //选择对照列表
  51. selectedArrOld: [], //选择对照列表上一次的数据
  52. };
  53. },
  54. onShow() {
  55. this.show = this.value;
  56. },
  57. computed: {
  58. // 返回是否全选
  59. isAll() {
  60. let wipeDisabledList = this.returnWipeDisabledList();
  61. if (!wipeDisabledList.length) return false;
  62. return !wipeDisabledList.includes(false);
  63. },
  64. },
  65. props: {
  66. // 双向绑定
  67. value: {
  68. type: Boolean,
  69. default: false,
  70. },
  71. // 取消按钮文字
  72. cancelText: {
  73. type: String,
  74. default: "取消",
  75. },
  76. // 确认按钮文字
  77. confirmText: {
  78. type: String,
  79. default: "确认",
  80. },
  81. // label对应的key名称
  82. labelName: {
  83. type: String,
  84. default: "label",
  85. },
  86. // value对应的key名称
  87. valueName: {
  88. type: String,
  89. default: "value",
  90. },
  91. // 是否允许点击遮罩层关闭
  92. maskCloseAble: {
  93. type: Boolean,
  94. default: true,
  95. },
  96. // 是否显示全选
  97. allShow: {
  98. type: Boolean,
  99. default: true,
  100. },
  101. // 模式
  102. mode: {
  103. type: String,
  104. default: "multiple",
  105. },
  106. // 默认选中值
  107. defaultSelected: {
  108. type: Array,
  109. default: function () {
  110. return [];
  111. },
  112. },
  113. // 数据源
  114. data: {
  115. type: Array,
  116. required: true,
  117. default: () => {
  118. return [];
  119. },
  120. },
  121. },
  122. watch: {
  123. async value(newVal) {
  124. this.show = newVal;
  125. await this.$nextTick();
  126. this.activeClass = newVal;
  127. if (newVal) {
  128. this.selectedArrOld = JSON.parse(JSON.stringify(this.selectedArr));
  129. }
  130. },
  131. show(newVal) {
  132. this.$emit("input", newVal);
  133. this.$emit("change", newVal);
  134. },
  135. data: {
  136. // 设置初始选择对照列表
  137. handler(list) {
  138. this.selectedArr = list.map((el) => false);
  139. this.setItemActiveState();
  140. },
  141. deep: true,
  142. immediate: true,
  143. },
  144. defaultSelected: {
  145. handler() {
  146. this.setItemActiveState();
  147. },
  148. deep: true,
  149. immediate: true,
  150. },
  151. },
  152. methods: {
  153. // 设置默认选中通用办法
  154. setItemActiveState() {
  155. if (this.data.length && this.defaultSelected.length) {
  156. this.data.forEach((item, i) => {
  157. for (let n = 0; n < this.defaultSelected.length; n++) {
  158. if (
  159. !item.disabled &&
  160. item[this.valueName] === this.defaultSelected[n]
  161. ) {
  162. this.selectedArr.splice(i, 1, true);
  163. break;
  164. }
  165. }
  166. });
  167. }
  168. },
  169. /**
  170. * 选择事件
  171. * @index {Number} 点击下标
  172. */
  173. onSelected(index) {
  174. if (this.data[index].disabled) return;
  175. let index2Active = this.selectedArr[index];
  176. this.selectedArr.splice(index, 1, !index2Active);
  177. },
  178. // 取消事件
  179. onCancel(isMask) {
  180. if (!isMask || this.maskCloseAble) {
  181. this.show = false;
  182. this.selectedArr = JSON.parse(JSON.stringify(this.selectedArrOld));
  183. } else {
  184. return;
  185. }
  186. this.$emit("cancel");
  187. },
  188. // 返回去除了disabled状态后的对照列表
  189. returnWipeDisabledList() {
  190. let arr = [];
  191. this.selectedArr.forEach((el, index) => {
  192. if (!this.data[index].disabled) arr.push(el);
  193. });
  194. return arr;
  195. },
  196. // 全选/非全选事件
  197. onAllToggle() {
  198. let wipeDisabledList = this.returnWipeDisabledList();
  199. // 如果去除了disabled的对照列表有false的数据,代表未全选
  200. if (wipeDisabledList.includes(false)) {
  201. this.selectedArr.forEach((el, index) => {
  202. if (!this.data[index].disabled)
  203. this.selectedArr.splice(index, 1, true);
  204. });
  205. } else {
  206. this.selectedArr.forEach((el, index) => {
  207. if (!this.data[index].disabled)
  208. el = this.selectedArr.splice(index, 1, false);
  209. });
  210. }
  211. },
  212. // 确定事件
  213. onConfirm() {
  214. this.show = false;
  215. let selectedData = [];
  216. this.selectedArr.forEach((el, index) => {
  217. if (el) {
  218. selectedData.push(this.data[index]);
  219. }
  220. });
  221. if (this.mode === "multiple") {
  222. this.$emit("confirm", selectedData);
  223. } else {
  224. let backData = selectedData[0] || {};
  225. this.$emit("confirm", backData);
  226. }
  227. },
  228. },
  229. };
  230. </script>
  231. <style lang="scss" scoped>
  232. .select-container {
  233. width: 100vw;
  234. height: 100vh;
  235. position: fixed;
  236. left: 0;
  237. top: 0;
  238. z-index: 999;
  239. $paddingLR: 18rpx;
  240. .mask {
  241. width: 100%;
  242. height: 100%;
  243. background-color: $uni-bg-color-mask;
  244. opacity: 0;
  245. transition: opacity 0.3s;
  246. &.mask-show {
  247. opacity: 1;
  248. }
  249. }
  250. // 选择器内容区域
  251. .select-box {
  252. width: 100%;
  253. position: absolute;
  254. bottom: 0;
  255. left: 0;
  256. transform: translate3d(0px, 100%, 0px);
  257. background-color: $uni-bg-color;
  258. transition: all 0.3s;
  259. &.select-box-show {
  260. transform: translateZ(0);
  261. }
  262. .header {
  263. display: flex;
  264. box-sizing: border-box;
  265. width: 100%;
  266. justify-content: space-between;
  267. border-bottom: 1px solid $uni-border-color;
  268. line-height: 76rpx;
  269. font-size: 30rpx;
  270. padding: 0 $paddingLR;
  271. .cancel {
  272. color: $uni-text-color-grey;
  273. }
  274. .all {
  275. .all-active {
  276. &::after {
  277. display: inline-block;
  278. content: "✔";
  279. padding-left: 8rpx;
  280. }
  281. }
  282. }
  283. .confirm {
  284. color: $uni-color-primary;
  285. }
  286. }
  287. .body-warp {
  288. width: 100%;
  289. height: 30vh;
  290. box-sizing: border-box;
  291. padding: 20rpx $paddingLR;
  292. }
  293. .body {
  294. width: 100%;
  295. height: 100%;
  296. overflow-y: auto;
  297. .empty-tips {
  298. margin-top: 25%;
  299. text-align: center;
  300. font-size: 26rpx;
  301. color: $uni-color-error;
  302. }
  303. .select-item {
  304. display: flex;
  305. font-size: 26rpx;
  306. line-height: 58rpx;
  307. color: #303133;
  308. position: relative;
  309. transition: all 0.3s;
  310. &.selected {
  311. color: $uni-color-primary;
  312. }
  313. &.disabled {
  314. color: $uni-text-color-disable;
  315. }
  316. > .label {
  317. flex: 1;
  318. text-align: center;
  319. }
  320. > .selected-icon {
  321. position: absolute;
  322. right: 0;
  323. top: 50%;
  324. transform: translateY(-50%);
  325. }
  326. }
  327. }
  328. }
  329. }
  330. </style>