CustomMultiSelect.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <view class="popup" v-show="show">
  3. <view class="bg" @tap="cancelMultiple"></view>
  4. <view class="selectMultiple" :animation="animationData">
  5. <view class="multipleBody">
  6. <view class="title">
  7. <view class="close" @tap="cancelMultiple">
  8. 取消
  9. </view>
  10. <view class="title-label">
  11. 请选择
  12. </view>
  13. <view class="confirm" @tap="confirmMultiple">
  14. 确定
  15. </view>
  16. </view>
  17. <!-- 单选 -->
  18. <view class="list" v-if="!isMultiSelect && list.length">
  19. <picker-view :value="selectValue" @change="onSingleChange" class="picker-view" indicator-style="height: 80rpx;">
  20. <picker-view-column>
  21. <view class="picker-item" v-for="(item, index) in list" :key="index" style="font-size: 32rpx;">{{item.label}}</view>
  22. </picker-view-column>
  23. </picker-view>
  24. </view>
  25. <!-- 多选 -->
  26. <view class="list" v-if="isMultiSelect && list.length">
  27. <scroll-view class="diet-list" :scroll-y="true">
  28. <view v-for="(item, index) in list" :class="['item', item.selected ? 'checked' : '']" @tap="onChange(index, item)">
  29. <view class="empty-box" style="width: 32rpx;height: 32rpx;"></view>
  30. <span style="font-size: 32rpx;">{{item.label}}</span>
  31. <icon v-show="item.selected" type="success_no_circle" size="16" color="#2D8DFF"/>
  32. <view v-show="!item.selected" class="empty-box" style="width: 32rpx;height: 32rpx;"></view>
  33. </view>
  34. </scroll-view>
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. export default {
  42. name:"custom-multi-select",
  43. data() {
  44. return {
  45. selectValue: [0],
  46. // 选中值
  47. value: [],
  48. // 选中列表
  49. selected: [],
  50. // 列表数据
  51. list: [],
  52. originList: [],
  53. // 出场动画
  54. animationData: {},
  55. };
  56. },
  57. props: {
  58. // 是否显示
  59. show: {
  60. type: Boolean,
  61. default: false
  62. },
  63. //数据列表
  64. columns: {
  65. type: Array,
  66. default: []
  67. },
  68. // 默认选中
  69. defaultIndex: {
  70. type: Array,
  71. default: [],
  72. },
  73. isMultiSelect: {
  74. type: Boolean,
  75. default: true
  76. },
  77. },
  78. watch: {
  79. // 监听是否显示
  80. show(val) {
  81. if(val) {
  82. this.openMultiple();
  83. }
  84. }
  85. },
  86. methods: {
  87. onSingleChange(e){
  88. console.log(this.selectValue)
  89. this.value = [];
  90. this.selected = [];
  91. let item = this.list[e.detail.value[0]]
  92. if(!item){
  93. return
  94. }
  95. this.value.push(item.value.toString());
  96. this.selected.push({
  97. label: item.label,
  98. value: item.value,
  99. });
  100. },
  101. // 列点击事件
  102. onChange(index, item) {
  103. // 单选
  104. if (!this.isMultiSelect) {
  105. }
  106. // 是否已选中
  107. if(this.value.indexOf(item.value.toString()) >= 0) {
  108. this.list[index].selected = false;
  109. } else {
  110. this.list[index].selected = true;
  111. }
  112. // 筛选已勾选数据
  113. this.value = [];
  114. this.selected = [];
  115. this.list.forEach((col_item, col_index) => {
  116. if(col_item.selected) {
  117. this.value.push(col_item.value.toString());
  118. this.selected.push({
  119. label: col_item.label,
  120. value: col_item.value,
  121. });
  122. }
  123. });
  124. this.$emit("change", {selected: this.selected, value: this.value});
  125. },
  126. // 弹出框开启触发事件
  127. openMultiple() {
  128. // 初始化列表数据,默认勾选数据
  129. this.value = this.defaultIndex;
  130. this.columns.forEach((item, index) => {
  131. this.$set(item, "selected", false);
  132. if(this.value.indexOf(item.value.toString()) >= 0) {
  133. item.selected = true;
  134. }
  135. });
  136. this.originList = Object.assign([], this.columns);
  137. this.list = JSON.parse(JSON.stringify(this.originList))
  138. // 弹出动画
  139. this.openAnimation();
  140. },
  141. // 确认
  142. confirmMultiple() {
  143. this.$emit("confirm", {selected: this.selected, value: this.value});
  144. },
  145. // 关闭/取消
  146. cancelMultiple() {
  147. this.$emit("cancel");
  148. },
  149. // 展开动画
  150. openAnimation() {
  151. var animation = uni.createAnimation()
  152. animation.translate(0, 300).step({ duration: 0 });
  153. this.animationData = animation.export();
  154. this.$nextTick(() => {
  155. animation.translate(0, 0).step({ duration: 300, timingFunction: 'ease' });
  156. this.animationData = animation.export()
  157. })
  158. },
  159. }
  160. }
  161. </script>
  162. <style scoped lang="scss">
  163. .popup {
  164. width: 100%;
  165. height: 100vh;
  166. position: fixed;
  167. z-index: 99999;
  168. left: 0;
  169. bottom: 0;
  170. ::-webkit-scrollbar{
  171. display: none;
  172. }
  173. .bg {
  174. width: 100%;
  175. height: 100%;
  176. background-color: rgba(black, .5);
  177. }
  178. }
  179. .selectMultiple {
  180. width: 100%;
  181. position: absolute;
  182. left: 0;
  183. bottom: 0;
  184. background-color: white;
  185. border-top-left-radius: 20rpx;
  186. border-top-right-radius: 20rpx;
  187. .multipleBody {
  188. width: 100%;
  189. box-sizing: border-box;
  190. .title {
  191. padding: 0 20rpx;
  192. height: 80rpx;
  193. font-size: 28rpx;
  194. display: flex;
  195. justify-content: space-between;
  196. align-items: center; /* 添加这一行 */
  197. box-sizing: border-box;
  198. border-bottom: 2rpx solid rgba(#999, 0.5);
  199. margin-bottom: 20rpx;
  200. .close {
  201. color: $uni-text-color-grey;
  202. }
  203. .title-label{
  204. color: $uni-text-color;
  205. }
  206. .confirm {
  207. color: $uni-user-selected;
  208. }
  209. }
  210. .list {
  211. width: 100%;
  212. height: 600rpx;
  213. position: relative;
  214. .picker-view{
  215. height: 600rpx;
  216. .picker-item {
  217. line-height: 80rpx;
  218. text-align: center;
  219. }
  220. }
  221. .diet-list {
  222. height: 600rpx;
  223. .item {
  224. width: 100%;
  225. height: 80rpx;
  226. font-size: 32rpx;
  227. display: flex;
  228. align-items: center;
  229. justify-content: space-between;
  230. span {
  231. flex: 1;
  232. text-align: center;
  233. overflow: hidden;
  234. display: -webkit-box;
  235. -webkit-box-orient:vertical;
  236. -webkit-line-clamp:1;
  237. }
  238. .icon{
  239. height: 80rpx;
  240. }
  241. &.checked {
  242. color: #2D8DFF;
  243. }
  244. &:last-child {
  245. border-bottom: none;
  246. }
  247. }
  248. }
  249. }
  250. }
  251. }
  252. </style>