picker-view.uvue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view class="uni-title">
  6. 日期:{{year}}年{{month}}月{{day}}日
  7. </view>
  8. </view>
  9. <picker-view class="picker-view" :value="value" @change="bindChange" :indicator-style="indicatorStyle"
  10. :indicator-class="indicatorClass" :mask-style="maskStyle" :mask-class="maskClass" :mask-top-style="maskTopStyle"
  11. :mask-bottom-style="maskBottomStyle">
  12. <picker-view-column class="picker-view-column">
  13. <view class="item" v-for="(item,index) in years" :key="index"><text class="text">{{item}}年</text></view>
  14. </picker-view-column>
  15. <picker-view-column class="picker-view-column">
  16. <view class="item" v-for="(item,index) in months" :key="index"><text class="text">{{item}}月</text>
  17. </view>
  18. </picker-view-column>
  19. <picker-view-column class="picker-view-column">
  20. <view class="item" v-for="(item,index) in days" :key="index"><text class="text">{{item}}日</text></view>
  21. </picker-view-column>
  22. </picker-view>
  23. <boolean-data :defaultValue="false" title="设置选择器中间选中框的样式" @change="setIndicatorStyle"></boolean-data>
  24. <!-- #ifdef WEB || MP-WEIXIN -->
  25. <boolean-data :defaultValue="false" title="设置选择器中间选中框的类名" @change="setIndicatorClass"></boolean-data>
  26. <boolean-data :defaultValue="false" title="设置蒙层的样式" @change="setMaskStyle"></boolean-data>
  27. <boolean-data :defaultValue="false" title="设置蒙层的类名" @change="setMaskClass"></boolean-data>
  28. <!-- #endif -->
  29. <!-- #ifdef APP -->
  30. <boolean-data :defaultValue="false" title="设置蒙层上半部分的样式" @change="setMaskTopStyle"></boolean-data>
  31. <boolean-data :defaultValue="false" title="设置蒙层下半部分的样式" @change="setMaskBottomStyle"></boolean-data>
  32. <!-- #endif -->
  33. </view>
  34. </template>
  35. <script lang="uts">
  36. import { state, setEventCallbackNum } from '@/store/index.uts'
  37. export default {
  38. data() {
  39. // 20180112 HBuilderX内测开始 :)
  40. const _years : number[] = []
  41. const _year = 2018
  42. const _months : number[] = []
  43. const _month : number = 1
  44. const _days : number[] = []
  45. const _day = 12
  46. for (let i = 2000; i <= _year; i++) {
  47. _years.push(i)
  48. }
  49. for (let i = 1; i <= 12; i++) {
  50. _months.push(i)
  51. }
  52. for (let i = 1; i <= 31; i++) {
  53. _days.push(i)
  54. }
  55. return {
  56. title: 'picker-view',
  57. years: _years as number[],
  58. year: _year as number,
  59. months: _months as number[],
  60. month: _month as number,
  61. days: _days as number[],
  62. day: _day as number,
  63. value: [_year - 2000, _month - 1, _day - 1] as number[],
  64. result: [] as number[],
  65. indicatorStyle: 'height: 50px;',
  66. indicatorClass: '',
  67. maskStyle: '',
  68. maskClass: '',
  69. maskTopStyle: '',
  70. maskBottomStyle: ''
  71. }
  72. },
  73. methods: {
  74. setIndicatorStyle(checked : boolean) {
  75. const extraStyle = 'height: 50px;border:#ff5500 solid 1px;background:rgba(182, 179, 255, 0.4);';
  76. // #ifdef APP-HARMONY
  77. this.indicatorStyle = checked ? extraStyle : "height: 50px;border:none;background:transparent;";
  78. // #endif
  79. // #ifndef APP-HARMONY
  80. this.indicatorStyle = checked ? extraStyle : "height: 50px;";
  81. // #endif
  82. },
  83. setIndicatorClass(checked : boolean) {
  84. this.indicatorClass = checked ? "indicator-test" : ""
  85. },
  86. setMaskStyle(checked : boolean) {
  87. const extraMaskStyle = "background-image: linear-gradient(to bottom, #d8e5ff, rgba(216, 229, 255, 0));"
  88. this.maskStyle = checked ? extraMaskStyle : ""
  89. },
  90. setMaskClass(checked : boolean) {
  91. this.maskClass = checked ? "mask-test" : ""
  92. },
  93. setMaskTopStyle(checked : boolean) {
  94. const linearToTop = "background-image: linear-gradient(to bottom, #f4ff73, rgba(216, 229, 255, 0));"
  95. // #ifdef APP-HARMONY
  96. this.maskTopStyle = checked ? linearToTop : "background-image: linear-gradient(to bottom, transparent, transparent);"
  97. // #endif
  98. // #ifndef APP-HARMONY
  99. this.maskTopStyle = checked ? linearToTop : ""
  100. // #endif
  101. },
  102. setMaskBottomStyle(checked : boolean) {
  103. const linearToBottom = "background-image: linear-gradient(to top, #f4ff73, rgba(216, 229, 255, 0));"
  104. // #ifdef APP-HARMONY
  105. this.maskBottomStyle = checked ? linearToBottom : "background-image: linear-gradient(to bottom, transparent, transparent);"
  106. // #endif
  107. // #ifndef APP-HARMONY
  108. this.maskBottomStyle = checked ? linearToBottom : ""
  109. // #endif
  110. },
  111. // 自动化测试
  112. getEventCallbackNum() : number {
  113. return state.eventCallbackNum
  114. },
  115. // 自动化测试
  116. setEventCallbackNum(num : number) {
  117. setEventCallbackNum(num)
  118. },
  119. bindChange(e : UniPickerViewChangeEvent) {
  120. // 自动化测试 触发事件元素、type 类型
  121. // console.log(e.target?.tagName, e.type);
  122. if ((e.target?.tagName ?? '').includes('PICKER-VIEW')) {
  123. this.setEventCallbackNum(state.eventCallbackNum + 1)
  124. }
  125. if (e.type === 'change') {
  126. this.setEventCallbackNum(state.eventCallbackNum + 2)
  127. }
  128. const val = e.detail.value
  129. this.result = val
  130. this.year = this.years[val[0]]
  131. this.month = this.months[val[1]]
  132. this.day = this.days[val[2]]
  133. },
  134. setValue() {
  135. this.value = [0, 1, 30] as number[]
  136. },
  137. setValue1() {
  138. this.value = [10, 10, 10] as number[]
  139. },
  140. }
  141. }
  142. </script>
  143. <style>
  144. .picker-view {
  145. width: 100%;
  146. height: 320px;
  147. margin-top: 10px;
  148. margin-bottom: 20px;
  149. }
  150. .item {
  151. height: 50px;
  152. }
  153. .text {
  154. line-height: 50px;
  155. text-align: center;
  156. }
  157. /* 自动化测试 */
  158. .indicator-test {
  159. height: 50px;
  160. border: #ff5500 solid 1px;
  161. background:rgba(182, 179, 255, 0.4);
  162. }
  163. .mask-test {
  164. background-image: linear-gradient(to bottom, #d8e5ff, rgba(216, 229, 255, 0));
  165. }
  166. </style>