multiple-choice.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <view>
  3. <view class="popupClick" @click="onPopupShow()"><slot></slot></view>
  4. <popup v-model="currentValue">
  5. <view class="multiple_choice_title">
  6. <text @click="currentValue = false">取消</text>
  7. <view>{{title}}</view>
  8. <text class="main-bg-color" @click="onConfirm">确定</text>
  9. </view>
  10. <scroll-view scroll-y="true" class="multiple_choice_scroll">
  11. <view class="multiple_choice_box">
  12. <view class="multiple_choice_content">
  13. <view class="multiple_choice_item" v-for="(item,index) of rangeList" :key="index" @click="onSelect(index)">
  14. <view class="select" :class="{'active main-bg-color main-border-color': item.select }"></view>
  15. <view class="value">{{item[rangeKey]}}</view>
  16. </view>
  17. </view>
  18. </view>
  19. </scroll-view>
  20. </popup>
  21. </view>
  22. </template>
  23. <script>
  24. import Popup from './popup.vue';
  25. export default {
  26. components: {
  27. Popup
  28. },
  29. props: {
  30. value: {
  31. type: Boolean,
  32. default: false
  33. },
  34. title: {
  35. type: String,
  36. default: ""
  37. },
  38. range: {
  39. type: Array,
  40. default: function(){
  41. return []
  42. }
  43. },
  44. rangeKey: {
  45. type: String,
  46. default: "name"
  47. },
  48. },
  49. created() {
  50. if (typeof this.value !== 'undefined') {
  51. this.currentValue = this.value;
  52. }
  53. this.rangeList = this.range.map(item => {
  54. item.select = false;
  55. return item;
  56. });
  57. },
  58. watch: {
  59. value(val) {
  60. this.currentValue = val;
  61. },
  62. currentValue(val) {
  63. this.$emit(val ? 'on-show' : 'on-hide');
  64. this.$emit('input', val);
  65. },
  66. range(val){
  67. this.rangeList = val.map(item => {
  68. item.select = false;
  69. return item;
  70. });
  71. }
  72. },
  73. data() {
  74. return {
  75. currentValue: false,
  76. rangeList: []
  77. };
  78. },
  79. methods: {
  80. onPopupShow(){
  81. this.currentValue = true;
  82. },
  83. onSelect(index){
  84. let item = this.rangeList[index];
  85. item.select = !item.select;
  86. this.$set(this.rangeList, index, item);
  87. },
  88. onConfirm(){
  89. let resultList = this.rangeList.filter(item => {
  90. if(item.select){
  91. return true;
  92. } else {
  93. return false;
  94. }
  95. });
  96. if(resultList.length > 0){
  97. this.currentValue = false;
  98. this.$emit("change", resultList);
  99. } else {
  100. uni.showToast({
  101. title: "请选择",
  102. icon: "none"
  103. });
  104. }
  105. }
  106. },
  107. mounted() {}
  108. };
  109. </script>
  110. <style scoped>
  111. .multiple_choice_title {
  112. display: flex;
  113. justify-content: space-between;
  114. height: 88upx;
  115. line-height: 88upx;
  116. border-bottom: 2upx solid #ebebeb;
  117. padding: 0 20upx;
  118. background-color: #FFF;
  119. }
  120. .multiple_choice_title view {
  121. font-size: 32upx;
  122. }
  123. .multiple_choice_title text {
  124. width: 80upx;
  125. flex-shrink: 0;
  126. text-align: center;
  127. }
  128. .multiple_choice_title text {
  129. font-size: 28upx;
  130. color: #999;
  131. }
  132. .multiple_choice_scroll {
  133. background-color: #FFF;
  134. max-height: 60vh;
  135. min-height: 30vh;
  136. }
  137. .multiple_choice_box {
  138. display: flex;
  139. justify-content: center;
  140. align-items: center;
  141. min-height: 30vh;
  142. }
  143. .multiple_choice_content .multiple_choice_item {
  144. height: 100rpx;
  145. padding: 0 30rpx;
  146. font-size: 30rpx;
  147. display: flex;
  148. align-items: center;
  149. }
  150. .multiple_choice_content .multiple_choice_item .select {
  151. width: 40rpx;
  152. height: 40rpx;
  153. margin-right: 15rpx;
  154. flex-shrink: 0;
  155. border-radius: 50%;
  156. border: 2rpx solid #ccc;
  157. }
  158. .multiple_choice_content .multiple_choice_item .select.active {
  159. border: 2rpx solid;
  160. text-align: center;
  161. line-height: 38rpx;
  162. transform: rotate(15deg);
  163. }
  164. .multiple_choice_content .multiple_choice_item .select.active::before {
  165. content: "√";
  166. color: #FFF;
  167. }
  168. .multiple_choice_content .multiple_choice_item .value {
  169. width: calc(100% - 55rpx);
  170. overflow: hidden;
  171. text-overflow: ellipsis;
  172. white-space: nowrap;
  173. }
  174. </style>