multiple-select.vue 8.2 KB

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