picker.uvue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex: 1">
  4. <!-- #endif -->
  5. <page-head :title="title"></page-head>
  6. <text class="uni-title uni-common-pl">禁用选择器</text>
  7. <view class="uni-list">
  8. <view class="uni-list-cell">
  9. <view class="uni-list-cell-left">当前选择</view>
  10. <view class="uni-list-cell-db">
  11. <picker class="picker-disabled--test" @change="bindPickerChange" disabled :value="index" :range="selectorArray" range-key="name">
  12. <view class="uni-input picker-disabled--value">{{selectorArray[index].name}}</view>
  13. </picker>
  14. </view>
  15. </view>
  16. </view>
  17. <text class="uni-picker-tips">注:值与普通选择器同步,但不可选择</text>
  18. <text class="uni-title uni-common-pl">普通选择器</text>
  19. <view class="uni-list">
  20. <view class="uni-list-cell">
  21. <view class="uni-list-cell-left">当前选择</view>
  22. <view class="uni-list-cell-db">
  23. <picker class="picker-selector--test" @change="bindPickerChange" :value="index" :range="selectorArray" range-key="name">
  24. <view class="uni-input picker-selector--value">{{selectorArray[index].name}}</view>
  25. </picker>
  26. </view>
  27. </view>
  28. </view>
  29. <text class="uni-title uni-common-pl">多列选择器</text>
  30. <view class="uni-list">
  31. <view class="uni-list-cell">
  32. <text class="uni-list-cell-left">当前选择</text>
  33. <view class="uni-list-cell-db">
  34. <picker class="picker-multi--test" mode="multiSelector" @columnchange="bindMultiPickerColumnChange" :value="multiIndex" :range="multiArray">
  35. <view class="uni-input picker-multi--value">
  36. {{multiArray[0][multiIndex[0]]}},{{multiArray[1][multiIndex[1]]}},{{multiArray[2][multiIndex[2]]}}
  37. </view>
  38. </picker>
  39. </view>
  40. </view>
  41. </view>
  42. <text class="uni-title uni-common-pl">时间选择器</text>
  43. <view class="uni-list">
  44. <view class="uni-list-cell">
  45. <view class="uni-list-cell-left">当前选择</view>
  46. <view class="uni-list-cell-db">
  47. <picker class="picker-time--test" mode="time" :value="time" start="09:01" end="21:01" @change="bindTimeChange">
  48. <view class="uni-input">{{time}}</view>
  49. </picker>
  50. </view>
  51. </view>
  52. </view>
  53. <text class="uni-picker-tips">注:选择 09:01 ~ 21:01 之间的时间, 不在区间内不能选中</text>
  54. <text class="uni-title uni-common-pl">日期选择器 fields = day</text>
  55. <view class="uni-list">
  56. <view class="uni-list-cell">
  57. <text class="uni-list-cell-left">当前选择 </text>
  58. <view class="uni-list-cell-db">
  59. <picker class="picker-date-day--test" mode="date" :value="dayDate" :start="startDate" :end="endDate" @change="bindDayDateChange">
  60. <view class="uni-input">{{dayDate}}</view>
  61. </picker>
  62. </view>
  63. </view>
  64. </view>
  65. <text class="uni-picker-tips">注:选择当前时间 ±10 年之间的时间, 不在区间内不能选中</text>
  66. <text class="uni-title uni-common-pl">日期选择器 fields = month</text>
  67. <view class="uni-list">
  68. <view class="uni-list-cell">
  69. <text class="uni-list-cell-left">当前选择 </text>
  70. <view class="uni-list-cell-db">
  71. <picker class="picker-date-month--test" mode="date" fields="month" :value="monthDate" :start="startDate" :end="endDate" @change="bindMonthDateChange">
  72. <view class="uni-input">{{monthDate}}</view>
  73. </picker>
  74. </view>
  75. </view>
  76. </view>
  77. <text class="uni-picker-tips">注:选择当前时间 ±10 年之间的时间, 不在区间内不能选中,且日不可选</text>
  78. <text class="uni-title uni-common-pl">日期选择器 fields = year</text>
  79. <view class="uni-list">
  80. <view class="uni-list-cell">
  81. <text class="uni-list-cell-left">当前选择 </text>
  82. <view class="uni-list-cell-db">
  83. <picker class="picker-date-year--test" mode="date" fields="year" :value="yearDate" :start="startDate" :end="endDate" @change="bindYearDateChange">
  84. <view class="uni-input">{{yearDate}}</view>
  85. </picker>
  86. </view>
  87. </view>
  88. </view>
  89. <text class="uni-picker-tips">注:选择当前时间 ±10 年之间的时间, 不在区间内不能选中,且只能选择年</text>
  90. <!-- #ifdef APP -->
  91. </scroll-view>
  92. <!-- #endif -->
  93. </template>
  94. <script lang="uts">
  95. import { state, setEventCallbackNum } from '@/store/index.uts'
  96. type DataType = {
  97. name : string
  98. }
  99. function getDate(date_text : string = 'day', type ?: string) : string {
  100. const date = new Date();
  101. let year : string | number = date.getFullYear();
  102. let month : string | number = date.getMonth() + 1;
  103. let day : string | number = date.getDate();
  104. if (type == 'start') {
  105. year = year - 10;
  106. } else if (type == 'end') {
  107. year = year + 10;
  108. }
  109. month = month > 9 ? month : '0' + month;;
  110. day = day > 9 ? day : '0' + day;
  111. if (date_text == 'month') {
  112. return `${year}-${month}`;
  113. } else if (date_text == 'year') {
  114. return `${year}`;
  115. }
  116. return `${year}-${month}-${day}`;
  117. }
  118. export default {
  119. data() {
  120. return {
  121. title: 'picker',
  122. selectorArray: [{ name: '中国' }, { name: '美国' }, { name: '巴西' }, { name: '日本' }] as DataType[],
  123. index: 0,
  124. multiArray: [
  125. ['亚洲', '欧洲'],
  126. ['中国', '日本'],
  127. ['北京', '上海', '广州']
  128. ] as Array<string[]>,
  129. multiIndex: [0, 0, 0] as number[],
  130. dayDate: getDate('day'),
  131. monthDate: getDate('month'),
  132. yearDate: getDate('year'),
  133. startDate: getDate('day', 'start'),
  134. endDate: getDate('day', 'end'),
  135. time: '12:01'
  136. }
  137. },
  138. methods: {
  139. bindPickerChange: function (e : UniPickerChangeEvent) {
  140. console.log('picker发送选择改变,携带值为:' + e.detail.value)
  141. this.index = e.detail.value
  142. },
  143. bindMultiPickerColumnChange: function (e : UniPickerColumnChangeEvent) {
  144. console.log('修改的列为:' + e.detail.column + ',值为:' + e.detail.value)
  145. this.multiIndex[e.detail.column] = e.detail.value
  146. switch (e.detail.column) {
  147. case 0: //拖动第1列
  148. switch (this.multiIndex[0]) {
  149. case 0:
  150. this.multiArray[1] = ['中国', '日本']
  151. this.multiArray[2] = ['北京', '上海', '广州']
  152. break
  153. case 1:
  154. this.multiArray[1] = ['英国', '法国']
  155. this.multiArray[2] = ['伦敦', '曼彻斯特']
  156. break
  157. }
  158. this.multiIndex.splice(1, 1, 0)
  159. this.multiIndex.splice(2, 1, 0)
  160. break
  161. case 1: //拖动第2列
  162. switch (this.multiIndex[0]) { //判断第一列是什么
  163. case 0:
  164. switch (this.multiIndex[1]) {
  165. case 0:
  166. this.multiArray[2] = ['北京', '上海', '广州']
  167. break
  168. case 1:
  169. this.multiArray[2] = ['东京', '北海道']
  170. break
  171. }
  172. break
  173. case 1:
  174. switch (this.multiIndex[1]) {
  175. case 0:
  176. this.multiArray[2] = ['伦敦', '曼彻斯特']
  177. break
  178. case 1:
  179. this.multiArray[2] = ['巴黎', '马赛']
  180. break
  181. }
  182. break
  183. }
  184. this.multiIndex.splice(2, 1, 0)
  185. break
  186. }
  187. this.$forceUpdate()
  188. },
  189. bindDayDateChange: function (e : UniPickerChangeEvent) {
  190. this.dayDate = e.detail.value as string
  191. },
  192. bindMonthDateChange: function (e : UniPickerChangeEvent) {
  193. this.monthDate = e.detail.value as string
  194. },
  195. bindYearDateChange: function (e : UniPickerChangeEvent) {
  196. this.yearDate = e.detail.value as string
  197. },
  198. bindTimeChange: function (e : UniPickerChangeEvent) {
  199. this.time = e.detail.value as string
  200. },
  201. // 自动化函数
  202. setSelectorValue(){
  203. this.index = 2
  204. },
  205. getEventCallbackNum() : number {
  206. return state.eventCallbackNum
  207. },
  208. // 自动化测试
  209. setEventCallbackNum(num : number) {
  210. setEventCallbackNum(num)
  211. },
  212. }
  213. }
  214. </script>
  215. <style>
  216. .uni-title {
  217. font-size: 14px;
  218. }
  219. .uni-picker-tips {
  220. font-size: 12px;
  221. color: #666;
  222. margin-top: 5px;
  223. margin-bottom: 15px;
  224. padding: 0 15px;
  225. }
  226. </style>