zj-dialog-picker.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <zjDialogBox
  3. :title="title"
  4. :minHeight="'50vh'"
  5. :isShow="isShow"
  6. :isSearch="isSearch"
  7. @search="searchDialog"
  8. @cancel="cancelDialog"
  9. @confirm="confirmDialog">
  10. <view class="list2" v-if="styleType === 2">
  11. <view
  12. class="item"
  13. v-for="(item, index) in list"
  14. :key="index"
  15. :class="activeIndex.includes(index) ? 'active':''"
  16. @tap="clickItem(index)">
  17. <view class="text" v-if="getType() == 'string'">{{item[keyName]}}</view>
  18. <view class="text" v-else>
  19. <text v-for="(it, idx) in keyName" :key="index">{{item[it]}}</text>
  20. </view>
  21. <text class="iconfont icon-gouxuan" v-if="activeIndex.includes(index)"></text>
  22. </view>
  23. </view>
  24. <view class="list" v-else>
  25. <view
  26. class="item"
  27. v-for="(item, index) in list"
  28. :key="index"
  29. :class="activeIndex.includes(index) ? 'active':''"
  30. @tap="clickItem(index)">
  31. {{item[keyName]}}
  32. </view>
  33. </view>
  34. </zjDialogBox>
  35. </template>
  36. <script>
  37. import zjDialogBox from "@/components/zj-dialog/zj-dialog-box.vue";
  38. export default {
  39. components: {
  40. zjDialogBox,
  41. },
  42. props: {
  43. isShow: {
  44. type: Boolean,
  45. default: false
  46. },
  47. title: {
  48. type: String,
  49. default: ''
  50. },
  51. list: {
  52. type: Array,
  53. default: []
  54. },
  55. multiple: {
  56. type: Boolean,
  57. default: false
  58. },
  59. keyName: {
  60. type: [String, Array],
  61. default: ''
  62. },
  63. styleType: {
  64. type: Number,
  65. default: 1
  66. },
  67. isSearch: {
  68. type: Boolean,
  69. default: false
  70. }
  71. },
  72. data() {
  73. return {
  74. activeIndex: [],
  75. }
  76. },
  77. mounted() {
  78. },
  79. methods: {
  80. cancelDialog() {
  81. this.$emit('cancel');
  82. },
  83. confirmDialog() {
  84. if(this.activeIndex.length < 1) return this.$toast('请选择');
  85. this.$emit('confirm', this.activeIndex.sort());
  86. },
  87. searchDialog(keyword) {
  88. this.$emit('search', keyword);
  89. },
  90. setValue(val) {
  91. this.activeIndex = JSON.parse(JSON.stringify(val));
  92. },
  93. clickItem(index) {
  94. if(this.multiple) {
  95. const idx = this.activeIndex.indexOf(index);
  96. if (idx > -1) {
  97. this.activeIndex.splice(idx, 1);
  98. } else {
  99. this.activeIndex.push(index);
  100. }
  101. }else {
  102. this.activeIndex = [index];
  103. }
  104. },
  105. findElem(array, attr, val) {
  106. for (var i = 0; i < array.length; i++) {
  107. if (array[i][attr] == val) {
  108. return i // 返回当前索引值
  109. }
  110. }
  111. return -1
  112. },
  113. getType() {
  114. if(typeof(this.keyName) == 'string') {
  115. return 'string';
  116. }else {
  117. return 'other'
  118. }
  119. }
  120. }
  121. }
  122. </script>
  123. <style lang="scss" scoped>
  124. .list {
  125. display: flex;
  126. flex-wrap: wrap;
  127. padding: 10rpx 30rpx 30rpx;
  128. .item {
  129. width: calc((100% - 30rpx) / 2);
  130. height: 76rpx;
  131. text-align: center;
  132. line-height: 76rpx;
  133. border-radius: 76rpx;
  134. background: #F4F5F9;
  135. margin-right: 30rpx;
  136. margin-bottom: 30rpx;
  137. &:nth-child(2n) {
  138. margin-right: 0;
  139. }
  140. &.disabled {
  141. background: #eaeaea;
  142. color: $sec-font;
  143. }
  144. &.active {
  145. background: $theme-color;
  146. color: #ffffff;
  147. }
  148. }
  149. }
  150. .list2 {
  151. background: #ffffff;
  152. display: flex;
  153. flex-direction: column;
  154. padding: 0 30rpx;
  155. overflow-y: scroll;
  156. border-top: 1px solid #F3F4F8;
  157. height: 70vh;
  158. .item {
  159. border-bottom: 1px solid #EAEAEA;
  160. display: flex;
  161. align-items: center;
  162. justify-content: space-between;
  163. padding: 30rpx 0;
  164. position: relative;
  165. &:last-child {
  166. border: none;
  167. }
  168. .text {
  169. font-size: 32rpx;
  170. margin-right: 60rpx;
  171. line-height: 40rpx;
  172. text {
  173. &:not(:first-child) {
  174. font-size: 28rpx;
  175. color: $sec-font;
  176. margin-left: 12rpx;
  177. }
  178. }
  179. }
  180. .iconfont {
  181. color: $theme-color;
  182. font-size: 32rpx;
  183. position: absolute;
  184. right: 0;
  185. top: 50%;
  186. margin-top: -16rpx;
  187. }
  188. &.active {
  189. .text {
  190. color: $theme-color;
  191. text {
  192. color: $theme-color;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. </style>