pickerThreeColumn.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <!-- 三级联动picker -->
  2. <template>
  3. <view v-if="visible">
  4. <view :class="{'pickerMask':visible}" @click="pickerCancel" catchtouchmove="true"></view>
  5. <view :class="['picker-cont',{'picker-view-show':visible}]">
  6. <view class="picker-top">
  7. <view style="color:#999" @click="pickerCancel">取消</view>
  8. <view style="color:#576b95" @click="pickerEnsure">确认</view>
  9. </view>
  10. <picker-view
  11. indicator-style="height: 40px;"
  12. class="picker-view-cont"
  13. :value="value"
  14. @change="pickerChangeHandle"
  15. >
  16. <picker-view-column>
  17. <view class="picker-item" v-for="(item,index) in pickerOne" :key="index">{{item.label}}
  18. </view>
  19. </picker-view-column>
  20. <picker-view-column>
  21. <view class="picker-item" v-for="(item,index) in pickerTwo" :key="index">{{item.label}}
  22. </view>
  23. </picker-view-column>
  24. <picker-view-column>
  25. <view class="picker-item" v-for="(item,index) in pickerThree" :key="index">
  26. {{item.label}}
  27. </view>
  28. </picker-view-column>
  29. </picker-view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. export default {
  35. props:{
  36. data: {
  37. type: Array
  38. },
  39. visible: {
  40. type: Boolean
  41. },
  42. default_picker_val:{
  43. type: Array
  44. }
  45. },
  46. watch: {
  47. visible(newval) {
  48. newval && this.init()
  49. },
  50. default_picker_val(newval) {
  51. this.val = newval;
  52. }
  53. },
  54. data() {
  55. return {
  56. value: [],
  57. pickerOne:[],
  58. pickerTwo:[],
  59. pickerThree: []
  60. };
  61. },
  62. mounted() {
  63. },
  64. methods: {
  65. /* 值改变时 */
  66. pickerChangeHandle(e) {
  67. let data = this.data;
  68. let changeValue = e.detail.value;
  69. let pickerTwo = [];
  70. let pickerThree = [];
  71. // 重新渲染第二列
  72. // 如果是第一列滚动
  73. if (changeValue[0] !== this.value[0]) {
  74. this.pickerTwo = [];
  75. for (let i = 0, length = data[changeValue[0]].children.length; i < length; i++) {
  76. pickerTwo.push(data[changeValue[0]].children[i]);
  77. }
  78. // 重新渲染第三列
  79. for (let i = 0, length = data[changeValue[0]].children[0].children.length; i <
  80. length; i++) {
  81. pickerThree.push(data[changeValue[0]].children[0].children[i]);
  82. }
  83. changeValue[1] = 0;
  84. changeValue[2] = 0;
  85. this.pickerTwo = pickerTwo;
  86. this.pickerThree = pickerThree;
  87. } else if (changeValue[1] !== this.value[1]) {
  88. // 第二列滚动
  89. // 重新渲染第三列
  90. this.pickerThree = [];
  91. pickerTwo = this.pickerTwo;
  92. for (let i = 0, length = data[changeValue[0]].children[changeValue[1]].children
  93. .length; i <
  94. length; i++) {
  95. pickerThree.push(data[changeValue[0]].children[changeValue[1]]
  96. .children[
  97. i]);
  98. }
  99. changeValue[2] = 0;
  100. this.pickerThree = pickerThree;
  101. }
  102. this.value = changeValue;
  103. },
  104. /* 取消*/
  105. pickerCancel() {
  106. this.$emit('cancel')
  107. },
  108. /* 确定*/
  109. pickerEnsure() {
  110. console.log(this.value)
  111. let val = this.value;
  112. if(!this.data[val[0]].children.length || !this.data[val[0]].children[val[1]].children.length) return this.pickerCancel();
  113. // this.pickerValueDefault = this.value;
  114. this.$emit('ensure',this.value)
  115. this.pickerCancel();
  116. },
  117. init() {
  118. let pickerOne = [];
  119. let pickerTwo = [];
  120. let pickerThree = [];
  121. // 第一列
  122. for (let i = 0, length = this.data.length; i < length; i++) {
  123. pickerOne.push(this.data[i]);
  124. }
  125. // 渲染第二列
  126. this.value = this.default_picker_val.length ? this.default_picker_val : [0, 0, 0];
  127. let num = this.value[0];
  128. for (let i = 0, length = this.data[num].children.length; i < length; i++) {
  129. if( this.data[num].children === 0) {
  130. pickerTwo = []
  131. return
  132. }
  133. pickerTwo.push(this.data[num].children[i]);
  134. }
  135. // 第三列
  136. let numSecond = this.value[1];
  137. if(this.data[num].children.length) for (let i = 0, length = this.data[num].children[numSecond].children.length; i <
  138. length; i++) {
  139. if( this.data[num].children[numSecond].children.length === 0) {
  140. pickerThree = []
  141. return
  142. }
  143. pickerThree.push(
  144. this.data[num].children[numSecond].children[i]
  145. );
  146. }
  147. this.pickerOne = pickerOne;
  148. this.pickerTwo = pickerTwo;
  149. this.pickerThree = pickerThree;
  150. }
  151. }
  152. }
  153. </script>
  154. <style lang="scss" scoped>
  155. .pickerMask {
  156. position: fixed;
  157. z-index: 1000;
  158. top: 0;
  159. right: 0;
  160. left: 0;
  161. bottom: 0;
  162. background: rgba(0, 0, 0, 0.6);
  163. }
  164. .picker-cont {
  165. position: fixed;
  166. bottom: 0;
  167. left: 0;
  168. width: 100%;
  169. height: 250px;
  170. transition: all 0.5s;
  171. transform: translateY(100%);
  172. z-index: 3000;
  173. background-color: #fff;
  174. .picker-top {
  175. display: flex;
  176. height: 100rpx;
  177. padding: 0 30rpx;
  178. justify-content: space-between;
  179. align-items: center;
  180. }
  181. }
  182. .picker-view-show {
  183. transform: translateY(0);
  184. }
  185. .picker-view-cont {
  186. position: relative;
  187. bottom: 0;
  188. left: 0;
  189. width: 100%;
  190. height: 238px;
  191. background-color: rgba(255, 255, 255, 1);
  192. .picker-item {
  193. height: 40px;
  194. text-align: center;
  195. line-height: 40px;
  196. color: #333;
  197. }
  198. }
  199. </style>