form.uvue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="scroll-view">
  4. <!-- #endif -->
  5. <view class="page">
  6. <form @submit="onFormSubmit" @reset="onFormReset">
  7. <view class="uni-form-item">
  8. <text class="title">姓名</text>
  9. <input class="uni-input" name="nickname" :value="nickname" placeholder="请输入姓名" maxlength="-1" />
  10. </view>
  11. <view class="uni-form-item">
  12. <text class="title">性别</text>
  13. <radio-group name="gender" class="flex-row">
  14. <view class="group-item">
  15. <radio value="0" :checked="gender=='0'" /><text>男</text>
  16. </view>
  17. <view class="group-item">
  18. <radio value="1" :checked="gender=='1'" /><text>女</text>
  19. </view>
  20. </radio-group>
  21. </view>
  22. <view class="uni-form-item">
  23. <text class="title">爱好</text>
  24. <checkbox-group name="loves" class="flex-row">
  25. <view class="group-item">
  26. <checkbox value="0" :checked="loves.indexOf('0')>-1" /><text>读书</text>
  27. </view>
  28. <view class="group-item">
  29. <checkbox value="1" :checked="loves.indexOf('1')>-1" /><text>写字</text>
  30. </view>
  31. </checkbox-group>
  32. </view>
  33. <view class="uni-form-item">
  34. <text class="title">年龄</text>
  35. <slider name="age" :value="age" :show-value="true"></slider>
  36. </view>
  37. <view class="uni-form-item">
  38. <text class="title">保留选项</text>
  39. <view>
  40. <switch name="switch" :checked="switch" />
  41. </view>
  42. </view>
  43. <view class="uni-form-item">
  44. <text class="title">备注</text>
  45. <textarea name="comment" :value="comment" placeholder="请输入备注" style="background: #FFF;" />
  46. <!-- <textarea class="uni-input" name="comment" :value="comment" placeholder="这个class的写法,导致iOS和Android产生了高度差异"/> -->
  47. </view>
  48. <!-- picker -->
  49. <!-- #ifdef APP-HARMONY -->
  50. <view class="uni-form-item flex-row">
  51. <text class="title">时区</text>
  52. <picker class="picker" name="timeZone" @change="onTimeZoneChange" :value="timeZoneIndex" :range="timeZoneList">
  53. <view class="uni-input pickerValue">{{timeZoneList[timeZoneIndex]}}</view>
  54. </picker>
  55. </view>
  56. <view class="uni-form-item flex-row">
  57. <text class="title">多列选择器</text>
  58. <picker class="picker pickerMulti" mode="multiSelector" @columnchange="onMultiPickerColumnChange"
  59. :value="multiIndex" :range="multiArray">
  60. <view class="uni-input pickerMultiValue">
  61. {{multiArray[0][multiIndex[0]]}},{{multiArray[1][multiIndex[1]]}},{{multiArray[2][multiIndex[2]]}}
  62. </view>
  63. </picker>
  64. </view>
  65. <view class="uni-form-item flex-row">
  66. <text class="title">时间选择器</text>
  67. <picker class="picker pickerTime" mode="time" :value="timePickerValue" start="09:01" end="21:01" @change="onTimeChange">
  68. <view class="uni-input">{{timePickerValue}}</view>
  69. </picker>
  70. </view>
  71. <view class="uni-form-item flex-row">
  72. <text class="title">日期选择器</text>
  73. <picker class="picker pickerDate" mode="date" :value="datePickerValue" :start="startDate" :end="endDate"
  74. @change="onDateChange">
  75. <view class="uni-input">{{datePickerValue}}</view>
  76. </picker>
  77. </view>
  78. <!-- #endif -->
  79. <view class="uni-form-item">
  80. <text class="title">时间</text>
  81. <picker-view class="picker-view" name="time" :value="time" indicator-style="height:50px">
  82. <picker-view-column>
  83. <view class="picker-view-item" v-for="(item,index) in hours" :key="index">
  84. <text class="picker-view-text">{{item}}时</text>
  85. </view>
  86. </picker-view-column>
  87. <picker-view-column>
  88. <view class="picker-view-item" v-for="(item,index) in minutes" :key="index">
  89. <text class="picker-view-text">{{item}}分</text>
  90. </view>
  91. </picker-view-column>
  92. </picker-view>
  93. </view>
  94. <view class="flex-row">
  95. <button class="btn btn-submit" form-type="submit" type="primary">Submit</button>
  96. <button class="btn btn-reset" type="default" form-type="reset">Reset</button>
  97. </view>
  98. </form>
  99. <view class="result">提交的表单数据</view>
  100. <textarea class="textarea" :value="formDataText" :maxlength="-1" :auto-height="true"></textarea>
  101. </view>
  102. <!-- #ifdef APP -->
  103. </scroll-view>
  104. <!-- #endif -->
  105. </template>
  106. <script>
  107. function getDate(type ?: string) : string {
  108. const date = new Date();
  109. let year = date.getFullYear();
  110. let month = date.getMonth() + 1;
  111. let day = date.getDate();
  112. if (type === 'start') {
  113. year = year - 10;
  114. } else if (type === 'end') {
  115. year = year + 10;
  116. }
  117. const monthString = month > 9 ? month.toString() : '0' + month;
  118. const dayString = day > 9 ? day.toString() : '0' + day;
  119. return `${year}-${monthString}-${dayString}`;
  120. }
  121. export default {
  122. data() {
  123. let hours = new Array<string>()
  124. let minutes = new Array<string>()
  125. for (let i = 1; i <= 24; i++) {
  126. hours.push(i.toString())
  127. }
  128. for (let i = 1; i <= 60; i++) {
  129. minutes.push(i.toString())
  130. }
  131. const date = new Date()
  132. return {
  133. nickname: '',
  134. gender: '0',
  135. age: 18,
  136. loves: ['0'],
  137. switch: true,
  138. timeZoneIndex: 0,
  139. timeZoneList: ['中国', '美国', '巴西', '日本'] as Array<string>,
  140. multiArray: [
  141. ['亚洲', '欧洲'],
  142. ['中国', '日本'],
  143. ['北京', '上海', '广州']
  144. ] as Array<Array<string>>,
  145. multiIndex: [0, 0, 0] as Array<number>,
  146. datePickerValue: getDate(null),
  147. startDate: getDate('start'),
  148. endDate: getDate('end'),
  149. timePickerValue: '12:01',
  150. time: [date.getHours() - 1, date.getMinutes() - 1] as Array<number>,
  151. hours: hours as Array<string>,
  152. minutes: minutes as Array<string>,
  153. comment: '',
  154. formData: {} as UTSJSONObject,
  155. // 仅测试
  156. testVerifySubmit: false,
  157. testVerifyReset: false,
  158. }
  159. },
  160. computed: {
  161. formDataText() : string {
  162. return JSON.stringify(this.formData)
  163. }
  164. },
  165. methods: {
  166. // #ifdef APP-HARMONY
  167. onTimeZoneChange(e : UniPickerChangeEvent) {
  168. this.timeZoneIndex = e.detail.value as number
  169. console.log('时区选择改变,携带值为:' + e.detail.value)
  170. },
  171. onMultiPickerColumnChange(e : UniPickerChangeEvent) {
  172. console.log('修改的列为:' + e.detail.column + ',值为:' + e.detail.value)
  173. this.multiIndex[e.detail.column] = e.detail.value as Array<number>
  174. switch (e.detail.column) {
  175. case 0: //拖动第1列
  176. switch (this.multiIndex[0]) {
  177. case 0:
  178. this.multiArray[1] = ['中国', '日本']
  179. this.multiArray[2] = ['北京', '上海', '广州']
  180. break
  181. case 1:
  182. this.multiArray[1] = ['英国', '法国']
  183. this.multiArray[2] = ['伦敦', '曼彻斯特']
  184. break
  185. }
  186. this.multiIndex.splice(1, 1, 0)
  187. this.multiIndex.splice(2, 1, 0)
  188. break
  189. case 1: //拖动第2列
  190. switch (this.multiIndex[0]) { //判断第一列是什么
  191. case 0:
  192. switch (this.multiIndex[1]) {
  193. case 0:
  194. this.multiArray[2] = ['北京', '上海', '广州']
  195. break
  196. case 1:
  197. this.multiArray[2] = ['东京', '北海道']
  198. break
  199. }
  200. break
  201. case 1:
  202. switch (this.multiIndex[1]) {
  203. case 0:
  204. this.multiArray[2] = ['伦敦', '曼彻斯特']
  205. break
  206. case 1:
  207. this.multiArray[2] = ['巴黎', '马赛']
  208. break
  209. }
  210. break
  211. }
  212. this.multiIndex.splice(2, 1, 0)
  213. break
  214. }
  215. this.$forceUpdate()
  216. },
  217. onDateChange(e : UniPickerChangeEvent) {
  218. this.date = e.detail.value as number
  219. },
  220. onTimeChange(e : UniPickerChangeEvent) {
  221. this.time = e.detail.value as string
  222. },
  223. // #endif
  224. onFormSubmit(e : UniFormSubmitEvent) {
  225. this.formData = e.detail.value
  226. // 仅测试
  227. this.testVerifySubmit = (e.type == 'submit' && (e.target?.tagName ?? '') == "FORM")
  228. },
  229. onFormReset(e : UniFormResetEvent) {
  230. this.formData = {}
  231. this.timeZoneIndex = 0
  232. // 仅测试
  233. this.testVerifyReset = (e.type == 'reset' && (e.target?.tagName ?? '') == "FORM")
  234. }
  235. }
  236. }
  237. </script>
  238. <style>
  239. .scroll-view {
  240. flex: 1;
  241. }
  242. .page {
  243. padding: 15px;
  244. }
  245. .flex-row {
  246. flex-direction: row;
  247. align-items: center;
  248. }
  249. .uni-form-item {
  250. padding: 15px 0;
  251. }
  252. .title {
  253. margin-bottom: 10px;
  254. opacity: 0.8;
  255. }
  256. .picker {
  257. margin-left: 15px;
  258. }
  259. .group-item {
  260. flex-direction: row;
  261. margin-right: 20px;
  262. }
  263. .picker-view {
  264. width: 200px;
  265. height: 320px;
  266. margin-top: 10px;
  267. }
  268. .picker-view-item {
  269. height: 50px;
  270. }
  271. .picker-view-text {
  272. line-height: 50px;
  273. text-align: center;
  274. }
  275. .btn {
  276. flex: 1;
  277. }
  278. .btn-submit {
  279. margin-right: 5px;
  280. }
  281. .btn-reset {
  282. margin-left: 5px;
  283. }
  284. .result {
  285. margin-top: 30px;
  286. }
  287. .textarea {
  288. margin-top: 5px;
  289. padding: 5px;
  290. background-color: #fff;
  291. }
  292. </style>