picker.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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-picker-tips">
  46. 注:选择 09:01 ~ 21:01 之间的时间, 不在区间内不能选中
  47. </view>
  48. <view class="uni-title uni-common-pl">日期选择器</view>
  49. <view class="uni-list">
  50. <view class="uni-list-cell">
  51. <view class="uni-list-cell-left">
  52. 当前选择
  53. </view>
  54. <view class="uni-list-cell-db">
  55. <picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
  56. <view class="uni-input">{{date}}</view>
  57. </picker>
  58. </view>
  59. </view>
  60. </view>
  61. <view class="uni-picker-tips">
  62. 注:选择当前时间 ±10 年之间的时间, 不在区间内不能选中
  63. </view>
  64. </view>
  65. </template>
  66. <script>
  67. function getDate(type) {
  68. const date = new Date();
  69. let year = date.getFullYear();
  70. let month = date.getMonth() + 1;
  71. let day = date.getDate();
  72. if (type === 'start') {
  73. year = year - 10;
  74. } else if (type === 'end') {
  75. year = year + 10;
  76. }
  77. month = month > 9 ? month : '0' + month;;
  78. day = day > 9 ? day : '0' + day;
  79. return `${year}-${month}-${day}`;
  80. }
  81. export default {
  82. data() {
  83. return {
  84. title: 'picker',
  85. array: [{name:'中国'},{name: '美国'}, {name:'巴西'}, {name:'日本'}],
  86. index: 0,
  87. multiArray: [
  88. ['亚洲', '欧洲'],
  89. ['中国', '日本'],
  90. ['北京', '上海', '广州']
  91. ],
  92. multiIndex: [0, 0, 0],
  93. date: getDate({
  94. format: true
  95. }),
  96. startDate:getDate('start'),
  97. endDate:getDate('end'),
  98. time: '12:01'
  99. }
  100. },
  101. methods: {
  102. bindPickerChange: function(e) {
  103. console.log('picker发送选择改变,携带值为:' + e.detail.value)
  104. this.index = e.detail.value
  105. },
  106. bindMultiPickerColumnChange: function(e) {
  107. console.log('修改的列为:' + e.detail.column + ',值为:' + e.detail.value)
  108. this.multiIndex[e.detail.column] = e.detail.value
  109. switch (e.detail.column) {
  110. case 0: //拖动第1列
  111. switch (this.multiIndex[0]) {
  112. case 0:
  113. this.multiArray[1] = ['中国', '日本']
  114. this.multiArray[2] = ['北京', '上海', '广州']
  115. break
  116. case 1:
  117. this.multiArray[1] = ['英国', '法国']
  118. this.multiArray[2] = ['伦敦', '曼彻斯特']
  119. break
  120. }
  121. this.multiIndex.splice(1, 1, 0)
  122. this.multiIndex.splice(2, 1, 0)
  123. break
  124. case 1: //拖动第2列
  125. switch (this.multiIndex[0]) { //判断第一列是什么
  126. case 0:
  127. switch (this.multiIndex[1]) {
  128. case 0:
  129. this.multiArray[2] = ['北京', '上海', '广州']
  130. break
  131. case 1:
  132. this.multiArray[2] = ['东京','北海道']
  133. break
  134. }
  135. break
  136. case 1:
  137. switch (this.multiIndex[1]) {
  138. case 0:
  139. this.multiArray[2] = ['伦敦', '曼彻斯特']
  140. break
  141. case 1:
  142. this.multiArray[2] = ['巴黎', '马赛']
  143. break
  144. }
  145. break
  146. }
  147. this.multiIndex.splice(2, 1, 0)
  148. break
  149. }
  150. this.$forceUpdate()
  151. },
  152. bindDateChange: function(e) {
  153. this.date = e.detail.value
  154. },
  155. bindTimeChange: function(e) {
  156. this.time = e.detail.value
  157. }
  158. }
  159. }
  160. </script>
  161. <style>
  162. .uni-picker-tips {
  163. font-size: 12px;
  164. color: #666;
  165. margin-bottom: 15px;
  166. padding: 0 15px;
  167. /* text-align: right; */
  168. }
  169. </style>