picker.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-title uni-common-pl">普通选择器</view>
  5. <view class="uni-list">
  6. <view class="uni-list-cell">
  7. <view class="uni-list-cell-left">
  8. 当前选择
  9. </view>
  10. <view class="uni-list-cell-db">
  11. <picker @change="bindPickerChange" :value="index" :range="array" range-key="name">
  12. <view class="uni-input">{{array[index].name}}</view>
  13. </picker>
  14. </view>
  15. </view>
  16. </view>
  17. <!-- #ifndef MP-ALIPAY -->
  18. <view class="uni-title uni-common-pl">多列选择器</view>
  19. <view class="uni-list">
  20. <view class="uni-list-cell">
  21. <view class="uni-list-cell-left">
  22. 当前选择
  23. </view>
  24. <view class="uni-list-cell-db">
  25. <picker mode="multiSelector" @columnchange="bindMultiPickerColumnChange" :value="multiIndex" :range="multiArray">
  26. <view class="uni-input">{{multiArray[0][multiIndex[0]]}},{{multiArray[1][multiIndex[1]]}},{{multiArray[2][multiIndex[2]]}}</view>
  27. </picker>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- #endif -->
  32. <view class="uni-title uni-common-pl">时间选择器</view>
  33. <view class="uni-list">
  34. <view class="uni-list-cell">
  35. <view class="uni-list-cell-left">
  36. 当前选择
  37. </view>
  38. <view class="uni-list-cell-db">
  39. <picker mode="time" :value="time" start="09:01" end="21:01" @change="bindTimeChange">
  40. <view class="uni-input">{{time}}</view>
  41. </picker>
  42. </view>
  43. </view>
  44. </view>
  45. <view class="uni-title uni-common-pl">日期选择器</view>
  46. <view class="uni-list">
  47. <view class="uni-list-cell">
  48. <view class="uni-list-cell-left">
  49. 当前选择
  50. </view>
  51. <view class="uni-list-cell-db">
  52. <picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
  53. <view class="uni-input">{{date}}</view>
  54. </picker>
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. </template>
  60. <script>
  61. function getDate(type) {
  62. const date = new Date();
  63. let year = date.getFullYear();
  64. let month = date.getMonth() + 1;
  65. let day = date.getDate();
  66. if (type === 'start') {
  67. year = year - 60;
  68. } else if (type === 'end') {
  69. year = year + 2;
  70. }
  71. month = month > 9 ? month : '0' + month;;
  72. day = day > 9 ? day : '0' + day;
  73. return `${year}-${month}-${day}`;
  74. }
  75. export default {
  76. data() {
  77. return {
  78. title: 'picker',
  79. array: [{name:'中国'},{name: '美国'}, {name:'巴西'}, {name:'日本'}],
  80. index: 0,
  81. multiArray: [
  82. ['亚洲', '欧洲'],
  83. ['中国', '日本'],
  84. ['北京', '上海', '广州']
  85. ],
  86. multiIndex: [0, 0, 0],
  87. date: getDate({
  88. format: true
  89. }),
  90. startDate:getDate('start'),
  91. endDate:getDate('end'),
  92. time: '12:01'
  93. }
  94. },
  95. methods: {
  96. bindPickerChange: function(e) {
  97. console.log('picker发送选择改变,携带值为:' + e.detail.value)
  98. this.index = e.detail.value
  99. },
  100. bindMultiPickerColumnChange: function(e) {
  101. console.log('修改的列为:' + e.detail.column + ',值为:' + e.detail.value)
  102. this.multiIndex[e.detail.column] = e.detail.value
  103. switch (e.detail.column) {
  104. case 0: //拖动第1列
  105. switch (this.multiIndex[0]) {
  106. case 0:
  107. this.multiArray[1] = ['中国', '日本']
  108. this.multiArray[2] = ['北京', '上海', '广州']
  109. break
  110. case 1:
  111. this.multiArray[1] = ['英国', '法国']
  112. this.multiArray[2] = ['伦敦', '曼彻斯特']
  113. break
  114. }
  115. this.multiIndex.splice(1, 1, 0)
  116. this.multiIndex.splice(2, 1, 0)
  117. break
  118. case 1: //拖动第2列
  119. switch (this.multiIndex[0]) { //判断第一列是什么
  120. case 0:
  121. switch (this.multiIndex[1]) {
  122. case 0:
  123. this.multiArray[2] = ['北京', '上海', '广州']
  124. break
  125. case 1:
  126. this.multiArray[2] = ['东京','北海道']
  127. break
  128. }
  129. break
  130. case 1:
  131. switch (this.multiIndex[1]) {
  132. case 0:
  133. this.multiArray[2] = ['伦敦', '曼彻斯特']
  134. break
  135. case 1:
  136. this.multiArray[2] = ['巴黎', '马赛']
  137. break
  138. }
  139. break
  140. }
  141. this.multiIndex.splice(2, 1, 0)
  142. break
  143. }
  144. this.$forceUpdate()
  145. },
  146. bindDateChange: function(e) {
  147. this.date = e.detail.value
  148. },
  149. bindTimeChange: function(e) {
  150. this.time = e.detail.value
  151. }
  152. }
  153. }
  154. </script>
  155. <style>
  156. </style>